-
Notifications
You must be signed in to change notification settings - Fork 39
/
http_calls.rb
85 lines (74 loc) · 2.99 KB
/
http_calls.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module MangoPay
module HTTPCalls
module Create
module ClassMethods
def create(params, id = nil, idempotency_key = nil)
# LEGACY SUPPORT FOR OLD SIGNATURE: def create(*id, params)
if !params.is_a?(Hash) && id.is_a?(Hash)
temp = params
params = id
id = temp
end
MangoPay.request(:post, url(id), params, {}, idempotency_key)
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
module Update
module ClassMethods
def update(id = nil, params = {}, idempotency_key = nil)
MangoPay.request(:put, url(id), params, {}, idempotency_key)
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
module Fetch
module ClassMethods
# - Fetching _single_entity_ by +id+:
#
# MangoPay::User.fetch("user-id") # => {"FirstName"=>"Mango", "LastName"=>"Pay", ...}
#
# - or fetching _multiple_entities_ with _optional_ +filters+ hash,
# including _pagination_ and _sorting_ params
# +page+, +per_page+, +sort+ (see http://docs.mangopay.com/api-references/pagination/):
#
# MangoPay::User.fetch() # => [{...}, ...]: list of user data hashes (10 per page by default)
# MangoPay::User.fetch({'page' => 2, 'per_page' => 3}) # => list of 3 hashes from 2nd page
# MangoPay::BankAccount.fetch(user_id, {'sort' => 'CreationDate:desc'}) # => bank accounts by user, sorting by date descending (with default pagination)
# MangoPay::BankAccount.fetch(user_id, {'sort' => 'CreationDate:desc', 'page' => 2, 'per_page' => 3}) # both sorting and pagination params provided
#
# - For paginated queries the +filters+ param will be supplemented by +total_pages+ and +total_items+ info:
#
# MangoPay::User.fetch(filter = {'page' => 2, 'per_page' => 3})
# filter # => {"page"=>2, "per_page"=>3, "total_pages"=>1969, "total_items"=>5905}
#
def fetch(id_or_filters = nil, idempotency_key = nil)
id, filters = HTTPCalls::Fetch.parse_id_or_filters(id_or_filters)
response = MangoPay.request(:get, url(id), {}, filters, idempotency_key)
end
end
def self.included(base)
base.extend(ClassMethods)
end
def self.parse_id_or_filters(id_or_filters = nil)
id_or_filters.is_a?(Hash) ? [nil, id_or_filters] : [id_or_filters, {}]
end
end
module Refund
module ClassMethods
# See http://docs.mangopay.com/api-references/refund/%E2%80%A2-refund-a-pay-in/
# See http://docs.mangopay.com/api-references/refund/%E2%80%A2-refund-a-transfer/
def refund(id = nil, params = {}, idempotency_key = nil)
MangoPay.request(:post, url(id) + '/refunds', params, {}, idempotency_key)
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
end
end