ruby-jet 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jet/client.rb +26 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5e20b02470e4099df5634553406809917613d1d
|
4
|
+
data.tar.gz: 3b0c798cc743eda5e151ee81152a36725a8fe34d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30295fad8c210e5512755bdfa82b2fc0da9807372fa5f40535c04a9ff4ee7e2d604b8e74f5ffacb06ee585929b7aefe6a6323e501993bdd3344d20bafaf1aa90
|
7
|
+
data.tar.gz: c3254c6a0062a4107c0bf000e12ffb41d1db41706c97599d271ec36e4f46d9e2cbfe480863e94b100f9a5422049db975386018431e5e477317b82542f14cf8eb
|
data/lib/jet/client.rb
CHANGED
@@ -6,10 +6,13 @@ module Jet
|
|
6
6
|
class Client
|
7
7
|
API_URL = 'https://merchant-api.jet.com/api'.freeze
|
8
8
|
|
9
|
-
def initialize(config = {})
|
9
|
+
def initialize(config = {}, raw_token = {})
|
10
10
|
@api_user = config[:api_user]
|
11
11
|
@secret = config[:secret]
|
12
12
|
@merchant_id = config[:merchant_id]
|
13
|
+
@id_token = raw_token[:id_token]
|
14
|
+
@token_type = raw_token[:token_type]
|
15
|
+
@expires_on = raw_token[:expires_on]
|
13
16
|
end
|
14
17
|
|
15
18
|
def encode_json(data)
|
@@ -20,8 +23,8 @@ module Jet
|
|
20
23
|
Oj.load(json)
|
21
24
|
end
|
22
25
|
|
23
|
-
def
|
24
|
-
unless
|
26
|
+
def generate_token
|
27
|
+
unless token_valid?
|
25
28
|
body = { user: @api_user, pass: @secret }
|
26
29
|
response = RestClient.post("#{API_URL}/token", encode_json(body))
|
27
30
|
parsed_response = decode_json(response.body)
|
@@ -29,10 +32,30 @@ module Jet
|
|
29
32
|
@token_type = parsed_response['token_type']
|
30
33
|
@expires_on = Time.parse(parsed_response['expires_on'])
|
31
34
|
end
|
35
|
+
raw_token
|
36
|
+
end
|
32
37
|
|
38
|
+
def token
|
39
|
+
generate_token unless token_valid?
|
33
40
|
{ Authorization: "#{@token_type} #{@id_token}" }
|
34
41
|
end
|
35
42
|
|
43
|
+
def raw_token
|
44
|
+
if token_valid?
|
45
|
+
{
|
46
|
+
id_token: @id_token,
|
47
|
+
token_type: @token_type,
|
48
|
+
expires_on: @expires_on
|
49
|
+
}
|
50
|
+
else
|
51
|
+
{}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def token_valid?
|
56
|
+
@id_token && @token_type && @expires_on > Time.now
|
57
|
+
end
|
58
|
+
|
36
59
|
def rest_get_with_token(path, query_params = {})
|
37
60
|
headers = token
|
38
61
|
headers[:params] = query_params unless query_params.empty?
|