Class: ConektaMotion::Card

Inherits:
Object
  • Object
show all
Defined in:
lib/conekta-motion/card.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cvcObject

Returns the value of attribute cvc.



3
4
5
# File 'lib/conekta-motion/card.rb', line 3

def cvc
  @cvc
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/conekta-motion/card.rb', line 5

def errors
  @errors
end

#expiration_monthObject

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_yearObject

Returns the value of attribute expiration_year.



3
4
5
# File 'lib/conekta-motion/card.rb', line 3

def expiration_year
  @expiration_year
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/conekta-motion/card.rb', line 3

def name
  @name
end

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/conekta-motion/card.rb', line 3

def number
  @number
end

Instance Method Details

#brandObject



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_fourObject



30
31
32
# File 'lib/conekta-motion/card.rb', line 30

def last_four
  clean_number.reverse[0..3].reverse
end

#to_hashObject



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

Returns:

  • (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