coupon_code 0.0.2 → 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
- SHA1:
3
- metadata.gz: 66c35baf6d4c13f6943b85c3558e9a22c830b400
4
- data.tar.gz: b983fe59c933ede4c5351fd9875b66365ed2f471
2
+ SHA256:
3
+ metadata.gz: 4a5fe9e0fccbe7e2a8897b9fc37d04d7eda798eba03ba7275a890ccc269aabcb
4
+ data.tar.gz: f4d0bb81a8b83a11ae053109a833b9b6c77aa7e5eb7edcc720ff3159dd912102
5
5
  SHA512:
6
- metadata.gz: b85ea82fc307464cd877a2fc7cc1c856576765a81bb090b8c0bcf94bd8e97685978b3779f46c393b6b199881af4b9b75b9857b95c65019d3bc5c70cc0fb61d3b
7
- data.tar.gz: 55fa61617a7b1d012ab6a86704c79d82d2f9a3b143ba0f7b4d7e5787e52ef8fd88e428d1bc203353628a89e03f673077ca666d968a7377724ec9f53a1e87f405
6
+ metadata.gz: 6129aa503dd392ef1ddb77ecc8cda803265cc60b7b8bb4074d5925b9963687b3458b8920dcf0529fa1924d36da32be98d450db446e57bfab6b9aed3fe784fa1f
7
+ data.tar.gz: 1d26342a6f24ba0eb2c257deaf66fbd7015080c30bfb8e0199e35c50bed7545036b0cf3a78673f042419bc12c48d789e0c4a38070013a16b7e3de1ab81bf0380
data/CHANGELOG.md CHANGED
@@ -5,3 +5,13 @@
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.3
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,14 +44,14 @@
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
 
55
53
  ```ruby
56
- $ bundle exec rake test
54
+ $ bundle exec rake spec
57
55
  ```
58
56
  ## Thanks to
59
57
 
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,14 +40,21 @@ 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"
46
+
47
+ You can also change the length of each part like:
48
+
49
+ >> CouponCode.generate(parts: 3, part_length: 5)
50
+ => "GRG65-X0PF4-KP7TJ"
51
+ >> CouponCode.validate("GRG65-X0PF4-KP7TJ", 3, 5)
52
+ => "GRG65-X0PF4-KP7TJ"
50
53
 
51
54
  ## Testing
52
55
 
53
56
  ```ruby
54
- $ bundle exec rake test
57
+ $ bundle exec rake spec
55
58
  ```
56
59
 
57
60
  ## Thanks to
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.2'.freeze
4
+ VERSION = "0.1.0"
3
5
  end
data/lib/coupon_code.rb CHANGED
@@ -1,39 +1,36 @@
1
- require 'coupon_code/version'
2
- require 'securerandom'
3
- require 'digest/sha1'
1
+ # frozen_string_literal: true
2
+
3
+ require "coupon_code/version"
4
+ require "securerandom"
4
5
 
5
6
  module CouponCode
6
- SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY'
7
- PARTS = 3
7
+ SYMBOL = "0123456789ABCDEFGHJKLMNPQRTUVWXY"
8
+ PARTS = 3
8
9
  LENGTH = 4
9
10
 
10
- def self.generate(options = { parts: PARTS })
11
- num_parts = options.delete(:parts)
11
+ def self.generate(options = {})
12
+ num_parts = options[:parts] || PARTS
13
+ length_of_parts = options[:part_length] || LENGTH
14
+
12
15
  parts = []
13
16
  (1..num_parts).each do |i|
14
- part = ''
15
- (1...LENGTH).each { part << random_symbol }
16
- part << checkdigit_alg_1(part, i)
17
- parts << part
17
+ data = (1...length_of_parts).map { random_symbol }.join
18
+ parts << data + checkdigit_alg_1(data, i)
18
19
  end
19
- parts.join('-')
20
+ parts.join("-")
20
21
  end
21
22
 
22
- def self.validate(orig, num_parts = PARTS)
23
+ def self.validate(orig, num_parts = PARTS, part_length = LENGTH)
23
24
  code = orig.upcase
