Class: Aceitafacil::Card

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/aceitafacil/card.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Card

Returns a new instance of Card.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aceitafacil/card.rb', line 56

def initialize(params = {})
    @connection = Aceitafacil::Connection.new

    self.customer_id = params[:customer_id]
    self.number = params[:number]
    self.name = params[:name]
    self.exp_date = params[:exp_date]
    self.status = params[:status]
    self.token = params[:token]
    self.last_digits = params[:last_digits]
    self.card_brand = params[:card_brand]
end

Instance Attribute Details

#card_brandObject

Returns the value of attribute card_brand.



12
13
14
# File 'lib/aceitafacil/card.rb', line 12

def card_brand
  @card_brand
end

#customer_idObject

Returns the value of attribute customer_id.



11
12
13
# File 'lib/aceitafacil/card.rb', line 11

def customer_id
  @customer_id
end

#exp_dateObject

Returns the value of attribute exp_date.



11
12
13
# File 'lib/aceitafacil/card.rb', line 11

def exp_date
  @exp_date
end

#last_digitsObject

Returns the value of attribute last_digits.



12
13
14
# File 'lib/aceitafacil/card.rb', line 12

def last_digits
  @last_digits
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/aceitafacil/card.rb', line 11

def name
  @name
end

#numberObject

Returns the value of attribute number.



11
12
13
# File 'lib/aceitafacil/card.rb', line 11

def number
  @number
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/aceitafacil/card.rb', line 12

def status
  @status
end

#tokenObject

Returns the value of attribute token.



12
13
14
# File 'lib/aceitafacil/card.rb', line 12

def token
  @token
end

Class Method Details

.find_by_customer_id(customer_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aceitafacil/card.rb', line 14

def self.find_by_customer_id(customer_id)
    return nil if customer_id.nil?

    @connection = Aceitafacil::Connection.new

    cards = []

    connection_params = {}
    connection_params["customer[id]"] = customer_id
    response = @connection.get("card", connection_params)

    json = JSON.parse(response.body)
    
    return [] if json["card"].nil?

    json["card"].each do |remote_card|
        card = Card.new(token: remote_card["token"], card_brand: remote_card["card_brand"], last_digits: remote_card["last_digits"])
        cards << card
    end

    return cards
end

.remove(card) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aceitafacil/card.rb', line 37

def self.remove(card)
    return nil  if card.nil?

    delete_params = {}

    delete_params["customer[id]"] = card.customer_id
    delete_params["card[token]"] = card.token

    @connection = Aceitafacil::Connection.new

    response = @connection.delete("card", delete_params)

    json = JSON.parse(response.body)

    card = Card.new(token: json["card"][0]["token"], status: json["card"][0]["status"])

    return card
end

Instance Method Details

#paramsObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/aceitafacil/card.rb', line 69

def params
    params = {}

    params["customer[id]"] = self.customer_id
    params["card[name]"] = self.name
    params["card[number]"] = self.number
    params["card[exp_date]"] = self.exp_date

    return params
end

#saveObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aceitafacil/card.rb', line 80

def save
    return false if not self.valid?

    response = @connection.post("card", params)

    json = JSON.parse(response.body)
    
    self.token = json["card"][0]["token"]
    self.card_brand = json["card"][0]["card_brand"]
    self.last_digits = json["card"][0]["last_digits"]

    return response
end