Class: CCheck::Luhn

Inherits:
Object
  • Object
show all
Defined in:
lib/ccheck/luhn.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card) ⇒ Luhn

Sets @card and also builds its luhn @digits.

card - CC String



10
11
12
13
# File 'lib/ccheck/luhn.rb', line 10

def initialize card
  @card   = card
  @digits = luhn_digits
end

Instance Attribute Details

#cardObject (readonly)

Returns the value of attribute card.



4
5
6
# File 'lib/ccheck/luhn.rb', line 4

def card
  @card
end

#digitsObject (readonly)

Returns the value of attribute digits.



4
5
6
# File 'lib/ccheck/luhn.rb', line 4

def digits
  @digits
end

Instance Method Details

#valid?Boolean

Public: Verifies that the given number is a valid CC. Basically the third step of the Luhn algorighthm. If the result is a multiple of 10, the number is valid.

Returns Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/ccheck/luhn.rb', line 20

def valid?
  digits.split('').inject(0) do |sum, digit|
    sum + digit.to_i
  end % 10 == 0
end