Module: StripeMock::RequestHandlers::Cards

Included in:
Instance
Defined in:
lib/stripe_mock/request_handlers/cards.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/stripe_mock/request_handlers/cards.rb', line 5

def Cards.included(klass)
  klass.add_handler 'get /v1/customers/(.*)/cards', :retrieve_cards
  klass.add_handler 'post /v1/customers/(.*)/cards', :create_card
  klass.add_handler 'get /v1/customers/(.*)/cards/(.*)', :retrieve_card
  klass.add_handler 'delete /v1/customers/(.*)/cards/(.*)', :delete_card
  klass.add_handler 'post /v1/customers/(.*)/cards/(.*)', :update_card
  klass.add_handler 'get /v1/recipients/(.*)/cards/(.*)', :retrieve_recipient_card
end

Instance Method Details

#create_card(route, method_url, params, headers) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/stripe_mock/request_handlers/cards.rb', line 14

def create_card(route, method_url, params, headers)
  route =~ method_url
  customer = assert_existance :customer, $1, customers[$1]

  card = card_from_params(params[:card])
  add_card_to_object(:customer, card, customer)
end

#delete_card(route, method_url, params, headers) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/stripe_mock/request_handlers/cards.rb', line 45

def delete_card(route, method_url, params, headers)
  route =~ method_url
  customer = assert_existance :customer, $1, customers[$1]

  assert_existance :card, $2, get_card(customer, $2)

  card = { id: $2, deleted: true }
  customer[:cards][:data].reject!{|cc|
    cc[:id] == card[:id]
  }
  customer[:default_card] = customer[:cards][:data].count > 0 ? customer[:cards][:data].first[:id] : nil
  card
end

#retrieve_card(route, method_url, params, headers) ⇒ Object



31
32
33
34
35
36
# File 'lib/stripe_mock/request_handlers/cards.rb', line 31

def retrieve_card(route, method_url, params, headers)
  route =~ method_url
  customer = assert_existance :customer, $1, customers[$1]

  assert_existance :card, $2, get_card(customer, $2)
end

#retrieve_cards(route, method_url, params, headers) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/stripe_mock/request_handlers/cards.rb', line 22

def retrieve_cards(route, method_url, params, headers)
  route =~ method_url
  customer = assert_existance :customer, $1, customers[$1]

  cards = customer[:cards]
  cards[:count] = cards[:data].length
  cards
end

#retrieve_recipient_card(route, method_url, params, headers) ⇒ Object



38
39
40
41
42
43
# File 'lib/stripe_mock/request_handlers/cards.rb', line 38

def retrieve_recipient_card(route, method_url, params, headers)
  route =~ method_url
  recipient = assert_existance :recipient, $1, recipients[$1]

  assert_existance :card, $2, get_card(recipient, $2, "Recipient")
end

#update_card(route, method_url, params, headers) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/stripe_mock/request_handlers/cards.rb', line 59

def update_card(route, method_url, params, headers)
  route =~ method_url
  customer = assert_existance :customer, $1, customers[$1]

  card = assert_existance :card, $2, get_card(customer, $2)
  card.merge!(params)
  card
end