Module: Cdigits::Luhn
- Defined in:
- lib/cdigits/luhn.rb,
lib/cdigits/luhn/store.rb,
lib/cdigits/luhn/placeholder.rb,
lib/cdigits/luhn/random_table.rb
Overview
Implementation of Luhn mod N algorithm
Defined Under Namespace
Classes: Placeholder, RandomTable, Store
Constant Summary collapse
- NUMBER_CHARACTERS =
0 to 9 string array
(0..9).map(&:to_s).freeze
- HEX_CHARACTERS =
0 to 9 and a to f string array
(NUMBER_CHARACTERS + ('a'..'f').to_a).freeze
- ALPHANUMERIC_CHARACTERS =
0 to 9 and a to z string array
(NUMBER_CHARACTERS + ('a'..'z').to_a).freeze
- EASY_CHARACTERS =
Note:
any good idea?
Note:Misread charcters
-
0 and O(óu)
-
0 and D(díː)
-
0 and Q(kjúː)
-
1 and I(ái)
-
2 and Z(zíː)
Note:Misheard charcters
-
D(díː) and B(bíː)
-
M(ém) and N(én)
-
9(kyu:) and Q(kjúː) … Japanese only
Non(hard to)-Misread/Misheard characters
-
(NUMBER_CHARACTERS + ('A'..'Z').to_a - %w[D I M O Q Z]).freeze
Class Method Summary collapse
-
.alphanumeric(placeholder = nil) ⇒ String
Generate code with Luhn mod 36 algorithm.
-
.alphanumeric?(code) ⇒ Boolean
Validate code with Luhn mod 36 algorithm.
-
.easy(placeholder = nil) ⇒ String
Generate code with Luhn mod 30 algorithm Valid characters are 0 to 9 and A to Z without D/I/M/O/Q/Z.
-
.easy?(code) ⇒ Boolean
Validate code with Luhn mod 30 algorithm.
-
.generate(placeholder, characters) ⇒ String
Generate code.
-
.hex(placeholder = nil) ⇒ String
Generate code with Luhn mod 16 algorithm.
-
.hex?(code) ⇒ Boolean
Validate code with Luhn mod 16 algorithm.
- .instance(characters) ⇒ Cdigits::Luhn::Placeholder
-
.number(placeholder = nil) ⇒ String
Generate code with Luhn mod 10 algorithm.
-
.number?(code) ⇒ Boolean
Validate code with Luhn mod 10 algorithm.
- .valid?(code, characters) ⇒ Boolean
Class Method Details
.alphanumeric(placeholder = nil) ⇒ String
Generate code with Luhn mod 36 algorithm
59 60 61 |
# File 'lib/cdigits/luhn.rb', line 59 def alphanumeric(placeholder = nil) generate(placeholder, ALPHANUMERIC_CHARACTERS) end |
.alphanumeric?(code) ⇒ Boolean
Validate code with Luhn mod 36 algorithm
66 67 68 |
# File 'lib/cdigits/luhn.rb', line 66 def alphanumeric?(code) valid?(code, ALPHANUMERIC_CHARACTERS) end |
.easy(placeholder = nil) ⇒ String
Generate code with Luhn mod 30 algorithm Valid characters are 0 to 9 and A to Z without D/I/M/O/Q/Z
92 93 94 |
# File 'lib/cdigits/luhn.rb', line 92 def easy(placeholder = nil) generate(placeholder, EASY_CHARACTERS) end |
.easy?(code) ⇒ Boolean
Validate code with Luhn mod 30 algorithm
99 100 101 |
# File 'lib/cdigits/luhn.rb', line 99 def easy?(code) valid?(code, EASY_CHARACTERS) end |
.generate(placeholder, characters) ⇒ String
Generate code
107 108 109 110 |
# File 'lib/cdigits/luhn.rb', line 107 def generate(placeholder, characters) placeholder ||= '+########?' instance(characters).fill(placeholder) end |
.hex(placeholder = nil) ⇒ String
Generate code with Luhn mod 16 algorithm
39 40 41 |
# File 'lib/cdigits/luhn.rb', line 39 def hex(placeholder = nil) generate(placeholder, HEX_CHARACTERS) end |
.hex?(code) ⇒ Boolean
Validate code with Luhn mod 16 algorithm
46 47 48 |
# File 'lib/cdigits/luhn.rb', line 46 def hex?(code) valid?(code, HEX_CHARACTERS) end |
.instance(characters) ⇒ Cdigits::Luhn::Placeholder
121 122 123 |
# File 'lib/cdigits/luhn.rb', line 121 def instance(characters) ::Cdigits::Luhn::Placeholder.new(characters) end |
.number(placeholder = nil) ⇒ String
Generate code with Luhn mod 10 algorithm
19 20 21 |
# File 'lib/cdigits/luhn.rb', line 19 def number(placeholder = nil) generate(placeholder, NUMBER_CHARACTERS) end |
.number?(code) ⇒ Boolean
Validate code with Luhn mod 10 algorithm
26 27 28 |
# File 'lib/cdigits/luhn.rb', line 26 def number?(code) valid?(code, NUMBER_CHARACTERS) end |
.valid?(code, characters) ⇒ Boolean
115 116 117 |
# File 'lib/cdigits/luhn.rb', line 115 def valid?(code, characters) instance(characters).valid?(code) end |