berbix 0.0.2 → 0.0.3
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 +42 -23
- 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: c9b53c729e6703efb663060b297a04b1a8a5bb3d672cd167474d9a0f9cc55183
|
4
|
+
data.tar.gz: '079d33739bae65f6421554fa8db553de49189fd5e19076ef2d2491f3a479cdd4'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ccdaf863d45c20f9f47191358278d341802f794ce17f7e3cd73cb2b8254cc6b90310397aecc975e2ddae45ea5a458e0d8f9f532782914f864c3983a8c427911
|
7
|
+
data.tar.gz: 32a4b31c0c03efb526a2d161f756d469f6b96e77ba8d6de61d6d54838cf167b483bbf4473694fe9f5048fa9b028424d6dfe515451c8fa3d6df6ffd34391a3609
|
data/lib/berbix.rb
CHANGED
@@ -36,25 +36,33 @@ module Berbix
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
class
|
40
|
-
attr_reader :access_token, :refresh_token, :expiry, :user_id
|
39
|
+
class Tokens
|
40
|
+
attr_reader :access_token, :client_token, :refresh_token, :expiry, :transaction_id, :user_id
|
41
41
|
|
42
|
-
def initialize(refresh_token, access_token=nil, expiry=nil,
|
42
|
+
def initialize(refresh_token, access_token=nil, client_token=nil, expiry=nil, transaction_id=nil)
|
43
43
|
@refresh_token = refresh_token
|
44
44
|
@access_token = access_token
|
45
|
+
@client_token = client_token
|
45
46
|
@expiry = expiry
|
46
|
-
@
|
47
|
+
@transaction_id = transaction_id
|
48
|
+
@user_id = transaction_id
|
47
49
|
end
|
48
50
|
|
49
|
-
def refresh!(access_token, expiry,
|
51
|
+
def refresh!(access_token, client_token, expiry, transaction_id)
|
50
52
|
@access_token = access_token
|
53
|
+
@client_token = client_token
|
51
54
|
@expiry = expiry
|
52
|
-
@
|
55
|
+
@transaction_id = transaction_id
|
56
|
+
@user_id = transaction_id
|
53
57
|
end
|
54
58
|
|
55
59
|
def needs_refresh?
|
56
60
|
@access_token.nil? || @expiry.nil? || @expiry < Time.now
|
57
61
|
end
|
62
|
+
|
63
|
+
def self.from_refresh(refresh_token)
|
64
|
+
Tokens.new(refresh_token)
|
65
|
+
end
|
58
66
|
end
|
59
67
|
|
60
68
|
class Client
|
@@ -72,17 +80,22 @@ module Berbix
|
|
72
80
|
end
|
73
81
|
end
|
74
82
|
|
75
|
-
def
|
83
|
+
def create_transaction(opts={})
|
76
84
|
payload = {}
|
77
85
|
payload[:email] = opts[:email] unless opts[:email].nil?
|
78
86
|
payload[:phone] = opts[:phone] unless opts[:phone].nil?
|
79
87
|
payload[:customer_uid] = opts[:customer_uid] unless opts[:customer_uid].nil?
|
80
|
-
fetch_tokens('/v0/
|
88
|
+
fetch_tokens('/v0/transactions', payload)
|
89
|
+
end
|
90
|
+
|
91
|
+
# This method is deprecated, please use create_transaction instead
|
92
|
+
def create_user(opts={})
|
93
|
+
create_transaction(opts)
|
81
94
|
end
|
82
95
|
|
83
|
-
def refresh_tokens(
|
96
|
+
def refresh_tokens(tokens)
|
84
97
|
fetch_tokens('/v0/tokens', {
|
85
|
-
'refresh_token' =>
|
98
|
+
'refresh_token' => tokens.refresh_token,
|
86
99
|
'grant_type' => 'refresh_token',
|
87
100
|
})
|
88
101
|
end
|
@@ -94,28 +107,33 @@ module Berbix
|
|
94
107
|
})
|
95
108
|
end
|
96
109
|
|
97
|
-
def
|
98
|
-
token_auth_request(:get,
|
110
|
+
def fetch_transaction(tokens)
|
111
|
+
token_auth_request(:get, tokens, '/v0/transactions')
|
112
|
+
end
|
113
|
+
|
114
|
+
# This method is deprecated, please use fetch_transaction instead
|
115
|
+
def fetch_user(tokens)
|
116
|
+
fetch_transaction(tokens)
|
99
117
|
end
|
100
118
|
|
101
|
-
def create_continuation(
|
102
|
-
result = token_auth_request(:post,
|
119
|
+
def create_continuation(tokens)
|
120
|
+
result = token_auth_request(:post, tokens, '/v0/continuations')
|
103
121
|
result['value']
|
104
122
|
end
|
105
123
|
|
106
124
|
private
|
107
125
|
|
108
|
-
def refresh_if_necessary!(
|
109
|
-
if
|
110
|
-
refreshed = refresh_tokens(
|
111
|
-
|
126
|
+
def refresh_if_necessary!(tokens)
|
127
|
+
if tokens.needs_refresh?
|
128
|
+
refreshed = refresh_tokens(tokens)
|
129
|
+
tokens.refresh!(refreshed.access_token, refreshed.client_token, refreshed.expiry, refreshed.transaction_id)
|
112
130
|
end
|
113
131
|
end
|
114
132
|
|
115
|
-
def token_auth_request(method,
|
116
|
-
refresh_if_necessary!(
|
133
|
+
def token_auth_request(method, tokens, path)
|
134
|
+
refresh_if_necessary!(tokens)
|
117
135
|
headers = {
|
118
|
-
'Authorization' => 'Bearer ' +
|
136
|
+
'Authorization' => 'Bearer ' + tokens.access_token,
|
119
137
|
'Content-Type' => 'application/json',
|
120
138
|
}
|
121
139
|
@http_client.request(method, @api_host + path, headers)
|
@@ -129,11 +147,12 @@ module Berbix
|
|
129
147
|
headers,
|
130
148
|
data: payload,
|
131
149
|
auth: auth())
|
132
|
-
|
150
|
+
Tokens.new(
|
133
151
|
result['refresh_token'],
|
134
152
|
result['access_token'],
|
153
|
+
result['client_token'],
|
135
154
|
Time.now + result['expires_in'],
|
136
|
-
result['
|
155
|
+
result['transaction_id'])
|
137
156
|
end
|
138
157
|
|
139
158
|
def auth
|