coupon_code 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 66c35baf6d4c13f6943b85c3558e9a22c830b400
4
- data.tar.gz: b983fe59c933ede4c5351fd9875b66365ed2f471
2
+ SHA256:
3
+ metadata.gz: 76d25c8d75ec9f94df915aed8a41ff6141cea7e20d2342c09412763916d81869
4
+ data.tar.gz: f1ffd10712e8e966276d4b54b466aea30ffc377c76e8044d8d437a7f1efff275
5
5
  SHA512:
6
- metadata.gz: b85ea82fc307464cd877a2fc7cc1c856576765a81bb090b8c0bcf94bd8e97685978b3779f46c393b6b199881af4b9b75b9857b95c65019d3bc5c70cc0fb61d3b
7
- data.tar.gz: 55fa61617a7b1d012ab6a86704c79d82d2f9a3b143ba0f7b4d7e5787e52ef8fd88e428d1bc203353628a89e03f673077ca666d968a7377724ec9f53a1e87f405
6
+ metadata.gz: 62f908f274676f03fb33adf0c8f3243171619df9a636bd00016cd2ab8641468436fdb638ca690cac63059d9ec2880fddde832efe2a6c3afa885c5e83af012128
7
+ data.tar.gz: 8abdcecb97a48f026eafda0b794149594a29425458da24e1308827506aaa63cb2e30ae36254b8e190f8cb665eddae71842bdd8193b84075d9a3ab079ddb5a107
@@ -0,0 +1,8 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ time: "19:00"
8
+ open-pull-requests-limit: 10
@@ -0,0 +1,36 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - '*'
7
+ pull_request:
8
+ branches:
9
+ - '*'
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+
15
+ strategy:
16
+ matrix:
17
+ ruby-version: [ '2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4' ]
18
+
19
+ steps:
20
+ - name: Checkout code
21
+ uses: actions/checkout@v2
22
+
23
+ - name: Set up Ruby
24
+ uses: ruby/setup-ruby@v1
25
+ with:
26
+ ruby-version: ${{ matrix.ruby-version }}
27
+ bundler-cache: true
28
+
29
+ - name: Install dependencies
30
+ run: bundle install
31
+
32
+ - name: Run standardrb
33
+ run: bundle exec standardrb
34
+
35
+ - name: Run tests
36
+ run: bundle exec rspec
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.4
data/.standard.yml ADDED
@@ -0,0 +1,2 @@
1
+ format: progress # default: Standard::Formatter
2
+ ruby_version: 2.6 # default: RUBY_VERSION
data/CHANGELOG.md CHANGED
@@ -5,3 +5,6 @@
5
5
 
6
6
  ### 0.0.2
7
7
  - Bug fix `.validate` can't handle invalid characters and produces an error. (mrclmrvn)
8
+
9
+ ### 0.0.2
10
+ - add ability to specify length #15 (fourcolors)
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in coupon_code.gemspec
4
4
  gemspec
5
-
6
- gem 'codeclimate-test-reporter', group: :test, require: nil
data/README-ko.md CHANGED
@@ -53,7 +53,7 @@
53
53
  ## 테스팅
54
54
 
55
55
  ```ruby
56
- $ bundle exec rake test
56
+ $ bundle exec rake spec
57
57
  ```
58
58
  ## Thanks to
59
59
 
data/README.md CHANGED
@@ -48,10 +48,17 @@ You can change the number of parts of the generated code by passing an option ha
48
48
  >> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 4)
49
49
  => "1K7Q-CTFM-LMTC-DLGP"
50
50
 
51
+ You can also change the length of each part like:
52
+
53
+ >> CouponCode.generate(parts: 3, part_length: 5)
54
+ => "GRG65-X0PF4-KP7TJ"
55
+ >> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 3, 5)
56
+ => "1K7Q-CTFM-LMTC-DLGP"
57
+
51
58
  ## Testing
52
59
 
53
60
  ```ruby
54
- $ bundle exec rake test
61
+ $ bundle exec rake spec
55
62
  ```
