analytics-psw 0.3.7 → 0.3.8

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/analytics-psw/utilities.rb +28 -16
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd280c2c3f9b9ca6dbb9deece3998f3bccf748f8
4
- data.tar.gz: da2ade09287d2bacbe5a3436c46a1169571c61b4
3
+ metadata.gz: 53f493ce435433361a616d0b779a87d335616a2b
4
+ data.tar.gz: 128a17668f84edeb98f19cc37a249641043a12dd
5
5
  SHA512:
6
- metadata.gz: 2d71d2c479aa0eaad9c3cf4b0f7ab8a2f15a1eb262742c738b54de3a0708778c0bf377826b5c6817448a4dd4089e7f2c8829944c9f29861c7efb304ea32f7579
7
- data.tar.gz: 03e673c04dbaf3215a23f746ab41676788259397851f01693fa4ef5472a81b8db47b89686da0071bd8219ea64817a713e7ec7c17c8884adce0373eab0c826723
6
+ metadata.gz: 1ea406d43608bf5910d4b5b2ddbfb608d2638247a8297c9dbda440883180829b0b14c001abb68abc14a27ab28df81c5539abfad2534712c424d93b8c4104d285
7
+ data.tar.gz: d151d52b9a8bdb9558eb5b0d0bde63a5e589ec30825fabd63447611bbdc78dafae851fb190ffd32103e5826a4709f4a2c4f79e9808306aa81b328749aa873dd5
@@ -4,30 +4,39 @@ module AnalyticsPSW
4
4
 
5
5
  def update_token
6
6
  @expires = nil
7
- url = "#{@server_url}/oauth/token?client_id=#{@client_id}&client_secret=#{@client_secret}"
8
- token_response = http_request(:post, url)
9
- @token = token_response['body']['access_token']
10
- @expires = Time.now + token_response['body']['expires_in']
7
+ response = @conn.post do |request|
8
+ request.url "#{@server_url}/oauth/token?client_id=#{@client_id}&client_secret=#{@client_secret}"
9
+ request.headers['Content-Type'] = 'application/json'
10
+ end
11
+ if response['body'].present?
12
+ @token = response['body']['access_token']
13
+ @expires = Time.now + response['body']['expires_in']
14
+ end
11
15
  end
12
16
 
13
17
  def faraday_connection
14
18
  @proxy ? Faraday.new(@server_url, {proxy: {uri: @proxy}}) : Faraday.new(@server_url)
15
19
  end
16
20
 
17
- def http_request(http_method, url, params = nil, body = nil)
18
- update_token if @expires and Time.now > @expires
19
- json_block = ->(req) do
20
- req.url url
21
- req.headers['Content-Type'] = 'application/json'
22
- req.headers['Authorization']= "Bearer #{@token}" if @token
23
- req.params = params if params
24
- req.body = body if body
21
+ def http_request(http_method, url, params = nil, body = nil, retries = 2)
22
+ while retries > 0
23
+ json_block = ->(req) do
24
+ req.url url
25
+ req.headers['Content-Type'] = 'application/json'
26
+ req.headers['Authorization']= "Bearer #{@token}" if @token
27
+ req.params = params if params
28
+ req.body = body if body
29
+ end
30
+ response = parse_json_response(@conn.send(http_method, &json_block))
31
+ break if [200, 201, 202].include?(response['status'])
32
+ update_token if response['status'] == 401
33
+ retries -= 1
25
34
  end
26
- parse_json_response(@conn.send(http_method, &json_block))
35
+ response
27
36
  end
28
-
37
+
29
38
  def multi_get_json(service_location, query_parameters)
30
- update_token if @expires and Time.now > @expires
39
+ update_token if need_token_update?
31
40
  results = []
32
41
  EventMachine.run do
33
42
  multi = EventMachine::MultiRequest.new
@@ -83,6 +92,9 @@ module AnalyticsPSW
83
92
  end
84
93
  parsed_response
85
94
  end
86
-
95
+
96
+ def need_token_update?
97
+ (@client_id and @client_secret and @expires.nil?) or (@expires and Time.now > @expires)
98
+ end
87
99
  end
88
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: analytics-psw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lexmark International Technology S.A