forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfers.rb
59 lines (49 loc) · 1.98 KB
/
transfers.rb
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module StripeMock
module RequestHandlers
module Transfers
def Transfers.included(klass)
klass.add_handler 'post /v1/transfers', :new_transfer
klass.add_handler 'get /v1/transfers', :get_all_transfers
klass.add_handler 'get /v1/transfers/(.*)', :get_transfer
klass.add_handler 'post /v1/transfers/(.*)/cancel', :cancel_transfer
end
def get_all_transfers(route, method_url, params, headers)
if recipient = params[:recipient]
assert_existence :recipient, recipient, recipients[recipient]
end
_transfers = transfers.each_with_object([]) do |(_, transfer), array|
if recipient
array << transfer if transfer[:recipient] == recipient
else
array << transfer
end
end
if params[:limit]
_transfers = _transfers.first([params[:limit], _transfers.size].min)
end
Data.mock_list_object(_transfers, params)
end
def new_transfer(route, method_url, params, headers)
id = new_id('tr')
if params[:bank_account]
params[:account] = get_bank_by_token(params.delete(:bank_account))
end
unless params[:amount].is_a?(Integer) || (params[:amount].is_a?(String) && /^\d+$/.match(params[:amount]))
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', 400)
end
transfers[id] = Data.mock_transfer(params.merge :id => id)
end
def get_transfer(route, method_url, params, headers)
route =~ method_url
assert_existence :transfer, $1, transfers[$1]
transfers[$1] ||= Data.mock_transfer(:id => $1)
end
def cancel_transfer(route, method_url, params, headers)
route =~ method_url
assert_existence :transfer, $1, transfers[$1]
t = transfers[$1] ||= Data.mock_transfer(:id => $1)
t.merge!({:status => "canceled"})
end
end
end
end