24
- code.gsub!(/[^#{SYMBOL}]+/, '')
25
- parts = code.scan(/[#{SYMBOL}]{#{LENGTH}}/)
25
+ code.gsub!(/[^#{SYMBOL}]+/o, "")
26
+ parts = code.scan(/[#{SYMBOL}]{#{part_length}}/)
26
27
  return if parts.length != num_parts
27
- parts.each_with_index do |part, i|
28
- data = part[0...(LENGTH - 1)]
29
- check = part[-1]
30
- return if check != checkdigit_alg_1(data, i + 1)
31
- end
32
- parts.join('-')
28
+ return unless parts.each_with_index.all? { |part, i| part[-1] == checkdigit_alg_1(part[0...-1], i + 1) }
29
+ parts.join("-")
33
30
  end
34
31
 
35
32
  def self.checkdigit_alg_1(orig, check)
36
- orig.split('').each_with_index do |c, _|
33
+ orig.chars.each_with_index do |c, _|
37
34
  k = SYMBOL.index(c)
38
35
  check = check * 19 + k
39
36
  end
@@ -41,6 +38,6 @@ module CouponCode
41
38
  end
42
39
 
43
40
  def self.random_symbol
44
- SYMBOL[rand(SYMBOL.length)]
41
+ SYMBOL[SecureRandom.random_number(SYMBOL.length)]
45
42
  end
46
43
  end
metadata CHANGED
@@ -1,57 +1,15 @@
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.1.0
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
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'
11
+ date: 2026-10-22 00:00:00.000000000 Z
12
+ dependencies: []
55
13
  description: A Ruby implementation of Perl's Algorithm::CouponCode CPAN module.
56
14
  email:
57
15
  - sh@baxang.com
@@ -60,27 +18,22 @@ executables:
60
18
  extensions: []
61
19
  extra_rdoc_files: []
62
20
  files:
63
- - ".gitignore"
64
- - ".hound.yml"
65
- - ".rubocop.yml"
66
- - ".travis.yml"
67
21
  - CHANGELOG.md
68
- - Gemfile
69
22
  - LICENSE.txt
70
23
  - README-ko.md
71
24
  - README.md
72
- - Rakefile
73
25
  - bin/coupon-code
74
- - coupon_code.gemspec
75
26
  - lib/coupon_code.rb
76
27
  - lib/coupon_code/version.rb
77
- - spec/coupon_code_spec.rb
78
- - spec/spec_helper.rb
79
28
  homepage: https://github.com/baxang/coupon-code
80
29
  licenses:
81
30
  - MIT
82
- metadata: {}
83
- post_install_message:
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'
36
+ post_install_message:
84
37
  rdoc_options: []
85
38
  require_paths:
86
39
  - lib
@@ -88,18 +41,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
41
  requirements:
89
42
  - - ">="
90
43
  - !ruby/object:Gem::Version
91
- version: 1.9.3
44
+ version: 2.6.0
92
45
  required_rubygems_version: !ruby/object:Gem::Requirement
93
46
  requirements:
94
47
  - - ">="
95
48
  - !ruby/object:Gem::Version
96
49
  version: '0'
97
50
  requirements: []
98
- rubyforge_project:
99
- rubygems_version: 2.4.5
100
- signing_key:
51
+ rubygems_version: 3.5.11
52
+ signing_key:
101
53
  specification_version: 4
102
54
  summary: Generate and validate coupon codes.
103
- test_files:
104
- - spec/coupon_code_spec.rb
105
- - spec/spec_helper.rb
55
+ test_files: []
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/.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
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in coupon_code.gemspec
4
- gemspec
5
-
6
- gem 'codeclimate-test-reporter', group: :test, require: nil
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,27 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'coupon_code/version'
5
-
6
- 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'
16
-
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']
21
-
22
- spec.required_ruby_version = '>= 1.9.3'
23
-
24
- spec.add_development_dependency 'bundler'
25
- spec.add_development_dependency 'rake'
26
- spec.add_development_dependency 'rspec', '~> 3.4'
27
- end
@@ -1,56 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe CouponCode do
4
-
5
- describe '.generate' do
6
- subject { described_class.generate }
7
- it { is_expected.not_to be_nil }
8
- it { is_expected.to match(/^[0-9A-Z-]+$/) }
9
- it { is_expected.to match(/^\w{4}-\w{4}-\w{4}$/) }
10
- it 'generates a different code' do
11
- code2 = described_class.generate
12
- is_expected.not_to eq(code2)
13
- end
14
-
15
- context '2 parts' do
16
- subject { described_class.generate(parts: 2) }
17
- it { is_expected.to match(/^\w{4}-\w{4}$/) }
18
- end
19
- end
20
-
21
- describe '.validate' do
22
-
23
- it 'validates a good code' do
24
- expect(described_class.validate('1K7Q-CTFM-LMTC')).to eq('1K7Q-CTFM-LMTC')
25
- end
26
-
27
- it 'validates and returns the code in uppercase letters' do
28
- expect(described_class.validate('1K7Q-ctfm-LMTC')).to eq('1K7Q-CTFM-LMTC')
29
- end
30
-
31
- it 'returns nil for an invalid code' do
32
- expect(described_class.validate('1K7Q-CTFM')).to be_nil
33
- end
34
-
35
- it 'handles invalid characters' do
36
- expect(described_class.validate('OK7Q-CTFM-LMTC')).to be_nil
37
- end
38
-
39
- context 'valid cases: lowercase, different separator and parts' do
40
- [
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]
49
- ].each do |args|
50
- it { expect(described_class.validate(*args)).not_to be_nil }
51
- end
52
- end
53
-
54
- end
55
-
56
- end
data/spec/spec_helper.rb DELETED
@@ -1,97 +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
- =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
97
- end