lhc 6.6.0 → 6.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8592e613a6a21d6dfc84973f2ab064d9e441246
4
- data.tar.gz: 73b9069f8c0793e5775c95e754c9160f70fc158a
3
+ metadata.gz: b67eccab540898da2aa4314e60c3f5b2488f0890
4
+ data.tar.gz: f2389139e0adab0fd12393a2be3550de57aa1798
5
5
  SHA512:
6
- metadata.gz: 9fc936bc72038bfb6833307dc9594d7f3e9721f406df6ed9eac1036ea6b45afa3e149d8b333ca1b29c067308ad2c3762e77333c9a3abb9e12b6e77610ff1f82e
7
- data.tar.gz: ec2dda75db78848b877bd5bcfcc2f88aedb6694aeda1c70eec419c7df6f52dfe684397a30a60aebaf157769916b0ed34c309ca43e0215dfd2cdb9cdbf70b5f43
6
+ metadata.gz: e82e6da39f0aa9f0d6b4ca46843f376c78a87fe4f4bdd8da58548291783f213a0aa346d0a2ca2c56157f8a028ba99d050f17d958ad30cc472a8b35b7558c76dd
7
+ data.tar.gz: f5f99a5be4829ab641c3aedc4ef3ebc19d63786290c8c5050abace75ee659ebf16f826ac605659169bd2bab8c768150a679ef4709e279881a1e8df7c9da61554
@@ -38,3 +38,6 @@ RSpec/VerifiedDoubles:
38
38
 
39
39
  Style/RedundantReturn:
40
40
  Enabled: false
41
+
42
+ RSpec/InstanceVariable:
43
+ Enabled: false
data/README.md CHANGED
@@ -76,7 +76,7 @@ You will get back an array of LHC::Response objects in the same order of the pas
76
76
  ```
77
77
 
78
78
  ```ruby
79
- LHC.get([request1, request2, request3])
79
+ LHC.request([request1, request2, request3])
80
80
  # returns [response1, response2, response3]
81
81
  ```
82
82
 
@@ -148,7 +148,7 @@ If a error handler is provided nothing is raised.
148
148
  If your error handler returns anything else but `nil` it replaces the response body.
149
149
 
150
150
  ```ruby
151
- handler = ->{ do_something; return {name: 'unknown'} }
151
+ handler = ->(response){ do_something_with_repsonse; return {name: 'unknown'} }
152
152
  response = LHC.get('http://something', error_handler: handler)
153
153
  response.data.name # 'unknown'