56
63
 
57
64
  ## Thanks to
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
6
  task default: :spec
7
7
 
8
8
  task :console do
9
- exec 'irb -r coupon_code -I ./lib'
9
+ exec "irb -r coupon_code -I ./lib"
10
10
  end
data/coupon_code.gemspec CHANGED
@@ -1,27 +1,26 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'coupon_code/version'
3
+ require "coupon_code/version"
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = 'coupon_code'
8
- spec.version = CouponCode::VERSION
9
- spec.authors = ['Sanghyun Park']
10
- spec.email = ['sh@baxang.com']
11
- spec.summary = 'Generate and validate coupon codes.'
12
- spec.description = 'A Ruby implementation of Perl\'s' \
13
- ' Algorithm::CouponCode CPAN module.'
14
- spec.homepage = 'https://github.com/baxang/coupon-code'
15
- spec.license = 'MIT'
6
+ spec.name = "coupon_code"
7
+ spec.version = CouponCode::VERSION
8
+ spec.authors = ["Sanghyun Park"]
9
+ spec.email = ["sh@baxang.com"]
10
+ spec.summary = "Generate and validate coupon codes."
11
+ spec.description = "A Ruby implementation of Perl's" \
12
+ " Algorithm::CouponCode CPAN module."
13
+ spec.homepage = "https://github.com/baxang/coupon-code"
14
+ spec.license = "MIT"
16
15
 
17
- spec.files = `git ls-files -z`.split("\x0")
18
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
- spec.require_paths = ['lib']
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
21
19
 
22
- spec.required_ruby_version = '>= 1.9.3'
20
+ spec.required_ruby_version = ">= 2.6.0"
23
21
 
24
- spec.add_development_dependency 'bundler'
25
- spec.add_development_dependency 'rake'
26
- spec.add_development_dependency 'rspec', '~> 3.4'
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", "~> 3.4"
25
+ spec.add_development_dependency "standardrb"
27
26
  end
@@ -1,3 +1,3 @@
1
1
  module CouponCode
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = "0.0.3".freeze
3
3
  end
data/lib/coupon_code.rb CHANGED
@@ -1,39 +1,41 @@
1
- require 'coupon_code/version'
2
- require 'securerandom'
3
- require 'digest/sha1'
1
+ require "coupon_code/version"
2
+ require "securerandom"
3
+ require "digest/sha1"
4
4
 
5
5
  module CouponCode
6
- SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY'
7
- PARTS = 3
6
+ SYMBOL = "0123456789ABCDEFGHJKLMNPQRTUVWXY"
7
+ PARTS = 3
8
8
  LENGTH = 4
9
9
 
10
- def self.generate(options = { parts: PARTS })
10
+ def self.generate(options = {parts: PARTS, part_length: LENGTH})
11
11
  num_parts = options.delete(:parts)
12
+ length_of_parts = options.delete(:part_length) || LENGTH
13
+
12
14
  parts = []
13
15
  (1..num_parts).each do |i|
14
- part = ''
15
- (1...LENGTH).each { part << random_symbol }
16
+ part = ""
17
+ (1...length_of_parts).each { part << random_symbol }
16
18
  part << checkdigit_alg_1(part, i)
17
19
  parts << part
18
20
  end
19
- parts.join('-')
21
+ parts.join("-")
20
22
  end
21
23
 
22
- def self.validate(orig, num_parts = PARTS)
24
+ def self.validate(orig, num_parts = PARTS, part_length = LENGTH)
23
25
  code = orig.upcase
