restful_resource 0.9.7 → 0.10.0

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: 9d61d5fddf5237bc8ecc817ecbcd12e62ed2a2d3
4
- data.tar.gz: 6c09b612c6aeef9038616b10f0fd71f504f0da07
3
+ metadata.gz: b226b8051b7fd554302f9bd01818e989ed038b82
4
+ data.tar.gz: 1fc72ed03e190e72dd6d2218c002f09a8cc97486
5
5
  SHA512:
6
- metadata.gz: 4b670c8eb7a54839ebacc287b232b556e18e18ab34366e2e7de1cf983c70fd8decca035e04cb29dc15329a8c6e91fbabb2d59df61f2edbcb9f5c95b09a251c00
7
- data.tar.gz: 8b003a527dda9636ff65bece04dd3ca1b57c89b7fe5b9dc46734f199c933dbefe7fef43e73a40a13cd3e9c928bc424c689976d744c54b53bedc061fc2606d998
6
+ metadata.gz: 7c1ee3d6f785bef9ef6e02960413dfa8b7ab58be0894c6dd50ec8446559a24b1d17f7da83ae70480fff36e42bc763f24074953a5e6cdada66204afc3f90dfdd6
7
+ data.tar.gz: e1df5b650b4e77f65b775b760537aaa42d7820a6757a84c77ab3c21ddcd92f6a42062cf5260b84c60954d0421f5a5997411a3d0da797492cb29cc7f12331a9b9
@@ -18,5 +18,5 @@ require_relative 'restful_resource/response'
18
18
  require_relative 'restful_resource/http_client'
19
19
  require_relative 'restful_resource/associations'
20
20
  require_relative 'restful_resource/rails_validations'
21
+ require_relative 'restful_resource/redirections'
21
22
  require_relative 'restful_resource/base'
22
-
@@ -2,17 +2,17 @@ module RestfulResource
2
2
  class Base < OpenObject
3
3
  extend RestfulResource::Associations
4
4
 
