cdigits 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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rubocop.yml +19 -0
- data/.travis.yml +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +100 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/cdigits.gemspec +33 -0
- data/lib/cdigits.rb +6 -0
- data/lib/cdigits/luhn.rb +125 -0
- data/lib/cdigits/luhn/placeholder.rb +74 -0
- data/lib/cdigits/luhn/store.rb +94 -0
- data/lib/cdigits/version.rb +5 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4b3c39df5d34d7bdf6e0bc8ea05b9c073c404c8ec8e20b30ca1a4a7ac9d64fa9
|
4
|
+
data.tar.gz: de5313acad4b2d515b166d189df0fae8892f1b67156aa5b80bfdfb51a589e5d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f27e1abf203030d5b78ffbc06bf866ed5ce2f9bc1ddab6dfa9c1886b01a15c75f5dec76a96bdca8e795c0099a190cf1490a4e3f093a31162b3055f917d0352a4
|
7
|
+
data.tar.gz: c0234a910421f1e83be33d193c15de34ff012625bd783e3cccbc4d2abd09c907536a6e7c64aa366c46893051289f8693e0693b9f801e077fb789f0ce8686c11c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# The behavior of RuboCop can be controlled via the .rubocop.yml
|
2
|
+
# configuration file. It makes it possible to enable/disable
|
3
|
+
# certain cops (checks) and to alter their behavior if they accept
|
4
|
+
# any parameters. The file can be placed either in your home
|
5
|
+
# directory or in some project directory.
|
6
|
+
#
|
7
|
+
# RuboCop will start looking for the configuration file in the directory
|
8
|
+
# where the inspected file is and continue its way up to the root directory.
|
9
|
+
#
|
10
|
+
# See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
|
11
|
+
Layout/LineLength:
|
12
|
+
Max: 120
|
13
|
+
Style/Documentation:
|
14
|
+
Enabled: false
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Exclude:
|
17
|
+
- 'spec/**/*'
|
18
|
+
Style/AsciiComments:
|
19
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at kengo@kengos.jp. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 kengos
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Cdigits
|
2
|
+
|
3
|
+
[](https://travis-ci.org/kengos/cdigits)
|
4
|
+
|
5
|
+
Cdigits implements Luhn N algorithm.
|
6
|
+
|
7
|
+
usage | N | valid characters | example |
|
8
|
+
------------------------- | -- | ---------------- | ------- |
|
9
|
+
Cdigit::Luhn.number | 10 | 0 to 9 | 8217161655 |
|
10
|
+
Cdigit::Luhn.hex | 16 | 0 to 9 and a to f | e780dcc9c9 |
|
11
|
+
Cdigit::Luhn.alphanumeric | 36 | 0 to 9 and a to z | 6lgoybfzvr |
|
12
|
+
Cdigit::Luhn.easy | 30 | 0 to 9 and A to Z without D / I / M / O / Q / Z | U1B3J0SCG8
|
13
|
+
|
14
|
+
Also, you can generate code in any format using placeholder.
|
15
|
+
|
16
|
+
example:
|
17
|
+
|
18
|
+
```rb
|
19
|
+
Cdigits::Luhn.number 'CA##-####-####-###?' # => "CA66-6567-2324-6526"
|
20
|
+
Cdigits::Luhn.number '2020-01##-####-###?' # => "2020-0160-1171-1643"
|
21
|
+
```
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'cdigits'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle install
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install cdigits
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
### Generate code
|
42
|
+
|
43
|
+
```rb
|
44
|
+
placeholder = 'CA##-####-####-###?'
|
45
|
+
Cdigits::Luhn.number
|
46
|
+
# => "6907562414"
|
47
|
+
Cdigits::Luhn.number placeholder
|
48
|
+
# => "CA98-4890-6337-4381"
|
49
|
+
Cdigits::Luhn.hex
|
50
|
+
# => "2c14a42508"
|
51
|
+
Cdigits::Luhn.hex placeholder
|
52
|
+
# => "CA27-675e-7136-5fa1"
|
53
|
+
Cdigits::Luhn.alphanumeric
|
54
|
+
# => "yrkxeh4eie"
|
55
|
+
Cdigits::Luhn.alphanumeric placeholder
|
56
|
+
# => "CA59-bdxv-wjei-gdc9"
|
57
|
+
Cdigits::Luhn.easy
|
58
|
+
# => "16TPF8RETL"
|
59
|
+
Cdigits::Luhn.easy placeholder
|
60
|
+
# => "CABW-LF40-G11S-TL3U"
|
61
|
+
```
|
62
|
+
|
63
|
+
#### Special chars in placeholder
|
64
|
+
|
65
|
+
- `+` ... non-zero random number (1 to modulus)
|
66
|
+
- `#` ... random number (0 to modulus)
|
67
|
+
- `?` ... check digit
|
68
|
+
|
69
|
+
### Validate code
|
70
|
+
|
71
|
+
```rb
|
72
|
+
Cdigits::Luhn.number? '6907562414'
|
73
|
+
# => true
|
74
|
+
Cdigits::Luhn.hex? '2c14a42508'
|
75
|
+
# => true
|
76
|
+
Cdigits::Luhn.alphanumeric? 'yrkxeh4eie'
|
77
|
+
# => true
|
78
|
+
Cdigits::Luhn.easy? '16TPF8RETL'
|
79
|
+
# => true
|
80
|
+
```
|
81
|
+
|
82
|
+
|
83
|
+
## Development
|
84
|
+
|
85
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
86
|
+
|
87
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
88
|
+
|
89
|
+
## Contributing
|
90
|
+
|
91
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kengos/cdigits. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/kengos/cdigits/blob/master/CODE_OF_CONDUCT.md).
|
92
|
+
|
93
|
+
|
94
|
+
## License
|
95
|
+
|
96
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
97
|
+
|
98
|
+
## Code of Conduct
|
99
|
+
|
100
|
+
Everyone interacting in the Cdigits project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kengos/cdigits/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'cdigits'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/cdigits.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/cdigits/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'cdigits'
|
7
|
+
spec.version = Cdigits::VERSION
|
8
|
+
spec.authors = ['kengos']
|
9
|
+
spec.email = ['kengo@kengos.jp']
|
10
|
+
|
11
|
+
spec.summary = 'Check digits calculator'
|
12
|
+
spec.description = 'Check digits calculator'
|
13
|
+
spec.homepage = 'https://github.com/kengos/cdigits'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
|
29
|
+
spec.add_development_dependency 'rake', '>= 12.0'
|
30
|
+
spec.add_development_dependency 'rspec', '>= 3.0'
|
31
|
+
spec.add_development_dependency 'rubocop'
|
32
|
+
spec.add_development_dependency 'yard'
|
33
|
+
end
|
data/lib/cdigits.rb
ADDED
data/lib/cdigits/luhn.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cdigits/luhn/placeholder'
|
4
|
+
|
5
|
+
module Cdigits
|
6
|
+
# Implementation of Luhn mod N algorithm
|
7
|
+
module Luhn
|
8
|
+
module_function
|
9
|
+
|
10
|
+
# 0 to 9 string array
|
11
|
+
NUMBER_CHARACTERS = (0..9).map(&:to_s).freeze
|
12
|
+
|
13
|
+
# Generate code with Luhn mod 10 algorithm
|
14
|
+
# @example
|
15
|
+
# Cdigits::Luhn.number # => '123456782'
|
16
|
+
# @example
|
17
|
+
# Cdigits::Luhn.number('2###-0###-0###-1##?') # => 2960-0093-0751-1449
|
18
|
+
# @return [String]
|
19
|
+
def number(placeholder = nil)
|
20
|
+
generate(placeholder, NUMBER_CHARACTERS)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Validate code with Luhn mod 10 algorithm
|
24
|
+
# @param [String] code
|
25
|
+
# @return [Boolean]
|
26
|
+
def number?(code)
|
27
|
+
valid?(code, NUMBER_CHARACTERS)
|
28
|
+
end
|
29
|
+
|
30
|
+
# 0 to 9 and a to f string array
|
31
|
+
HEX_CHARACTERS = (NUMBER_CHARACTERS + ('a'..'f').to_a).freeze
|
32
|
+
|
33
|
+
# Generate code with Luhn mod 16 algorithm
|
34
|
+
# @example
|
35
|
+
# Cdigits::Luhn.hex # => 'd6fd358a29'
|
36
|
+
# @example
|
37
|
+
# Cdigits::Luhn.hex('2###-0###-0###-1##?') # => '2582-08fe-02fe-1d80'
|
38
|
+
# @return [String]
|
39
|
+
def hex(placeholder = nil)
|
40
|
+
generate(placeholder, HEX_CHARACTERS)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Validate code with Luhn mod 16 algorithm
|
44
|
+
# @param [String] code
|
45
|
+
# @return [Boolean]
|
46
|
+
def hex?(code)
|
47
|
+
valid?(code, HEX_CHARACTERS)
|
48
|
+
end
|
49
|
+
|
50
|
+
# 0 to 9 and a to z string array
|
51
|
+
ALPHANUMERIC_CHARACTERS = (NUMBER_CHARACTERS + ('a'..'z').to_a).freeze
|
52
|
+
|
53
|
+
# Generate code with Luhn mod 36 algorithm
|
54
|
+
# @example
|
55
|
+
# Cdigits::Luhn.alphanumeric # => 'a0gpmk4ye4'
|
56
|
+
# @example
|
57
|
+
# Cdigits::Luhn.alphanumeric('2###-0###-0###-1##?') # => '22u3-04s1f-0z9c-1lmo'
|
58
|
+
# @return [String]
|
59
|
+
def alphanumeric(placeholder = nil)
|
60
|
+
generate(placeholder, ALPHANUMERIC_CHARACTERS)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Validate code with Luhn mod 36 algorithm
|
64
|
+
# @param [String] code
|
65
|
+
# @return [Boolean]
|
66
|
+
def alphanumeric?(code)
|
67
|
+
valid?(code, ALPHANUMERIC_CHARACTERS)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Non(hard to)-Misread/Misheard characters
|
71
|
+
# @note any good idea?
|
72
|
+
# @note Misread charcters
|
73
|
+
# - 0 and O(óu)
|
74
|
+
# - 0 and D(díː)
|
75
|
+
# - 0 and Q(kjúː)
|
76
|
+
# - 1 and I(ái)
|
77
|
+
# - 2 and Z(zíː)
|
78
|
+
# @note Misheard charcters
|
79
|
+
# - D(díː) and B(bíː)
|
80
|
+
# - M(ém) and N(én)
|
81
|
+
# - 9(kyu:) and Q(kjúː) ... Japanese only
|
82
|
+
# @return [Array<String>]
|
83
|
+
EASY_CHARACTERS = (NUMBER_CHARACTERS + ('A'..'Z').to_a - %w[D I M O Q Z]).freeze
|
84
|
+
|
85
|
+
# Generate code with Luhn mod 30 algorithm
|
86
|
+
# Valid characters are 0 to 9 and A to Z without D/I/M/O/Q/Z
|
87
|
+
# @example
|
88
|
+
# Cdigits::Luhn.easy # => '5F20603XER'
|
89
|
+
# @example
|
90
|
+
# Cdigits::Luhn.easy('2###-0###-0###-1##?') # => '2P2M-0191-05XL-1BYN'
|
91
|
+
# @return [String]
|
92
|
+
def easy(placeholder = nil)
|
93
|
+
generate(placeholder, EASY_CHARACTERS)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Validate code with Luhn mod 30 algorithm
|
97
|
+
# @param [String] code
|
98
|
+
# @return [Boolean]
|
99
|
+
def easy?(code)
|
100
|
+
valid?(code, EASY_CHARACTERS)
|
101
|
+
end
|
102
|
+
|
103
|
+
# Generate code
|
104
|
+
# @param [String] placeholder
|
105
|
+
# @param [Array<String>] characters
|
106
|
+
# @return [String]
|
107
|
+
def generate(placeholder, characters)
|
108
|
+
placeholder ||= '+########?'
|
109
|
+
instance(characters).fill(placeholder)
|
110
|
+
end
|
111
|
+
|
112
|
+
# @param [String] code
|
113
|
+
# @param [Array<String>] characters
|
114
|
+
# @return [Boolean]
|
115
|
+
def valid?(code, characters)
|
116
|
+
instance(characters).valid?(code)
|
117
|
+
end
|
118
|
+
|
119
|
+
# @private
|
120
|
+
# @return [Cdigits::Luhn::Placeholder]
|
121
|
+
def instance(characters)
|
122
|
+
::Cdigits::Luhn::Placeholder.new(characters)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cdigits/luhn/store'
|
4
|
+
|
5
|
+
module Cdigits
|
6
|
+
module Luhn
|
7
|
+
class Placeholder
|
8
|
+
NON_ZERO_SYMBOL = '+'
|
9
|
+
NUMERIC_SYMBOL = '#'
|
10
|
+
# @note any good idia?
|
11
|
+
CHECK_DIGIT_SYMBOL = '?'
|
12
|
+
|
13
|
+
# @param [Array<String>] characters
|
14
|
+
def initialize(characters)
|
15
|
+
@characters = characters
|
16
|
+
end
|
17
|
+
|
18
|
+
# Generate code
|
19
|
+
# @param [String] placeholder
|
20
|
+
# @return [String]
|
21
|
+
def fill(placeholder)
|
22
|
+
codes = []
|
23
|
+
store = build_store(placeholder) do |char, digit|
|
24
|
+
codes << (digit.nil? ? char : nil)
|
25
|
+
end
|
26
|
+
|
27
|
+
store.fill_check_digit
|
28
|
+
generate_code(codes, store.digits)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Validate code
|
32
|
+
# @param [String] placeholder
|
33
|
+
# @return [Boolean]
|
34
|
+
def valid?(placeholder)
|
35
|
+
store = build_store(placeholder)
|
36
|
+
(store.sum % modulus).zero?
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def modulus
|
42
|
+
@modulus ||= @characters.size
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_store(placeholder)
|
46
|
+
store = ::Cdigits::Luhn::Store.new(modulus: modulus)
|
47
|
+
placeholder.chars.each do |char|
|
48
|
+
digit = generate_digit(char, store)
|
49
|
+
yield char, digit if block_given?
|
50
|
+
end
|
51
|
+
store
|
52
|
+
end
|
53
|
+
|
54
|
+
def generate_code(chars, digits)
|
55
|
+
chars.map do |char|
|
56
|
+
char || @characters[digits.shift]
|
57
|
+
end.join
|
58
|
+
end
|
59
|
+
|
60
|
+
def generate_digit(char, store)
|
61
|
+
case char
|
62
|
+
when NON_ZERO_SYMBOL
|
63
|
+
store.append_non_zero_number
|
64
|
+
when NUMERIC_SYMBOL
|
65
|
+
store.append_number
|
66
|
+
when CHECK_DIGIT_SYMBOL
|
67
|
+
store.initialize_check_digit
|
68
|
+
when *@characters
|
69
|
+
store.append(@characters.index(char))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
module Cdigits
|
6
|
+
module Luhn
|
7
|
+
class Store
|
8
|
+
# @return [Array<Integer>]
|
9
|
+
attr_reader :digits
|
10
|
+
|
11
|
+
# @param [Integer] modulus
|
12
|
+
# @param [Array<Integer>] digits initial digits (default [])
|
13
|
+
def initialize(modulus:, digits: nil)
|
14
|
+
@modulus = modulus
|
15
|
+
@digits = digits || []
|
16
|
+
@position = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
# Set check digit position
|
20
|
+
def initialize_check_digit
|
21
|
+
@position = @digits.size
|
22
|
+
append 0
|
23
|
+
end
|
24
|
+
|
25
|
+
# Calculate check digit and fill its value into the check digit position
|
26
|
+
# @return [Integer] check digit
|
27
|
+
def fill_check_digit
|
28
|
+
return unless @position
|
29
|
+
|
30
|
+
odd = (@digits.size - @position).odd?
|
31
|
+
@digits[@position] = calculate_check_digit(sum, odd)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Append random non-zero number to digits array
|
35
|
+
# @return [Integer] appended value
|
36
|
+
def append_non_zero_number
|
37
|
+
append random_number(@modulus - 1) + 1
|
38
|
+
end
|
39
|
+
|
40
|
+
# Append random number to digits array
|
41
|
+
# @return [Integer] appended value
|
42
|
+
def append_number
|
43
|
+
append random_number(@modulus)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Append passed value to digits array
|
47
|
+
# @param [Integer] value
|
48
|
+
# @return [Integer] appended value
|
49
|
+
def append(value)
|
50
|
+
@digits << value
|
51
|
+
value
|
52
|
+
end
|
53
|
+
|
54
|
+
# sum of digits
|
55
|
+
# @return [Integer]
|
56
|
+
def sum
|
57
|
+
digits.reverse.each_with_index.inject(0) do |sum, (value, i)|
|
58
|
+
value = double(value) if (i + 1).even?
|
59
|
+
sum + value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def random_number(num)
|
66
|
+
::SecureRandom.random_number(num)
|
67
|
+
end
|
68
|
+
|
69
|
+
def calculate_check_digit(value, odd)
|
70
|
+
mod = value % @modulus
|
71
|
+
return 0 if mod.zero?
|
72
|
+
|
73
|
+
check_digit = @modulus - mod
|
74
|
+
return check_digit if odd
|
75
|
+
|
76
|
+
check_digit += @modulus - 1 if check_digit.odd?
|
77
|
+
check_digit / 2
|
78
|
+
end
|
79
|
+
|
80
|
+
# @example
|
81
|
+
# # num = 6, modulus = 10
|
82
|
+
# # (12 / 10) + (12 % 10) = 3
|
83
|
+
# double(6) # => 3
|
84
|
+
# @example
|
85
|
+
# # num = 12, modulus = 16
|
86
|
+
# # (24 / 16) + (24 % 16) = 9
|
87
|
+
# double(12) # => 9
|
88
|
+
def double(num)
|
89
|
+
num *= 2
|
90
|
+
(num / @modulus).to_i + (num % @modulus)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cdigits
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kengos
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '12.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '12.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
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'
|
69
|
+
description: Check digits calculator
|
70
|
+
email:
|
71
|
+
- kengo@kengos.jp
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- cdigits.gemspec
|
88
|
+
- lib/cdigits.rb
|
89
|
+
- lib/cdigits/luhn.rb
|
90
|
+
- lib/cdigits/luhn/placeholder.rb
|
91
|
+
- lib/cdigits/luhn/store.rb
|
92
|
+
- lib/cdigits/version.rb
|
93
|
+
homepage: https://github.com/kengos/cdigits
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
homepage_uri: https://github.com/kengos/cdigits
|
98
|
+
source_code_uri: https://github.com/kengos/cdigits
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: 2.3.0
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubygems_version: 3.1.2
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Check digits calculator
|
118
|
+
test_files: []
|