coupon_code 0.0.3 → 0.1.0

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
2
  SHA256:
3
- metadata.gz: 76d25c8d75ec9f94df915aed8a41ff6141cea7e20d2342c09412763916d81869
4
- data.tar.gz: f1ffd10712e8e966276d4b54b466aea30ffc377c76e8044d8d437a7f1efff275
3
+ metadata.gz: 4a5fe9e0fccbe7e2a8897b9fc37d04d7eda798eba03ba7275a890ccc269aabcb
4
+ data.tar.gz: f4d0bb81a8b83a11ae053109a833b9b6c77aa7e5eb7edcc720ff3159dd912102
5
5
  SHA512:
6
- metadata.gz: 62f908f274676f03fb33adf0c8f3243171619df9a636bd00016cd2ab8641468436fdb638ca690cac63059d9ec2880fddde832efe2a6c3afa885c5e83af012128
7
- data.tar.gz: 8abdcecb97a48f026eafda0b794149594a29425458da24e1308827506aaa63cb2e30ae36254b8e190f8cb665eddae71842bdd8193b84075d9a3ab079ddb5a107
6
+ metadata.gz: 6129aa503dd392ef1ddb77ecc8cda803265cc60b7b8bb4074d5925b9963687b3458b8920dcf0529fa1924d36da32be98d450db446e57bfab6b9aed3fe784fa1f
7
+ data.tar.gz: 1d26342a6f24ba0eb2c257deaf66fbd7015080c30bfb8e0199e35c50bed7545036b0cf3a78673f042419bc12c48d789e0c4a38070013a16b7e3de1ab81bf0380
data/CHANGELOG.md CHANGED
@@ -6,5 +6,12 @@
6
6
  ### 0.0.2
7
7
  - Bug fix `.validate` can't handle invalid characters and produces an error. (mrclmrvn)
8
8
 
9
- ### 0.0.2
9
+ ### 0.0.3
10
10
  - add ability to specify length #15 (fourcolors)
