Skip to content

Commit

Permalink
chore: use standardrb
Browse files Browse the repository at this point in the history
  • Loading branch information
baxang committed Oct 16, 2024
1 parent 398913f commit f746834
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 119 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ jobs:
- name: Install dependencies
run: bundle install

- name: Run standardrb
run: bundle exec standardrb

- name: Run tests
run: bundle exec rspec
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in coupon_code.gemspec
gemspec

gem 'codeclimate-test-reporter', group: :test, require: nil
gem "codeclimate-test-reporter", group: :test, require: nil
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task default: :spec

task :console do
exec 'irb -r coupon_code -I ./lib'
exec "irb -r coupon_code -I ./lib"
end
39 changes: 19 additions & 20 deletions coupon_code.gemspec
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'coupon_code/version'
require "coupon_code/version"

Gem::Specification.new do |spec|
spec.name = 'coupon_code'
spec.version = CouponCode::VERSION
spec.authors = ['Sanghyun Park']
spec.email = ['[email protected]']
spec.summary = 'Generate and validate coupon codes.'
spec.description = 'A Ruby implementation of Perl\'s' \
' Algorithm::CouponCode CPAN module.'
spec.homepage = 'https://github.com/baxang/coupon-code'
spec.license = 'MIT'
spec.name = "coupon_code"
spec.version = CouponCode::VERSION
spec.authors = ["Sanghyun Park"]
spec.email = ["[email protected]"]
spec.summary = "Generate and validate coupon codes."
spec.description = "A Ruby implementation of Perl's" \
" Algorithm::CouponCode CPAN module."
spec.homepage = "https://github.com/baxang/coupon-code"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.required_ruby_version = '>= 1.9.3'
spec.required_ruby_version = ">= 1.9.3"

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec', '~> 3.4'
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 3.4"
spec.add_development_dependency "standardrb"
end
26 changes: 13 additions & 13 deletions lib/coupon_code.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
require 'coupon_code/version'
require 'securerandom'
require 'digest/sha1'
require "coupon_code/version"
require "securerandom"
require "digest/sha1"

module CouponCode
SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY'
PARTS = 3
SYMBOL = "0123456789ABCDEFGHJKLMNPQRTUVWXY"
PARTS = 3
LENGTH = 4

def self.generate(options = { parts: PARTS })
def self.generate(options = {parts: PARTS})
num_parts = options.delete(:parts)
parts = []
(1..num_parts).each do |i|
part = ''
part = ""
(1...LENGTH).each { part << random_symbol }
part << checkdigit_alg_1(part, i)
parts << part
end
parts.join('-')
parts.join("-")
end

