berbix 0.0.5 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/berbix.rb +52 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07a9370bef12c540f5221ada05219c051922698b0d738017b97bf27e60c52deb
|
4
|
+
data.tar.gz: e075b7d20b1951b818312d6081e33da021b46247f33ece5006a9d5f8915972c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4381b67fe62cf954b963ef20ba5a859469c22bd13236dd8185f5ef48b1b0ae8c61733af48e2d5788edadd8951544b5eeb5e257f47117f8c4e53b0b830f6823fd
|
7
|
+
data.tar.gz: fafff8852c9cb95cc7f0e78f254875677dede2ca46f93c43dee21c83660efffc2ce9a03142733949f2aa04dafeb60ba81269d5570be08e93fbbcbe93f66760e5
|
data/lib/berbix.rb
CHANGED
@@ -2,8 +2,7 @@ require 'net/https'
|
|
2
2
|
require 'json'
|
3
3
|
|
4
4
|
module Berbix
|
5
|
-
|
6
|
-
SDK_VERSION = '0.0.5'
|
5
|
+
SDK_VERSION = '0.0.10'
|
7
6
|
CLOCK_DRIFT = 300
|
8
7
|
|
9
8
|
class HTTPClient
|
@@ -13,10 +12,23 @@ module Berbix
|
|
13
12
|
end
|
14
13
|
|
15
14
|
class NetHTTPClient < HTTPClient
|
15
|
+
attr_reader :read_timeout
|
16
|
+
attr_reader :open_timeout
|
17
|
+
|
18
|
+
def initialize(opts={})
|
19
|
+
# Sets the defaults to align with the Net::HTTP defaults
|
20
|
+
@open_timeout = opts[:open_timeout] || 60
|
21
|
+
@read_timeout = opts[:read_timeout] || 60
|
22
|
+
end
|
23
|
+
|
16
24
|
def request(method, url, headers, opts={})
|
17
25
|
uri = URI(url)
|
18
26
|
klass = if method == :post
|
19
27
|
Net::HTTP::Post
|
28
|
+
elsif method == :patch
|
29
|
+
Net::HTTP::Patch
|
30
|
+
elsif method == :delete
|
31
|
+
Net::HTTP::Delete
|
20
32
|
else
|
21
33
|
Net::HTTP::Get
|
22
34
|
end
|
@@ -29,26 +41,32 @@ module Berbix
|
|
29
41
|
end
|
30
42
|
cli = Net::HTTP.new(uri.host, uri.port).tap do |http|
|
31
43
|
http.use_ssl = true
|
44
|
+
http.read_timeout = read_timeout
|
45
|
+
http.open_timeout = open_timeout
|
32
46
|
end
|
33
47
|
res = cli.request(req)
|
34
48
|
code = res.code.to_i
|
35
49
|
if code < 200 || code >= 300
|
36
|
-
raise 'unexpected status code returned'
|
50
|
+
raise(Berbix::BerbixError, 'unexpected status code returned')
|
51
|
+
end
|
52
|
+
if code == 204
|
53
|
+
return
|
37
54
|
end
|
38
55
|
JSON.parse(res.body)
|
39
56
|
end
|
40
57
|
end
|
41
58
|
|
42
59
|
class Tokens
|
43
|
-
attr_reader :access_token, :client_token, :refresh_token, :expiry, :transaction_id, :user_id
|
60
|
+
attr_reader :access_token, :client_token, :refresh_token, :expiry, :transaction_id, :user_id, :response
|
44
61
|
|
45
|
-
def initialize(refresh_token, access_token=nil, client_token=nil, expiry=nil, transaction_id=nil)
|
62
|
+
def initialize(refresh_token, access_token=nil, client_token=nil, expiry=nil, transaction_id=nil, response=nil)
|
46
63
|
@refresh_token = refresh_token
|
47
64
|
@access_token = access_token
|
48
65
|
@client_token = client_token
|
49
66
|
@expiry = expiry
|
50
67
|
@transaction_id = transaction_id
|
51
68
|
@user_id = transaction_id
|
69
|
+
@response = response
|
52
70
|
end
|
53
71
|
|
54
72
|
def refresh!(access_token, client_token, expiry, transaction_id)
|
@@ -70,16 +88,12 @@ module Berbix
|
|
70
88
|
|
71
89
|
class Client
|
72
90
|
def initialize(opts={})
|
73
|
-
@
|
74
|
-
@client_secret = opts[:client_secret]
|
91
|
+
@api_secret = opts[:api_secret] || opts[:client_secret]
|
75
92
|
@api_host = api_host(opts)
|
76
93
|
@http_client = opts[:http_client] || NetHTTPClient.new
|
77
94
|
|
78
|
-
if @
|
79
|
-
raise ':
|
80
|
-
end
|
81
|
-
if @client_secret.nil?
|
82
|
-
raise ':client_secret must be provided when instantiating Berbix client'
|
95
|
+
if @api_secret.nil?
|
96
|
+
raise ':api_secret must be provided when instantiating Berbix client'
|
83
97
|
end
|
84
98
|
end
|
85
99
|
|
@@ -89,6 +103,7 @@ module Berbix
|
|
89
103
|
payload[:phone] = opts[:phone] unless opts[:phone].nil?
|
90
104
|
payload[:customer_uid] = opts[:customer_uid].to_s unless opts[:customer_uid].nil?
|
91
105
|
payload[:template_key] = opts[:template_key] unless opts[:template_key].nil?
|
106
|
+
payload[:hosted_options] = opts[:hosted_options] unless opts[:hosted_options].nil?
|
92
107
|
fetch_tokens('/v0/transactions', payload)
|
93
108
|
end
|
94
109
|
|
@@ -115,6 +130,24 @@ module Berbix
|
|
115
130
|
token_auth_request(:get, tokens, '/v0/transactions')
|
116
131
|
end
|
117
132
|
|
133
|
+
def delete_transaction(tokens)
|
134
|
+
token_auth_request(:delete, tokens, '/v0/transactions')
|
135
|
+
end
|
136
|
+
|
137
|
+
def update_transaction(tokens, opts={})
|
138
|
+
payload = {}
|
139
|
+
payload[:action] = opts[:action] unless opts[:action].nil?
|
140
|
+
payload[:note] = opts[:note] unless opts[:note].nil?
|
141
|
+
token_auth_request(:patch, tokens, '/v0/transactions', data: payload)
|
142
|
+
end
|
143
|
+
|
144
|
+
def override_transaction(tokens, opts={})
|
145
|
+
payload = {}
|
146
|
+
payload[:response_payload] = opts[:response_payload] unless opts[:response_payload].nil?
|
147
|
+
payload[:flags] = opts[:flags] unless opts[:flags].nil?
|
148
|
+
token_auth_request(:patch, tokens, '/v0/transactions/override', data: payload)
|
149
|
+
end
|
150
|
+
|
118
151
|
# This method is deprecated, please use fetch_transaction instead
|
119
152
|
def fetch_user(tokens)
|
120
153
|
fetch_transaction(tokens)
|
@@ -152,14 +185,14 @@ module Berbix
|
|
152
185
|
end
|
153
186
|
end
|
154
187
|
|
155
|
-
def token_auth_request(method, tokens, path)
|
188
|
+
def token_auth_request(method, tokens, path, opts={})
|
156
189
|
refresh_if_necessary!(tokens)
|
157
190
|
headers = {
|
158
191
|
'Authorization' => 'Bearer ' + tokens.access_token,
|
159
192
|
'Content-Type' => 'application/json',
|
160
193
|
'User-Agent' => 'BerbixRuby/' + SDK_VERSION,
|
161
194
|
}
|
162
|
-
@http_client.request(method, @api_host + path, headers)
|
195
|
+
@http_client.request(method, @api_host + path, headers, opts)
|
163
196
|
end
|
164
197
|
|
165
198
|
def fetch_tokens(path, payload)
|
@@ -178,11 +211,12 @@ module Berbix
|
|
178
211
|
result['access_token'],
|
179
212
|
result['client_token'],
|
180
213
|
Time.now + result['expires_in'],
|
181
|
-
result['transaction_id']
|
214
|
+
result['transaction_id'],
|
215
|
+
result)
|
182
216
|
end
|
183
217
|
|
184
218
|
def auth
|
185
|
-
{ user: @
|
219
|
+
{ user: @api_secret, pass: '' }
|
186
220
|
end
|
187
221
|
|
188
222
|
def api_host(opts)
|
@@ -204,4 +238,6 @@ module Berbix
|
|
204
238
|
end
|
205
239
|
end
|
206
240
|
|
241
|
+
class BerbixError < StandardError
|
242
|
+
end
|
207
243
|
end
|