5
- def self.configure(base_url: nil,
6
- username: nil,
7
- password: nil,
8
- logger: nil,
5
+ def self.configure(base_url: nil,
6
+ username: nil,
7
+ password: nil,
8
+ logger: nil,
9
9
  cache_store: nil)
10
10
 
11
11
  @base_url = URI.parse(base_url)
12
12
 
13
- @http = RestfulResource::HttpClient.new(username: username,
14
- password: password,
15
- logger: logger,
13
+ @http = RestfulResource::HttpClient.new(username: username,
14
+ password: password,
15
+ logger: logger,
16
16
  cache_store: cache_store)
17
17
  end
18
18
 
@@ -51,6 +51,7 @@ module RestfulResource
51
51
  url = collection_url(params)
52
52
 
53
53
  response = http.post(url, data: data)
54
+
54
55
  self.new(parse_json(response.body))
55
56
  end
56
57
 
@@ -73,7 +74,7 @@ module RestfulResource
73
74
  next_page = 1
74
75
  begin
75
76
  resources = self.where(conditions.merge(page: next_page))
76
- resources.each do |resource|
77
+ resources.each do |resource|
77
78
  y << resource
78
79
  end
79
80
  next_page = resources.next_page
@@ -95,6 +96,11 @@ module RestfulResource
95
96
  result
96
97
  end
97
98
 
99
+ def self.collection_url(params)
100
+ url = merge_url_paths(base_url, @resource_path, @action_prefix)
101
+ replace_parameters(url, params)
102
+ end
103
+
98
104
  private
99
105
  def self.merge_url_paths(uri, *paths)
100
106
  uri.merge(paths.compact.join('/')).to_s
@@ -106,11 +112,6 @@ module RestfulResource
106
112
  replace_parameters(url, params)
107
113
  end
108
114
 
109
- def self.collection_url(params)
110
- url = merge_url_paths(base_url, @resource_path, @action_prefix)
111
- replace_parameters(url, params)
112
- end
113
-
114
115
  def self.new_collection(json)
115
116
  json.map do |element|
116
117
  self.new(element)
@@ -46,8 +46,6 @@ module RestfulResource
46
46
  b.response :encoding
47
47
  b.use :gzip
48
48
 
49
- b.use FaradayMiddleware::FollowRedirects
50
-
51
49
  b.adapter :net_http
52
50
  end
53
51
  end
@@ -0,0 +1,48 @@
1
+ module RestfulResource
2
+ class MaximumAttemptsReached < StandardError
3
+ def message
4
+ "The maximum attempts limit was reached before the resource was ready"
5
+ end
6
+ end
7
+
8
+ module Redirections
9
+ def self.included(base)
10
+ base.instance_eval do
11
+ def post(data: {}, delay: 0.5, max_attempts: 10, **params)
12
+ url = collection_url(params)
13
+
14
+ response = self.accept_redirected_result(response: http.post(url, data: data), delay: delay, max_attempts: max_attempts)
15
+
16
+ self.new(parse_json(response.body))
17
+ end
18
+
19
+ private
20
+ def self.accept_redirected_result(response:, delay:, max_attempts:)
21
+ new_response = response
22
+ if response.status == 303
23
+ attempts = 0
24
+ resource_location = response.headers[:location]
25
+
26
+ RestfulResource::Redirections.wait(delay)
27
+ new_response = http.get(resource_location)
28
+
29
+ while (new_response.status == 202) && (attempts < max_attempts)
30
+ attempts += 1
31
+ RestfulResource::Redirections.wait(delay)
32
+ new_response = http.get(resource_location)
33
+ end
34
+
35
+ if attempts == max_attempts
36
+ raise RestfulResource::MaximumAttemptsReached
37
+ end
38
+ end
39
+ response = new_response
40
+ end
41
+ end
42
+ end
43
+
44
+ def self.wait(seconds)
45
+ Kernel.sleep(seconds)
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module RestfulResource
2
- VERSION = '0.9.7'
2
+ VERSION = '0.10.0'
3
3
  end
data/spec/fixtures.rb CHANGED
@@ -28,6 +28,12 @@ class TestB < BaseB
28
28
  self.resource_path "testb"
29
29
  end
30
30
 
31
+ class ModelWithRedirections < RestfulResource::Base
32
+ include RestfulResource::Redirections
33
+
34
+ resource_path 'model_with_redirections'
35
+ end
36
+
31
37
  module ComplicatedModule
32
38
  class Parent < BaseA
33
39
  resource_path "parent"
@@ -0,0 +1,88 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe RestfulResource::Redirections do
4
+ before :each do
5
+ @mock_http = double("mock_http")
6
+ allow(RestfulResource::Base).to receive(:http).and_return(@mock_http)
7
+ RestfulResource::Base.configure(base_url: 'http://api.carwow.co.uk/')
8
+ end
9
+
10
+ describe "#post" do
11
+ let(:data) { {data: 123} }
12
+
13
+ subject { ModelWithRedirections.post(data: data) }
14
+
15
+ context 'with a 200 response' do
16
+ it 'should behave as usual' do
17
+ expected_response = RestfulResource::Response.new(body: {test_data: 42}.to_json)
18
+ expect_post("http://api.carwow.co.uk/model_with_redirections", expected_response, data: data)
19
+
20
+ expect(subject.test_data).to eq 42
21
+ end
22
+ end
23
+
24
+ context 'with a redirect to resource location' do
25
+ let(:redirect_target) { 'http://api.carwow.co.uk/model_with_redirections/123' }
26
+
27
+ before(:each) do
28
+ allow(RestfulResource::Redirections).to receive(:wait)
29
+ expected_redirect_response = RestfulResource::Response.new(body: 'You are being redirected', status: 303, headers: { location: redirect_target})
30
+ expect_post("http://api.carwow.co.uk/model_with_redirections", expected_redirect_response, data: data)
31
+ end
32
+
33
+ it 'should get the resource from the new location' do
34
+ expected_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
35
+ expect_get(redirect_target, expected_get_response)
36
+
37
+ expect(subject.test_data).to eq 42
38
+ end
39
+
40
+ it 'should wait 0.5 seconds after first redirect' do
41
+ expected_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
42
+
43
+ expect(RestfulResource::Redirections).to receive(:wait).with(0.5).ordered
44
+ expect_get(redirect_target, expected_get_response).ordered
45
+
46
+ expect(subject.test_data).to eq 42
47
+ end
48
+
49
+ it 'should wait 0.5 between retries' do
50
+ resource_not_ready_get_response = RestfulResource::Response.new(body: 'pending', status: 202)
51
+ resource_ready_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
52
+
53
+ expect(RestfulResource::Redirections).to receive(:wait).with(0.5).ordered
54
+ expect_get(redirect_target, resource_not_ready_get_response).ordered
55
+ expect(RestfulResource::Redirections).to receive(:wait).with(0.5).ordered
56
+ expect_get(redirect_target, resource_ready_get_response).ordered
57
+
58
+ expect(subject.test_data).to eq 42
59
+ end
60
+
61
+ it 'should retry 10 times by default' do
62
+ resource_not_ready_get_response = RestfulResource::Response.new(body: 'pending', status: 202)
63
+ resource_ready_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
64
+
65
+ 9.times do
66
+ expect(RestfulResource::Redirections).to receive(:wait).with(0.5).ordered
67
+ expect_get(redirect_target, resource_not_ready_get_response).ordered
68
+ end
69
+ expect(RestfulResource::Redirections).to receive(:wait).with(0.5).ordered
70
+ expect_get(redirect_target, resource_ready_get_response).ordered
71
+
72
+ expect(subject.test_data).to eq 42
73
+ end
74
+
75
+ it 'raise after max_retries value is reached' do
76
+ resource_not_ready_get_response = RestfulResource::Response.new(body: 'pending', status: 202)
77
+ resource_ready_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
78
+
79
+ 11.times do
80
+ expect(RestfulResource::Redirections).to receive(:wait).with(0.5).ordered
81
+ expect_get(redirect_target, resource_not_ready_get_response).ordered
82
+ end
83
+
84
+ expect{subject}.to raise_error(RestfulResource::MaximumAttemptsReached)
85
+ end
86
+ end
87
+ end
88
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Santoro
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-04-05 00:00:00.000000000 Z
12
+ date: 2016-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -174,6 +174,7 @@ files:
174
174
  - lib/restful_resource/paginated_array.rb
175
175
  - lib/restful_resource/parameter_missing_error.rb
176
176
  - lib/restful_resource/rails_validations.rb
177
+ - lib/restful_resource/redirections.rb
177
178
  - lib/restful_resource/resource_id_missing_error.rb
178
179
  - lib/restful_resource/response.rb
179
180
  - lib/restful_resource/version.rb
@@ -185,6 +186,7 @@ files:
185
186
  - spec/restful_resource/http_client_spec.rb
186
187
  - spec/restful_resource/open_object_spec.rb
187
188
  - spec/restful_resource/rails_validations_spec.rb
189
+ - spec/restful_resource/redirections_spec.rb
188
190
  - spec/spec_helper.rb
189
191
  homepage: http://www.github.com/carwow/restful_resource
190
192
  licenses:
@@ -219,4 +221,5 @@ test_files:
219
221
  - spec/restful_resource/http_client_spec.rb
220
222
  - spec/restful_resource/open_object_spec.rb
221
223
  - spec/restful_resource/rails_validations_spec.rb
224
+ - spec/restful_resource/redirections_spec.rb
222
225
  - spec/spec_helper.rb