11
+
12
+ ### 0.1.0
13
+ - **Behaviour change:** `.validate` now returns `nil` for codes with a wrong check digit. 0.0.3 accepted them by mistake. (#17)
14
+ - Generate codes with `SecureRandom` instead of `rand`. (#17)
15
+ - Ruby 4 support: frozen string literals, no warnings. (#17)
16
+ - Tested on Ruby 2.6 to 4.0; the gem ships only the files it needs and requires MFA to publish. (#18)
17
+ - Fix `.generate` hanging when only `part_length:` is given, and stop it emptying the options hash passed in. (#19)
data/README-ko.md CHANGED
@@ -15,8 +15,6 @@
15
15
  하지만 현 시점에는 펄 버전이 가진 세심하고 다양한 기능이 모두 구현되어 있지 않다는 점을 기억해 주십시오.
16
16
  평문을 입력해 코드를 생성하는 기능이나 자동교정, 금칙어 설정, 그리고 클라이언트 상에서의 코드 검증을 위한 jQuery 플러그인은 포함되어있지 않습니다.
17
17
 
18
- 이 루비 젬은 [https://stripes.co.kr](https://stripes.co.kr)를 위해 개발됐습니다.
19
-
20
18
  ## 설치
21
19
 
22
20
  애플리케이션의 Gemfile에 아래의 코드 한 줄을 추가하십시오:
@@ -46,9 +44,9 @@
46
44
  쿠폰 코드를 만들 때 아래와 같이 옵션값을 넣어, 기본 설정인 3부분이 아닌 쿠폰 코드를 얻을 수 있습니다.
47
45
 
48
46
  >> CouponCode.generate(parts: 4)
49
- => "1K7Q-CTFM-LMTC-DLGP"
50
- >> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 4)
51
- => "1K7Q-CTFM-LMTC-DLGP"
47
+ => "7YQH-1FU7-E1HX-0BG9"
48
+ >> CouponCode.validate("7YQH-1FU7-E1HX-0BG9", 4)
49
+ => "7YQH-1FU7-E1HX-0BG9"
52
50
 
53
51
  ## 테스팅
54
52
 
data/README.md CHANGED
@@ -1,6 +1,4 @@
1
- [![Build Status](https://travis-ci.org/baxang/coupon-code.svg)](https://travis-ci.org/baxang/coupon-code)
2
- [![Code Climate](https://codeclimate.com/github/baxang/coupon-code/badges/gpa.svg)](https://codeclimate.com/github/baxang/coupon-code)
3
- [![Test Coverage](https://codeclimate.com/github/baxang/coupon-code/badges/coverage.svg)](https://codeclimate.com/github/baxang/coupon-code/coverage)
1
+ [![Ruby](https://github.com/baxang/coupon-code/actions/workflows/ruby.yml/badge.svg)](https://github.com/baxang/coupon-code/actions/workflows/ruby.yml)
4
2
  [![Gem Version](https://badge.fury.io/rb/coupon_code.svg)](http://badge.fury.io/rb/coupon_code)
5
3
 
6
4
  # CouponCode
@@ -13,8 +11,6 @@ It is a Ruby implementation of [Grant][grant]'s [Algorithm::CouponCode][couponco
13
11
 
14
12
  Please be aware that some of the original CPAN module's features are not implemented yet. For example, generating codes by plaintext, auto correction, and jQuery plugin are missing in this gem.
15
13
 
16
- This gem is developed for https://stripes.co.kr
17
-
18
14
  ## Installation
19
15
 
20
16
  Add this line to your application's Gemfile:
@@ -44,16 +40,16 @@ Or install it yourself as:
44
40
  You can change the number of parts of the generated code by passing an option hash value like:
45
41
 
46
42
  >> CouponCode.generate(parts: 4)
47
- => "1K7Q-CTFM-LMTC-DLGP"
48
- >> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 4)
49
- => "1K7Q-CTFM-LMTC-DLGP"
43
+ => "7YQH-1FU7-E1HX-0BG9"
44
+ >> CouponCode.validate("7YQH-1FU7-E1HX-0BG9", 4)
45
+ => "7YQH-1FU7-E1HX-0BG9"
50
46
 
51
47
  You can also change the length of each part like:
52
48
 
53
49
  >> CouponCode.generate(parts: 3, part_length: 5)
54
50
  => "GRG65-X0PF4-KP7TJ"
55
- >> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 3, 5)
56
- => "1K7Q-CTFM-LMTC-DLGP"
51
+ >> CouponCode.validate("GRG65-X0PF4-KP7TJ", 3, 5)
52
+ => "GRG65-X0PF4-KP7TJ"
57
53
 
58
54
  ## Testing
59
55
 
data/bin/coupon-code CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'coupon_code'
3
+ require "coupon_code"
4
4
 
5
5
  code = CouponCode.generate
6
6
  puts code
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CouponCode
2
- VERSION = "0.0.3".freeze
4
+ VERSION = "0.1.0"
3
5
  end
data/lib/coupon_code.rb CHANGED
@@ -1,22 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "coupon_code/version"
2
4
  require "securerandom"
3
- require "digest/sha1"
4
5
 
5
6
  module CouponCode
6
7
  SYMBOL = "0123456789ABCDEFGHJKLMNPQRTUVWXY"
7
8
  PARTS = 3
8
9
  LENGTH = 4
9
10
 
10
- def self.generate(options = {parts: PARTS, part_length: LENGTH})
11
- num_parts = options.delete(:parts)
12
- length_of_parts = options.delete(:part_length) || LENGTH
11
+ def self.generate(options = {})
12
+ num_parts = options[:parts] || PARTS
13
+ length_of_parts = options[:part_length] || LENGTH
13
14
 
14
15
  parts = []
15
16
  (1..num_parts).each do |i|
16
- part = ""
17
- (1...length_of_parts).each { part << random_symbol }
18
- part << checkdigit_alg_1(part, i)
19
- parts << part
17
+ data = (1...length_of_parts).map { random_symbol }.join
18
+ parts << data + checkdigit_alg_1(data, i)
20
19
  end
21
20
  parts.join("-")
22
21
  end
@@ -26,11 +25,7 @@ module CouponCode
26
25
  code.gsub!(/[^#{SYMBOL}]+/o, "")
27
26
  parts = code.scan(/[#{SYMBOL}]{#{part_length}}/)
28
27
  return if parts.length != num_parts
29
- parts.each_with_index do |part, i|
30
- data = part[0...(part_length - 1)]
31
- check = part[-1]
32
- break if check != checkdigit_alg_1(data, i + 1)
33
- end
28
+ return unless parts.each_with_index.all? { |part, i| part[-1] == checkdigit_alg_1(part[0...-1], i + 1) }
34
29
  parts.join("-")
35
30
  end
36
31
 
@@ -43,6 +38,6 @@ module CouponCode
43
38
  end
44
39
 
45
40
  def self.random_symbol
46
- SYMBOL[rand(SYMBOL.length)]
41
+ SYMBOL[SecureRandom.random_number(SYMBOL.length)]
47
42
  end
48
43
  end
metadata CHANGED
@@ -1,71 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coupon_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sanghyun Park
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-23 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '3.4'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
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'
11
+ date: 2026-10-22 00:00:00.000000000 Z
12
+ dependencies: []
69
13
  description: A Ruby implementation of Perl's Algorithm::CouponCode CPAN module.
70
14
  email:
71
15
  - sh@baxang.com
@@ -74,27 +18,21 @@ executables:
74
18
  extensions: []
75
19
  extra_rdoc_files: []
76
20
  files:
77
- - ".github/dependabot.yml"
78
- - ".github/workflows/ruby.yml"
79
- - ".gitignore"
80
- - ".ruby-version"
81
- - ".standard.yml"
82
21
  - CHANGELOG.md
83
- - Gemfile
84
22
  - LICENSE.txt
85
23
  - README-ko.md
86
24
  - README.md
87
- - Rakefile
88
25
  - bin/coupon-code
89
- - coupon_code.gemspec
90
26
  - lib/coupon_code.rb
91
27
  - lib/coupon_code/version.rb
92
- - spec/coupon_code_spec.rb
93
- - spec/spec_helper.rb
94
28
  homepage: https://github.com/baxang/coupon-code
95
29
  licenses:
96
30
  - MIT
97
- metadata: {}
31
+ metadata:
32
+ homepage_uri: https://github.com/baxang/coupon-code
33
+ source_code_uri: https://github.com/baxang/coupon-code/tree/master
34
+ changelog_uri: https://github.com/baxang/coupon-code/blob/master/CHANGELOG.md
35
+ rubygems_mfa_required: 'true'
98
36
  post_install_message:
99
37
  rdoc_options: []
100
38
  require_paths:
@@ -1,8 +0,0 @@
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
@@ -1,36 +0,0 @@
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/.gitignore DELETED
@@ -1,14 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- *.bundle
11
- *.so
12
- *.o
13
- *.a
14
- mkmf.log
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 3.3.4
data/.standard.yml DELETED
@@ -1,2 +0,0 @@
1
- format: progress # default: Standard::Formatter
2
- ruby_version: 2.6 # default: RUBY_VERSION
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in coupon_code.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task default: :spec
7
-
8
- task :console do
9
- exec "irb -r coupon_code -I ./lib"
10
- end
data/coupon_code.gemspec DELETED
@@ -1,26 +0,0 @@
1
- lib = File.expand_path("../lib", __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "coupon_code/version"
4
-
5
- Gem::Specification.new do |spec|
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"
15
-
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"]
19
-
20
- spec.required_ruby_version = ">= 2.6.0"
21
-
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"
26
- end
@@ -1,63 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe CouponCode do
4
- describe ".generate" do
5
- subject { described_class.generate }
6
- it { is_expected.not_to be_nil }
7
- it { is_expected.to match(/^[0-9A-Z-]+$/) }
8
- it { is_expected.to match(/^\w{4}-\w{4}-\w{4}$/) }
9
- it "generates a different code" do
10
- code2 = described_class.generate
11
- is_expected.not_to eq(code2)
12
- end
13
-
14
- context "2 parts" do
15
- subject { described_class.generate(parts: 2) }
16
- it { is_expected.to match(/^\w{4}-\w{4}$/) }
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
25
- end
26
-
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
31
-
32
- it "validates with a custom length" do
33
- expect(described_class.validate("GRG65-X0PF4-KP7TJ", 3, 5)).to eq("GRG65-X0PF4-KP7TJ")
34
- end
35
-
36
- it "validates and returns the code in uppercase letters" do
37
- expect(described_class.validate("1K7Q-ctfm-LMTC")).to eq("1K7Q-CTFM-LMTC")
38
- end
39
-
40
- it "returns nil for an invalid code" do
41
- expect(described_class.validate("1K7Q-CTFM")).to be_nil
42
- end
43
-
44
- it "handles invalid characters" do
45
- expect(described_class.validate("OK7Q-CTFM-LMTC")).to be_nil
46
- end
47
-
48
- context "valid cases: lowercase, different separator and parts" do
49
- [
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]
58
- ].each do |args|
59
- it { expect(described_class.validate(*args)).not_to be_nil }
60
- end
61
- end
62
- end
63
- end
data/spec/spec_helper.rb DELETED
@@ -1,95 +0,0 @@
1
- require "coupon_code"
2
- # This file was generated by the `rspec --init` command. Conventionally, all
3
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
- # The generated `.rspec` file contains `--require spec_helper` which will cause
5
- # this file to always be loaded, without a need to explicitly require it in any
6
- # files.
7
- #
8
- # Given that it is always loaded, you are encouraged to keep this file as
9
- # light-weight as possible. Requiring heavyweight dependencies from this file
10
- # will add to the boot time of your test suite on EVERY test run, even for an
11
- # individual file that may not need all of that loaded. Instead, consider making
12
- # a separate helper file that requires the additional dependencies and performs
13
- # the additional setup, and require it from the spec files that actually need
14
- # it.
15
- #
16
- # The `.rspec` file also contains a few flags that are not defaults but that
17
- # users commonly want.
18
- #
19
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
- RSpec.configure do |config|
21
- # rspec-expectations config goes here. You can use an alternate
22
- # assertion/expectation library such as wrong or the stdlib/minitest
23
- # assertions if you prefer.
24
- config.expect_with :rspec do |expectations|
25
- # This option will default to `true` in RSpec 4. It makes the `description`
26
- # and `failure_message` of custom matchers include text for helper methods
27
- # defined using `chain`, e.g.:
28
- # be_bigger_than(2).and_smaller_than(4).description
29
- # # => "be bigger than 2 and smaller than 4"
30
- # ...rather than:
31
- # # => "be bigger than 2"
32
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
33
- end
34
-
35
- # rspec-mocks config goes here. You can use an alternate test double
36
- # library (such as bogus or mocha) by changing the `mock_with` option here.
37
- config.mock_with :rspec do |mocks|
38
- # Prevents you from mocking or stubbing a method that does not exist on
39
- # a real object. This is generally recommended, and will default to
40
- # `true` in RSpec 4.
41
- mocks.verify_partial_doubles = true
42
- end
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
- # # 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
95
- end