Class: CCheck::Card

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_number) ⇒ Card

Cleans the given raw CC number.

raw_number - CC raw String



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

def initialize raw_number
  @number = cleanup(raw_number)
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

Instance Method Details

#to_sObject

Public: Formats CC information as such: TYPE: NUMBERS (VALIDITY)

Returns CC information as a String



31
32
33
34
35
36
# File 'lib/ccheck/card.rb', line 31

def to_s
  "%-28s (%s)\n" % [
    "#{type}: #{number}",
    Luhn.new(number).valid? ? 'valid' : 'invalid'
  ]
end

#typeObject

Public: Determines CC type/brand.

Returns CC type as a String or “Unknown” if not found.



17
18
19
20
21
22
23
24
25
# File 'lib/ccheck/card.rb', line 17

def type
  case number
  when /^3[47]\d{13}$/          ; return "AMEX"
  when /^4(\d{15}|\d{12})$/     ; return "VISA"
  when /^5\d{15}|36\d{14}$/     ; return "MasterCard"
  when /^6011\d{12}|650\d{13}$/ ; return "Discover"
  else                            return "Unknown"
  end
end