def self.validate(orig, num_parts = PARTS)
code = orig.upcase
code.gsub!(/[^#{SYMBOL}]+/, '')
parts = code.scan(/[#{SYMBOL}]{#{LENGTH}}/)
code.gsub!(/[^#{SYMBOL}]+/o, "")
parts = code.scan(/[#{SYMBOL}]{#{LENGTH}}/o)
return if parts.length != num_parts
parts.each_with_index do |part, i|
data = part[0...(LENGTH - 1)]
data = part[0...(LENGTH - 1)]
check = part[-1]
return if check != checkdigit_alg_1(data, i + 1)
end
parts.join('-')
parts.join("-")
end

def self.checkdigit_alg_1(orig, check)
orig.split('').each_with_index do |c, _|
orig.split("").each_with_index do |c, _|
k = SYMBOL.index(c)
check = check * 19 + k
end
Expand Down
2 changes: 1 addition & 1 deletion lib/coupon_code/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CouponCode
VERSION = '0.0.2'.freeze
VERSION = "0.0.2".freeze
end
48 changes: 22 additions & 26 deletions spec/coupon_code_spec.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
require 'spec_helper'
require "spec_helper"

RSpec.describe CouponCode do

describe '.generate' do
describe ".generate" do
subject { described_class.generate }
it { is_expected.not_to be_nil }
it { is_expected.to match(/^[0-9A-Z-]+$/) }
it { is_expected.to match(/^\w{4}-\w{4}-\w{4}$/) }
it 'generates a different code' do
it "generates a different code" do
code2 = described_class.generate
is_expected.not_to eq(code2)
end

context '2 parts' do
context "2 parts" do
subject { described_class.generate(parts: 2) }
it { is_expected.to match(/^\w{4}-\w{4}$/) }
end
end

describe '.validate' do

it 'validates a good code' do
expect(described_class.validate('1K7Q-CTFM-LMTC')).to eq('1K7Q-CTFM-LMTC')
describe ".validate" do
it "validates a good code" do
expect(described_class.validate("1K7Q-CTFM-LMTC")).to eq("1K7Q-CTFM-LMTC")
end

it 'validates and returns the code in uppercase letters' do
expect(described_class.validate('1K7Q-ctfm-LMTC')).to eq('1K7Q-CTFM-LMTC')
it "validates and returns the code in uppercase letters" do
expect(described_class.validate("1K7Q-ctfm-LMTC")).to eq("1K7Q-CTFM-LMTC")
end

it 'returns nil for an invalid code' do
expect(described_class.validate('1K7Q-CTFM')).to be_nil
it "returns nil for an invalid code" do
expect(described_class.validate("1K7Q-CTFM")).to be_nil
end

it 'handles invalid characters' do
expect(described_class.validate('OK7Q-CTFM-LMTC')).to be_nil
it "handles invalid characters" do
expect(described_class.validate("OK7Q-CTFM-LMTC")).to be_nil
end

context 'valid cases: lowercase, different separator and parts' do
context "valid cases: lowercase, different separator and parts" do
[
['1k7q-ctfm-lmtc'],
['1K7Q/CTFM/LMTC'],
['1K7Q CTFM LMTC'],
['1k7qctfmlmtc'],
['1K7Q-CTFM', 2],
['7YQH-1FU7-E1HX-0BG9', 4],
['YENH-UPJK-PTE0-20U6-QYME', 5],
['YENH-UPJK-PTE0-20U6-QYME-RBK1', 6]
["1k7q-ctfm-lmtc"],
["1K7Q/CTFM/LMTC"],
["1K7Q CTFM LMTC"],
["1k7qctfmlmtc"],
["1K7Q-CTFM", 2],
["7YQH-1FU7-E1HX-0BG9", 4],
["YENH-UPJK-PTE0-20U6-QYME", 5],
["YENH-UPJK-PTE0-20U6-QYME-RBK1", 6]
].each do |args|
it { expect(described_class.validate(*args)).not_to be_nil }
end
end

end

end
106 changes: 52 additions & 54 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'coupon_code'
require "coupon_code"
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
Expand Down Expand Up @@ -41,57 +41,55 @@
mocks.verify_partial_doubles = true
end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
# # These two settings work together to allow you to limit a spec run
# # to individual examples or groups you care about by tagging them with
# # `:focus` metadata. When nothing is tagged with `:focus`, all examples
# # get run.
# config.filter_run :focus
# config.run_all_when_everything_filtered = true
#
# # Allows RSpec to persist some state between runs in order to support
# # the `--only-failures` and `--next-failure` CLI options. We recommend
# # you configure your source control system to ignore this file.
# config.example_status_persistence_file_path = "spec/examples.txt"
#
# # Limits the available syntax to the non-monkey patched syntax that is
# # recommended. For more details, see:
# # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
# config.disable_monkey_patching!
#
# # This setting enables warnings. It's recommended, but in some cases may
# # be too noisy due to issues in dependencies.
# config.warnings = true
#
# # Many RSpec users commonly either run the entire suite or an individual
# # file, and it's useful to allow more verbose output when running an
# # individual spec file.
# if config.files_to_run.one?
# # Use the documentation formatter for detailed output,
# # unless a formatter has already been configured
# # (e.g. via a command-line flag).
# config.default_formatter = 'doc'
# end
#
# # Print the 10 slowest examples and example groups at the
# # end of the spec run, to help surface which specs are running
# # particularly slow.
# config.profile_examples = 10
#
# # Run specs in random order to surface order dependencies. If you find an
# # order dependency and want to debug it, you can fix the order by providing
# # the seed, which is printed after each run.
# # --seed 1234
# config.order = :random
#
# # Seed global randomization in this process using the `--seed` CLI option.
# # Setting this allows you to use `--seed` to deterministically reproduce
# # test failures related to randomization by passing the same `--seed` value
# # as the one that triggered the failure.
# Kernel.srand config.seed
end

0 comments on commit f746834

Please sign in to comment.