Class: ConektaMotion::Card
- Inherits:
-
Object
- Object
- ConektaMotion::Card
- Defined in:
- lib/conekta-motion/card.rb
Instance Attribute Summary collapse
-
#cvc ⇒ Object
Returns the value of attribute cvc.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#expiration_month ⇒ Object
Returns the value of attribute expiration_month.
-
#expiration_year ⇒ Object
Returns the value of attribute expiration_year.
-
#name ⇒ Object
Returns the value of attribute name.
-
#number ⇒ Object
Returns the value of attribute number.
Instance Method Summary collapse
- #brand ⇒ Object
-
#initialize(number = nil, name = nil, cvc = nil, expiration_month = nil, expiration_year = nil) ⇒ Card
constructor
A new instance of Card.
- #last_four ⇒ Object
- #to_hash ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(number = nil, name = nil, cvc = nil, expiration_month = nil, expiration_year = nil) ⇒ Card
Returns a new instance of Card.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/conekta-motion/card.rb', line 7 def initialize(number = nil, name = nil, cvc = nil, expiration_month = nil, expiration_year = nil) self.number = number self.name = name self.cvc = cvc self.expiration_month = expiration_month self.expiration_year = expiration_year @errors = {} end |
Instance Attribute Details
#cvc ⇒ Object
Returns the value of attribute cvc.
3 4 5 |
# File 'lib/conekta-motion/card.rb', line 3 def cvc @cvc end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
5 6 7 |
# File 'lib/conekta-motion/card.rb', line 5 def errors @errors end |
#expiration_month ⇒ Object
Returns the value of attribute expiration_month.
3 4 5 |
# File 'lib/conekta-motion/card.rb', line 3 def expiration_month @expiration_month end |
#expiration_year ⇒ Object
Returns the value of attribute expiration_year.
3 4 5 |
# File 'lib/conekta-motion/card.rb', line 3 def expiration_year @expiration_year end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/conekta-motion/card.rb', line 3 def name @name end |
#number ⇒ Object
Returns the value of attribute number.
3 4 5 |
# File 'lib/conekta-motion/card.rb', line 3 def number @number end |
Instance Method Details
#brand ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/conekta-motion/card.rb', line 50 def brand _number = clean_number return :dinners if _number.length == 14 && _number =~ /^3(0[0-5]|[68])/ # 300xxx-305xxx, 36xxxx, 38xxxx return :amex if _number.length == 15 && _number =~ /^3[47]/ # 34xxxx, 37xxxx return :visa if [13,16].include?(_number.length) && _number =~ /^4/ # 4xxxxx return :master if _number.length == 16 && _number =~ /^5[1-5]/ # 51xxxx-55xxxx return :discover if _number.length == 16 && _number =~ /^6011/ # 6011xx nil end |
#last_four ⇒ Object
30 31 32 |
# File 'lib/conekta-motion/card.rb', line 30 def last_four clean_number.reverse[0..3].reverse end |
#to_hash ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/conekta-motion/card.rb', line 18 def to_hash { card: { name: name.to_s, number: clean_number.to_s, cvc: cvc.to_s, exp_month: expiration_month.to_s, exp_year: expiration_year.to_s } } end |
#valid? ⇒ Boolean
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/conekta-motion/card.rb', line 34 def valid? @errors = {} @errors[:number] = 'Número de tarjeta inválida' if brand.nil? || !valid_card? @errors[:name] = 'Nombre de tarjetahabiente inválido' if name.nil? || name.empty? @errors[:cvc] = 'Código de seguridad inválido' if !valid_cvc? @errors[:expiration_month] = 'Mes de expiración inválido' if !valid_expiration_month? @errors[:expiration_year] = 'Año de expiración inválido' if !valid_expiration_year? @errors.keys.count == 0 end |