ruby-jet 0.10.0 → 0.11.0

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jet/client.rb +26 -3
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dff4eb0d33751366c11744bbcbf74c94c04fdc1d
4
- data.tar.gz: 443ddbed19d0affbb1d6f7868431a39e43e3bc71
3
+ metadata.gz: e5e20b02470e4099df5634553406809917613d1d
4
+ data.tar.gz: 3b0c798cc743eda5e151ee81152a36725a8fe34d
5
5
  SHA512:
6
- metadata.gz: 48c80783ac8afdf4a72417cf53f7af9dd5b30fc172af988e32cc8fd2a1fae164c25c1a8ba7b821114e12a1e29738288168290d63aa31bb4e18fdb1e6fa3478eb
7
- data.tar.gz: 76ce5a7e121e4de797c2028c500d6d88859cadd9abd0d581764c63a097f1e08f5390fdc99c3a619a7e7c357049499cb0edd3251da3665d9691e6c472006d7e63
6
+ metadata.gz: 30295fad8c210e5512755bdfa82b2fc0da9807372fa5f40535c04a9ff4ee7e2d604b8e74f5ffacb06ee585929b7aefe6a6323e501993bdd3344d20bafaf1aa90
7
+ data.tar.gz: c3254c6a0062a4107c0bf000e12ffb41d1db41706c97599d271ec36e4f46d9e2cbfe480863e94b100f9a5422049db975386018431e5e477317b82542f14cf8eb
@@ -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 token
24
- unless @id_token && @token_type && @expires_on > Time.now
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?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-jet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Wells