154
154
  ```
@@ -185,10 +185,11 @@ To monitor and manipulate the http communication done with LHC, you can define i
185
185
  → [Read more about interceptors](docs/interceptors.md)
186
186
 
187
187
  A set of core interceptors is part of LHC,
188
- like
188
+ like
189
189
  [Caching](/docs/interceptors/caching.md),
190
190
  [Monitoring](/docs/interceptors/monitoring.md),
191
191
  [Authentication](/docs/interceptors/authentication.md),
192
+ [Retry](/docs/interceptors/retry.md),
192
193
  [Rollbar](/docs/interceptors/rollbar.md),
193
194
  [Prometheus](/docs/interceptors/prometheus.md).
194
195
 
@@ -0,0 +1,24 @@
1
+ # Retry Interceptor
2
+
3
+ If you enable the retry interceptor, you can have lhc retry requests for you:
4
+
5
+ ```ruby
6
+ LHC.config.interceptors = [LHC::Retry]
7
+ response = LHC.get('http://local.ch', retry: true)
8
+ ```
9
+
10
+ It will try to retry the request up to 3 times (default) internally, before it passes the last response back, or raises an error for the last response.
11
+
12
+ Consider, that all other interceptors will run for every single retry.
13
+
14
+ ## Limit the amount of retries while making the request
15
+
16
+ ```ruby
17
+ LHC.get('http://local.ch', retry: { max: 1 })
18
+ ```
19
+
20
+ ## Change the default maximum of retries of the retry interceptor
21
+
22
+ ```ruby
23
+ LHC::Retry.max = 3
24
+ ```
data/lib/lhc.rb CHANGED
@@ -18,6 +18,8 @@ module LHC
18
18
  'lhc/interceptors/caching'
19
19
  autoload :Prometheus,
20
20
  'lhc/interceptors/prometheus'
21
+ autoload :Retry,
22
+ 'lhc/interceptors/retry'
21
23
  autoload :Config,
22
24
  'lhc/config'
23
25
  autoload :Endpoint,
@@ -0,0 +1,38 @@
1
+ class LHC::Retry < LHC::Interceptor
2
+ attr_accessor :retries, :current_retry
3
+
4
+ class << self
5
+ attr_accessor :max
6
+ end
7
+
8
+ def after_response(response)
9
+ response.request.options[:retries] ||= 0
10
+ return unless retry?(response.request)
11
+ response.request.options[:retries] += 1
12
+ current_retry = response.request.options[:retries]
13
+ begin
14
+ response.request.run!
15
+ rescue LHC::Error
16
+ return
17
+ end
18
+ response.request.response if current_retry == response.request.options[:retries]
19
+ end
20
+
21
+ private
22
+
23
+ def retry?(request)
24
+ return false if request.response.success?
25
+ return false unless request.options.dig(:retry)
26
+ request.options[:retries] < max(request)
27
+ end
28
+
29
+ def max(request)
30
+ options(request).is_a?(Hash) ? options(request).fetch(:max, LHC::Retry.max) : LHC::Retry.max
31
+ end
32
+
33
+ def options(request)
34
+ @options ||= request.options.dig(:retry)
35
+ end
36
+ end
37
+
38
+ LHC::Retry.max = 3
@@ -20,7 +20,7 @@ class LHC::Request
20
20
  self.raw = create_request
21
21
  self.format = options.delete('format') || LHC::Formats::JSON.new
22
22
  iprocessor.intercept(:before_request, self)
23
- raw.run if self_executing && !response
23
+ run! if self_executing && !response
24
24
  end
25
25
 
26
26
  def url
@@ -43,6 +43,10 @@ class LHC::Request
43
43
  ignore_error?
44
44
  end
45
45
 
46
+ def run!
47
+ raw.run
48
+ end
49
+
46
50
  private
47
51
 
48
52
  attr_accessor :iprocessor
@@ -100,7 +104,7 @@ class LHC::Request
100
104
  end
101
105
 
102
106
  def on_complete(response)
103
- self.response ||= LHC::Response.new(response, self)
107
+ self.response = LHC::Response.new(response, self)
104
108
  iprocessor.intercept(:after_response, self.response)
105
109
  handle_error(self.response) unless self.response.success?
106
110
  end
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= "6.6.0"
2
+ VERSION ||= "6.7.0"
3
3
  end
@@ -0,0 +1,42 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC::Rollbar do
4
+ before(:each) do
5
+ LHC.config.interceptors = [LHC::Retry]
6
+ end
7
+
8
+ let(:request_stub) do
9
+ @retry_count = 0
10
+ stub_request(:get, 'http://local.ch').to_return do |_|
11
+ if @retry_count == max_retry_count
12
+ { status: 200 }
13
+ else
14
+ @retry_count += 1
15
+ { status: 500 }
16
+ end
17
+ end
18
+ end
19
+
20
+ let(:max_retry_count) { 3 }
21
+
22
+ it 'retries a request up to 3 times (default)' do
23
+ request_stub
24
+ response = LHC.get('http://local.ch', retry: true)
25
+ expect(response.success?).to eq true
26
+ expect(response.code).to eq 200
27
+ expect(request_stub).to have_been_requested.times(4)
28
+ end
29
+
30
+ context 'retry only once' do
31
+ let(:retry_options) { { max: 1 } }
32
+ let(:max_retry_count) { 1 }
33
+
34
+ it 'retries only once' do
35
+ request_stub
36
+ response = LHC.get('http://local.ch', retry: { max: 1 })
37
+ expect(response.success?).to eq true
38
+ expect(response.code).to eq 200
39
+ expect(request_stub).to have_been_requested.times(2)
40
+ end
41
+ end
42
+ end
@@ -26,7 +26,6 @@ describe LHC::Request do
26
26
  LHC.configure { |c| c.interceptors = [TestInterceptor] }
27
27
  end
28
28
 
29
- # rubocop:disable RSpec/InstanceVariable
30
29
  it 'calls interceptors also for parallel requests' do
31
30
  stub_parallel_requests
32
31
  @called = 0
@@ -35,6 +34,5 @@ describe LHC::Request do
35
34
  LHC.request(request_options)
36
35
  expect(@called).to eq 2
37
36
  end
38
- # rubocop:enable RSpec/InstanceVariable
39
37
  end
40
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.6.0
4
+ version: 6.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-04 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -182,6 +182,7 @@ files:
182
182
  - docs/interceptors/caching.md
183
183
  - docs/interceptors/monitoring.md
184
184
  - docs/interceptors/prometheus.md
185
+ - docs/interceptors/retry.md
185
186
  - docs/interceptors/rollbar.md
186
187
  - docs/request.md
187
188
  - docs/response.md
@@ -206,6 +207,7 @@ files:
206
207
  - lib/lhc/interceptors/caching.rb
207
208
  - lib/lhc/interceptors/monitoring.rb
208
209
  - lib/lhc/interceptors/prometheus.rb
210
+ - lib/lhc/interceptors/retry.rb
209
211
  - lib/lhc/interceptors/rollbar.rb
210
212
  - lib/lhc/railtie.rb
211
213
  - lib/lhc/request.rb
@@ -292,6 +294,7 @@ files:
292
294
  - spec/interceptors/monitoring/main_spec.rb
293
295
  - spec/interceptors/prometheus_spec.rb
294
296
  - spec/interceptors/response_competition_spec.rb
297
+ - spec/interceptors/retry/main_spec.rb
295
298
  - spec/interceptors/return_response_spec.rb
296
299
  - spec/interceptors/rollbar/main_spec.rb
297
300
  - spec/rails_helper.rb
@@ -341,7 +344,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
341
344
  requirements:
342
345
  - Ruby >= 2.0.0
343
346
  rubyforge_project:
344
- rubygems_version: 2.6.8
347
+ rubygems_version: 2.6.12
345
348
  signing_key:
346
349
  specification_version: 4
347
350
  summary: LocalHttpClient
@@ -421,6 +424,7 @@ test_files:
421
424
  - spec/interceptors/monitoring/main_spec.rb
422
425
  - spec/interceptors/prometheus_spec.rb
423
426
  - spec/interceptors/response_competition_spec.rb
427
+ - spec/interceptors/retry/main_spec.rb
424
428
  - spec/interceptors/return_response_spec.rb
425
429
  - spec/interceptors/rollbar/main_spec.rb
426
430
  - spec/rails_helper.rb