Module: StripeMock::RequestHandlers::Recipients

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
# File 'lib/stripe_mock/request_handlers/recipients.rb', line 5

def Recipients.included(klass)
  klass.add_handler 'post /v1/recipients',            :new_recipient
  klass.add_handler 'post /v1/recipients/(.*)',       :update_recipient
  klass.add_handler 'get /v1/recipients/(.*)',        :get_recipient
end

Instance Method Details

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



54
55
56
57
# File 'lib/stripe_mock/request_handlers/recipients.rb', line 54

def get_recipient(route, method_url, params, headers)
  route =~ method_url
  assert_existence :recipient, $1, recipients[$1]
end

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stripe_mock/request_handlers/recipients.rb', line 11

def new_recipient(route, method_url, params, headers)
  params[:id] ||= new_id('rp')
  cards = []

  if params[:name].nil?
    raise StripeMock::StripeMockError.new("Missing required parameter name for recipients.")
  end

  if params[:type].nil?
    raise StripeMock::StripeMockError.new("Missing required parameter type for recipients.")
  end

  unless %w(individual corporation).include?(params[:type])
    raise StripeMock::StripeMockError.new("Type must be either individual or corporation..")
  end

  if params[:bank_account]
    params[:active_account] = get_bank_by_token(params.delete(:bank_account))
  end

  if params[:card]
    cards << get_card_by_token(params.delete(:card))
    params[:default_card] = cards.first[:id]
  end

  recipients[ params[:id] ] = Data.mock_recipient(cards, params)
  recipients[ params[:id] ]
end

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



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stripe_mock/request_handlers/recipients.rb', line 40

def update_recipient(route, method_url, params, headers)
  route =~ method_url
  recipient = assert_existence :recipient, $1, recipients[$1]
  recipient.merge!(params)

  if params[:card]
    new_card = get_card_by_token(params.delete(:card))
    add_card_to_object(:recipient, new_card, recipient, true)
    recipient[:default_card] = new_card[:id]
  end

  recipient
end