24
- code.gsub!(/[^#{SYMBOL}]+/, '')
25
- parts = code.scan(/[#{SYMBOL}]{#{LENGTH}}/)
26
+ code.gsub!(/[^#{SYMBOL}]+/o, "")
27
+ parts = code.scan(/[#{SYMBOL}]{#{part_length}}/)
26
28
  return if parts.length != num_parts
27
29
  parts.each_with_index do |part, i|
28
- data = part[0...(LENGTH - 1)]
30
+ data = part[0...(part_length - 1)]
29
31
  check = part[-1]
30
- return if check != checkdigit_alg_1(data, i + 1)
32
+ break if check != checkdigit_alg_1(data, i + 1)
31
33
  end
32
- parts.join('-')
34
+ parts.join("-")
33
35
  end
34
36
 
35
37
  def self.checkdigit_alg_1(orig, check)
36
- orig.split('').each_with_index do |c, _|
38
+ orig.chars.each_with_index do |c, _|
37
39
  k = SYMBOL.index(c)
38
40
  check = check * 19 + k
39
41
  end
@@ -1,56 +1,63 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  RSpec.describe CouponCode do
4
-
5
- describe '.generate' do
4
+ describe ".generate" do
6
5
  subject { described_class.generate }
7
6
  it { is_expected.not_to be_nil }
8
7
  it { is_expected.to match(/^[0-9A-Z-]+$/) }
9
8
  it { is_expected.to match(/^\w{4}-\w{4}-\w{4}$/) }
10
- it 'generates a different code' do
9
+ it "generates a different code" do
11
10
  code2 = described_class.generate
12
11
  is_expected.not_to eq(code2)
13
12
  end
14
13
 
15
- context '2 parts' do
14
+ context "2 parts" do
16
15
  subject { described_class.generate(parts: 2) }
17
16
  it { is_expected.to match(/^\w{4}-\w{4}$/) }
18
17
  end
18
+
19
+ context "when passed part_length" do
20
+ subject { described_class.generate(part_length: 4, parts: 1) }
21
+ it "allows part length to be set" do
22
+ expect(subject.length).to equal(4)
23
+ end
24
+ end
19
25
  end
20
26
 
21
- describe '.validate' do
27
+ describe ".validate" do
28
+ it "validates a good code" do
29
+ expect(described_class.validate("1K7Q-CTFM-LMTC")).to eq("1K7Q-CTFM-LMTC")
30
+ end
22
31
 
23
- it 'validates a good code' do
24
- expect(described_class.validate('1K7Q-CTFM-LMTC')).to eq('1K7Q-CTFM-LMTC')
32
+ it "validates with a custom length" do
33
+ expect(described_class.validate("GRG65-X0PF4-KP7TJ", 3, 5)).to eq("GRG65-X0PF4-KP7TJ")
25
34
  end
26
35
 
27
- it 'validates and returns the code in uppercase letters' do
28
- expect(described_class.validate('1K7Q-ctfm-LMTC')).to eq('1K7Q-CTFM-LMTC')
36
+ it "validates and returns the code in uppercase letters" do
37
+ expect(described_class.validate("1K7Q-ctfm-LMTC")).to eq("1K7Q-CTFM-LMTC")
29
38
  end
30
39
 
31
- it 'returns nil for an invalid code' do
32
- expect(described_class.validate('1K7Q-CTFM')).to be_nil
40
+ it "returns nil for an invalid code" do
41
+ expect(described_class.validate("1K7Q-CTFM")).to be_nil
33
42
  end
34
43
 
35
- it 'handles invalid characters' do
36
- expect(described_class.validate('OK7Q-CTFM-LMTC')).to be_nil
44
+ it "handles invalid characters" do
45
+ expect(described_class.validate("OK7Q-CTFM-LMTC")).to be_nil
37
46
  end
38
47
 
39
- context 'valid cases: lowercase, different separator and parts' do
48
+ context "valid cases: lowercase, different separator and parts" do
40
49
  [
41
- ['1k7q-ctfm-lmtc'],
42
- ['1K7Q/CTFM/LMTC'],
43
- ['1K7Q CTFM LMTC'],
44
- ['1k7qctfmlmtc'],
45
- ['1K7Q-CTFM', 2],
46
- ['7YQH-1FU7-E1HX-0BG9', 4],
47
- ['YENH-UPJK-PTE0-20U6-QYME', 5],
48
- ['YENH-UPJK-PTE0-20U6-QYME-RBK1', 6]
50
+ ["1k7q-ctfm-lmtc"],
51
+ ["1K7Q/CTFM/LMTC"],
52
+ ["1K7Q CTFM LMTC"],
53
+ ["1k7qctfmlmtc"],
54
+ ["1K7Q-CTFM", 2],
55
+ ["7YQH-1FU7-E1HX-0BG9", 4],
56
+ ["YENH-UPJK-PTE0-20U6-QYME", 5],
57
+ ["YENH-UPJK-PTE0-20U6-QYME-RBK1", 6]
49
58
  ].each do |args|
50
59
  it { expect(described_class.validate(*args)).not_to be_nil }
51
60
  end
52
61
  end
53
-
54
62
  end
55
-
56
63
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'coupon_code'
1
+ require "coupon_code"
2
2
  # This file was generated by the `rspec --init` command. Conventionally, all
3
3
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
4
  # The generated `.rspec` file contains `--require spec_helper` which will cause
@@ -41,57 +41,55 @@ RSpec.configure do |config|
41
41
  mocks.verify_partial_doubles = true
42
42
  end
43
43
 
44
- # The settings below are suggested to provide a good initial experience
45
- # with RSpec, but feel free to customize to your heart's content.
46
- =begin
47
- # These two settings work together to allow you to limit a spec run
48
- # to individual examples or groups you care about by tagging them with
49
- # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
- # get run.
51
- config.filter_run :focus
52
- config.run_all_when_everything_filtered = true
53
-
54
- # Allows RSpec to persist some state between runs in order to support
55
- # the `--only-failures` and `--next-failure` CLI options. We recommend
56
- # you configure your source control system to ignore this file.
57
- config.example_status_persistence_file_path = "spec/examples.txt"
58
-
59
- # Limits the available syntax to the non-monkey patched syntax that is
60
- # recommended. For more details, see:
61
- # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
62
- # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
63
- # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
64
- config.disable_monkey_patching!
65
-
66
- # This setting enables warnings. It's recommended, but in some cases may
67
- # be too noisy due to issues in dependencies.
68
- config.warnings = true
69
-
70
- # Many RSpec users commonly either run the entire suite or an individual
71
- # file, and it's useful to allow more verbose output when running an
72
- # individual spec file.
73
- if config.files_to_run.one?
74
- # Use the documentation formatter for detailed output,
75
- # unless a formatter has already been configured
76
- # (e.g. via a command-line flag).
77
- config.default_formatter = 'doc'
78
- end
79
-
80
- # Print the 10 slowest examples and example groups at the
81
- # end of the spec run, to help surface which specs are running
82
- # particularly slow.
83
- config.profile_examples = 10
84
-
85
- # Run specs in random order to surface order dependencies. If you find an
86
- # order dependency and want to debug it, you can fix the order by providing
87
- # the seed, which is printed after each run.
88
- # --seed 1234
89
- config.order = :random
90
-
91
- # Seed global randomization in this process using the `--seed` CLI option.
92
- # Setting this allows you to use `--seed` to deterministically reproduce
93
- # test failures related to randomization by passing the same `--seed` value
94
- # as the one that triggered the failure.
95
- Kernel.srand config.seed
96
- =end
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+ # # These two settings work together to allow you to limit a spec run
47
+ # # to individual examples or groups you care about by tagging them with
48
+ # # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # # get run.
50
+ # config.filter_run :focus
51
+ # config.run_all_when_everything_filtered = true
52
+ #
53
+ # # Allows RSpec to persist some state between runs in order to support
54
+ # # the `--only-failures` and `--next-failure` CLI options. We recommend
55
+ # # you configure your source control system to ignore this file.
56
+ # config.example_status_persistence_file_path = "spec/examples.txt"
57
+ #
58
+ # # Limits the available syntax to the non-monkey patched syntax that is
59
+ # # recommended. For more details, see:
60
+ # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
61
+ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
+ # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
63
+ # config.disable_monkey_patching!
64
+ #
65
+ # # This setting enables warnings. It's recommended, but in some cases may
66
+ # # be too noisy due to issues in dependencies.
67
+ # config.warnings = true
68
+ #
69
+ # # Many RSpec users commonly either run the entire suite or an individual
70
+ # # file, and it's useful to allow more verbose output when running an
71
+ # # individual spec file.
72
+ # if config.files_to_run.one?
73
+ # # Use the documentation formatter for detailed output,
74
+ # # unless a formatter has already been configured
75
+ # # (e.g. via a command-line flag).
76
+ # config.default_formatter = 'doc'
77
+ # end
78
+ #
79
+ # # Print the 10 slowest examples and example groups at the
80
+ # # end of the spec run, to help surface which specs are running
81
+ # # particularly slow.
82
+ # config.profile_examples = 10
83
+ #
84
+ # # Run specs in random order to surface order dependencies. If you find an
85
+ # # order dependency and want to debug it, you can fix the order by providing
86
+ # # the seed, which is printed after each run.
87
+ # # --seed 1234
88
+ # config.order = :random
89
+ #
90
+ # # Seed global randomization in this process using the `--seed` CLI option.
91
+ # # Setting this allows you to use `--seed` to deterministically reproduce
92
+ # # test failures related to randomization by passing the same `--seed` value
93
+ # # as the one that triggered the failure.
94
+ # Kernel.srand config.seed
97
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coupon_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sanghyun Park
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-08 00:00:00.000000000 Z
11
+ date: 2024-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: standardrb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: A Ruby implementation of Perl's Algorithm::CouponCode CPAN module.
56
70
  email:
57
71
  - sh@baxang.com
@@ -60,10 +74,11 @@ executables:
60
74
  extensions: []
61
75
  extra_rdoc_files: []
62
76
  files:
77
+ - ".github/dependabot.yml"
78
+ - ".github/workflows/ruby.yml"
63
79
  - ".gitignore"
64
- - ".hound.yml"
65
- - ".rubocop.yml"
66
- - ".travis.yml"
80
+ - ".ruby-version"
81
+ - ".standard.yml"
67
82
  - CHANGELOG.md
68
83
  - Gemfile
69
84
  - LICENSE.txt
@@ -80,7 +95,7 @@ homepage: https://github.com/baxang/coupon-code
80
95
  licenses:
81
96
  - MIT
82
97
  metadata: {}
83
- post_install_message:
98
+ post_install_message:
84
99
  rdoc_options: []
85
100
  require_paths:
86
101
  - lib
@@ -88,18 +103,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
103
  requirements:
89
104
  - - ">="
90
105
  - !ruby/object:Gem::Version
91
- version: 1.9.3
106
+ version: 2.6.0
92
107
  required_rubygems_version: !ruby/object:Gem::Requirement
93
108
  requirements:
94
109
  - - ">="
95
110
  - !ruby/object:Gem::Version
96
111
  version: '0'
97
112
  requirements: []
98
- rubyforge_project:
99
- rubygems_version: 2.4.5
100
- signing_key:
113
+ rubygems_version: 3.5.11
114
+ signing_key:
101
115
  specification_version: 4
102
116
  summary: Generate and validate coupon codes.
103
- test_files:
104
- - spec/coupon_code_spec.rb
105
- - spec/spec_helper.rb
117
+ test_files: []
data/.hound.yml DELETED
@@ -1,3 +0,0 @@
1
- ruby:
2
- enabled: true
3
- config_file: .rubocop.yml
data/.rubocop.yml DELETED
@@ -1,5 +0,0 @@
1
- Documentation:
2
- Enabled: false
3
-
4
- Style/FileName:
5
- Exclude: ['bin/coupon-code']
data/.travis.yml DELETED
@@ -1,12 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
- rvm:
4
- - 1.9.3
5
- - 2.0.0
6
- - 2.1.1
7
- script: 'bundle exec rake'
8
- before_install:
9
- - gem update bundler
10
- addons:
11
- code_climate:
12
- repo_token: 8a7079440da500bd46bff928386a4a512cf5a3c314b4882a2ce7e6fe160b4f45