Class: CCheck::Card
- Inherits:
-
Object
- Object
- CCheck::Card
- Defined in:
- lib/ccheck/card.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Instance Method Summary collapse
-
#initialize(raw_number) ⇒ Card
constructor
Cleans the given raw CC number.
-
#to_s ⇒ Object
Public: Formats CC information as such: TYPE: NUMBERS (VALIDITY).
-
#type ⇒ Object
Public: Determines CC type/brand.
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
#number ⇒ Object (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_s ⇒ Object
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 |
#type ⇒ Object
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 |