module Cdigits::Luhn

Implementation of Luhn mod N algorithm

Constants

ALPHANUMERIC_CHARACTERS

0 to 9 and a to z string array

EASY_CHARACTERS

Non(hard to)-Misread/Misheard 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

@return [Array<String>]

HEX_CHARACTERS

0 to 9 and a to f string array

NUMBER_CHARACTERS

0 to 9 string array

Public Instance Methods

alphanumeric(placeholder = nil) click to toggle source

Generate code with Luhn mod 36 algorithm @example

Cdigits::Luhn.alphanumeric # => 'a0gpmk4ye4'

@example

Cdigits::Luhn.alphanumeric('2###-0###-0###-1##?') # => '22u3-04s1f-0z9c-1lmo'

@return [String]

# File lib/cdigits/luhn.rb, line 59
def alphanumeric(placeholder = nil)
  generate(placeholder, ALPHANUMERIC_CHARACTERS)
end
alphanumeric?(code) click to toggle source

Validate code with Luhn mod 36 algorithm @param [String] code @return [Boolean]

# File lib/cdigits/luhn.rb, line 66
def alphanumeric?(code)
  valid?(code, ALPHANUMERIC_CHARACTERS)
end
easy(placeholder = nil) click to toggle source

Generate code with Luhn mod 30 algorithm Valid characters are 0 to 9 and A to Z without D/I/M/O/Q/Z @example

Cdigits::Luhn.easy # => '5F20603XER'

@example

Cdigits::Luhn.easy('2###-0###-0###-1##?') # => '2P2M-0191-05XL-1BYN'

@return [String]

# File lib/cdigits/luhn.rb, line 92
def easy(placeholder = nil)
  generate(placeholder, EASY_CHARACTERS)
end
easy?(code) click to toggle source

Validate code with Luhn mod 30 algorithm @param [String] code @return [Boolean]

# File lib/cdigits/luhn.rb, line 99
def easy?(code)
  valid?(code, EASY_CHARACTERS)
end
generate(placeholder, characters) click to toggle source

Generate code @param [String] placeholder @param [Array<String>] characters @return [String]

# File lib/cdigits/luhn.rb, line 107
def generate(placeholder, characters)
  placeholder ||= '+########?'
  instance(characters).fill(placeholder)
end
hex(placeholder = nil) click to toggle source

Generate code with Luhn mod 16 algorithm @example

Cdigits::Luhn.hex # => 'd6fd358a29'

@example

Cdigits::Luhn.hex('2###-0###-0###-1##?') # => '2582-08fe-02fe-1d80'

@return [String]

# File lib/cdigits/luhn.rb, line 39
def hex(placeholder = nil)
  generate(placeholder, HEX_CHARACTERS)
end
hex?(code) click to toggle source

Validate code with Luhn mod 16 algorithm @param [String] code @return [Boolean]

# File lib/cdigits/luhn.rb, line 46
def hex?(code)
  valid?(code, HEX_CHARACTERS)
end
instance(characters) click to toggle source

@private @return [Cdigits::Luhn::Placeholder]

# File lib/cdigits/luhn.rb, line 121
def instance(characters)
  ::Cdigits::Luhn::Placeholder.new(characters)
end
number(placeholder = nil) click to toggle source

Generate code with Luhn mod 10 algorithm @example

Cdigits::Luhn.number # => '123456782'

@example

Cdigits::Luhn.number('2###-0###-0###-1##?') # => 2960-0093-0751-1449

@return [String]

# File lib/cdigits/luhn.rb, line 19
def number(placeholder = nil)
  generate(placeholder, NUMBER_CHARACTERS)
end
number?(code) click to toggle source

Validate code with Luhn mod 10 algorithm @param [String] code @return [Boolean]

# File lib/cdigits/luhn.rb, line 26
def number?(code)
  valid?(code, NUMBER_CHARACTERS)
end
valid?(code, characters) click to toggle source

@param [String] code @param [Array<String>] characters @return [Boolean]

# File lib/cdigits/luhn.rb, line 115
def valid?(code, characters)
  instance(characters).valid?(code)
end