flexirest 1.7.3 → 1.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4588448f89d5370f5b208ad7185537c4c70fac4d
4
- data.tar.gz: b496d2852fdacc8ef9656656e96a5a469419de4e
3
+ metadata.gz: 5c923ce76f5b90c0bdb2fba0090bf7f7c0e9a8b3
4
+ data.tar.gz: deb9b3f6c36c9507fbba1a0bd98a485dd1828124
5
5
  SHA512:
6
- metadata.gz: 055f11c39ba735be43fdb5bebabb77d9c1912fded9cfe0040c06dfe4b12b33f093b8dc60295cb8abf7ccd8bac4ffd40b0729e6b47bd179b262818848e12490d6
7
- data.tar.gz: c131e203c28350206d3f18ddaeb0c6610b099f192e8c85d7f29756343da06bdcd1875636067465ff136d750a12ed23d7690bc7bcadead01cc7380f85fe7f2a89
6
+ metadata.gz: 430b70a85bf6b8d5cd77eb6d6440e55245ef9915ca8adfd5c76d26bde7c4a6c36ecdbc4e7bc5ed9bdb8176191ecf5b1c2c0f9e63a51da0f2f16cf092f2d66f16
7
+ data.tar.gz: 500dbc7e0637b9f48c9476f6a099fd12815ab21a49fc69357cd30d110378020d1e9710354c21a11215d621e48719ac8f18817be07883e7d818b6d3dcbadcaafd
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.7.4
4
+
5
+ Feature:
6
+
7
+ - Returning `:retry` or now raising `Flexirest::CallbackRetryRequestException` from a callback will retry the current request.
8
+
3
9
  ## 1.7.3
4
10
 
5
11
  Bugfix:
@@ -88,7 +88,7 @@ end
88
88
 
89
89
  **Note:** since v1.3.21 the empty response trick above isn't necessary, empty responses for 204 are accepted normally (the method returns `true`), but this is here to show an example of an `after_request` callback adjusting the body. The `cache_all_people` example shows how to cache a response even if the server doesn't send the correct headers.
90
90
 
91
- If you want to trap an error in an `after_request` callback and retry the request, this can be done - but retries will only happen once for each request (so we'd recommend checking all conditions in a single `after_request` and then retrying after fixing them all). You achieve this by returning `:retry` from the callback.
91
+ If you want to trap an error in an `after_request` callback and retry the request, this can be done - but retries will only happen once for each request (so we'd recommend checking all conditions in a single `after_request` and then retrying after fixing them all). You achieve this by raising a `Flexirest::CallbackRetryRequestException` from the callback.
92
92
 
93
93
  ```ruby
94
94
  class Person < Flexirest::Base
@@ -102,7 +102,7 @@ class Person < Flexirest::Base
102
102
  if response.status == 401
103
103
  # Do something to fix the state of caches/variables used in the
104
104
  # before_request, etc
105
- return :retry
105
+ raise Flexirest::CallbackRetryRequestException.new
106
106
  end
107
107
  end
108
108
  end
@@ -39,7 +39,7 @@ module Flexirest
39
39
  return false
40
40
  end
41
41
  if result == :retry
42
- return :retry
42
+ raise Flexirest::CallbackRetryRequestException.new
43
43
  end
44
44
  end
45
45
  end
@@ -66,4 +66,6 @@ module Flexirest
66
66
  end
67
67
 
68
68
  end
69
+
70
+ class CallbackRetryRequestException < Exception ; end
69
71
  end
@@ -239,13 +239,14 @@ module Flexirest
239
239
  if object_is_class? && @object.record_response?
240
240
  @object.record_response(self.url, response_env)
241
241
  end
242
- if object_is_class?
243
- callback_result = @object.send(:_callback_request, :after, @method[:name], response_env)
244
- else
245
- callback_result = @object.class.send(:_callback_request, :after, @method[:name], response_env)
246
- end
247
242
 
248
- if callback_result == :retry
243
+ begin
244
+ if object_is_class?
245
+ callback_result = @object.send(:_callback_request, :after, @method[:name], response_env)
246
+ else
247
+ callback_result = @object.class.send(:_callback_request, :after, @method[:name], response_env)
248
+ end
249
+ rescue Flexirest::CallbackRetryRequestException
249
250
  if self.retrying != true
250
251
  self.retrying = true
251
252
  return call()
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.7.3"
2
+ VERSION = "1.7.4"
3
3
  end
@@ -96,6 +96,11 @@ describe Flexirest::Request do
96
96
  base_url "http://www.example.com"
97
97
 
98
98
  after_request :handle_retries
99
+ after_request :inner_call
100
+
101
+ def self.reset_retries
102
+ @retries = 0
103
+ end
99
104
 
100
105
  def self.incr_retries
101
106
  @retries ||= 0
@@ -113,7 +118,17 @@ describe Flexirest::Request do
113
118
  end
114
119
  end
115
120
 
121
+ def inner_call(name, response)
122
+ if name == :first_call
123
+ self.class.incr_retries
124
+ self.second_call
125
+ raise Flexirest::CallbackRetryRequestException.new
126
+ end
127
+ end
128
+
116
129
  get :do_me_twice, "/do_me_twice"
130
+ get :first_call, "/first_call"
131
+ get :second_call, "/second_call"
117
132
  end
118
133
 
119
134
  class LazyLoadedExampleClient < ExampleClient
@@ -908,10 +923,21 @@ describe Flexirest::Request do
908
923
  it "should retry if an after_request callback returns :retry" do
909
924
  stub_request(:get, "http://www.example.com/do_me_twice").
910
925
  to_return(:status => 200, :body => "", :headers => {})
926
+ RetryingExampleClient.reset_retries
911
927
  RetryingExampleClient.do_me_twice
912
928
  expect(RetryingExampleClient.retries).to eq(2)
913
929
  end
914
930
 
931
+ it "should allow a second call and then retry if an after_request callback returns :retry" do
932
+ stub_request(:get, "http://www.example.com/first_call").
933
+ to_return(:status => 200, :body => "", :headers => {})
934
+ stub_request(:get, "http://www.example.com/second_call").
935
+ to_return(:status => 200, :body => "", :headers => {})
936
+ RetryingExampleClient.reset_retries
937
+ RetryingExampleClient.first_call
938
+ expect(RetryingExampleClient.retries).to eq(2)
939
+ end
940
+
915
941
 
916
942
  context "Direct URL requests" do
917
943
  class SameServerExampleClient < Flexirest::Base
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexirest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.3
4
+ version: 1.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-08 00:00:00.000000000 Z
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler