Class: CreditCardValidator::Validator
- Inherits:
-
Object
- Object
- CreditCardValidator::Validator
- Defined in:
- lib/credit_card_validator/validator.rb
Class Method Summary collapse
- .card_type(number) ⇒ Object
- .is_allowed_card_type?(number) ⇒ Boolean
- .is_test_number(number) ⇒ Object
- .options ⇒ Object
- .valid?(number) ⇒ Boolean
- .verify_luhn(number) ⇒ Object
Class Method Details
.card_type(number) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/credit_card_validator/validator.rb', line 45 def card_type(number) CARD_TYPES.keys.each do |t| return t.to_s if card_is(t, number) end nil end |
.is_allowed_card_type?(number) ⇒ Boolean
52 53 54 55 56 57 58 59 60 |
# File 'lib/credit_card_validator/validator.rb', line 52 def is_allowed_card_type?(number) card_type = card_type(number) return nil if card_type.nil? if [:allowed_card_types] and [:allowed_card_types].respond_to?('include?') [:allowed_card_types].include?(card_type.to_sym) else !card_type.nil? end end |
.is_test_number(number) ⇒ Object
62 63 64 |
# File 'lib/credit_card_validator/validator.rb', line 62 def is_test_number(number) TEST_NUMBERS.include?(strip(number)) end |
.options ⇒ Object
41 42 43 |
# File 'lib/credit_card_validator/validator.rb', line 41 def @@options ||= {} end |
.valid?(number) ⇒ Boolean
34 35 36 37 38 39 |
# File 'lib/credit_card_validator/validator.rb', line 34 def valid?(number) is_allowed_card_type?(number) and verify_luhn(number) and card_type(number) and ([:test_numbers_are_valid] ? true : !is_test_number(number)) end |
.verify_luhn(number) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/credit_card_validator/validator.rb', line 66 def verify_luhn(number) total = strip(number).reverse().split(//).inject([0,0]) do |accum,n| n = n.to_i accum[0] += (accum[1] % 2 == 1 ? rotate(n * 2) : n) accum[1] += 1 accum end (total[0] % 10 == 0) end |