ddy_remote_resource 1.1.2 → 1.2.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
  SHA256:
3
- metadata.gz: cdcc65c8c1ef37e590832d19cb42875c41be7e8c02510c17e92eb0a5a3bf6122
4
- data.tar.gz: '09d6e13818904b4ba85ec25ecb068515034cc653e559e5acd19f3e14c7074d07'
3
+ metadata.gz: d0032d56e8df7f6e90e146abb65b29a41a59a63e39747c6b0b7585c02f577069
4
+ data.tar.gz: 4454b9226626e5a2028dc871c83d15347da8a373d104e641810c3f002e8e94c0
5
5
  SHA512:
6
- metadata.gz: c54a32a3078e3ec62a3a24459b59441267c9d1829042d65c84e44de551f6507fdbec08a2bcb61f27c7e26abbe44f61678d02504d57f6dc8fbf772d195281a496
7
- data.tar.gz: 205982b8664b2d0fb78dacfb215b6b456305a63b3eef8fe0ccd036439f7ed826bd5d874b43d73a5a715f97c452d6d612e0d0a230e56ea95d3c871f4a0d78ba4a
6
+ metadata.gz: 327330942d08e782894d10b46be1c6a6fc560f4cab2a4f7bb77a2b2958853ad929a024ccc00c560a3672e48c55a5844e9064e9c7fc00ae9839228d904038f2b6
7
+ data.tar.gz: f6d6b6c025c695cd68f4697b9a001046d54a29b32f51aabbec187ff9359bf32da6db83510cc5a3a251729159100e06b11054986a009d55386ece26e5f372ed8b
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .idea/
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
@@ -14,6 +14,9 @@ module RemoteResource
14
14
 
15
15
  DEFAULT_EXTENSION = '.json'.freeze
16
16
 
17
+ DEFAULT_CONNECT_TIMEOUT = 30
18
+ DEFAULT_READ_TIMEOUT = 120
19
+
17
20
  attr_reader :resource, :resource_klass, :http_action, :attributes
18
21
 
19
22
  def initialize(resource, http_action, attributes = {}, connection_options = {})
@@ -41,7 +44,7 @@ module RemoteResource
41
44
  def perform
42
45
  SUPPORTED_HTTP_METHODS.include?(http_action) || raise(RemoteResource::HTTPMethodUnsupported, "Requested HTTP method=#{http_action.to_s} is NOT supported, the HTTP action MUST be a supported HTTP action=#{SUPPORTED_HTTP_METHODS.join(', ')}")
43
46
 
44
- connection_response = connection.public_send(http_action, request_url, params: query, body: body, headers: headers)
47
+ connection_response = connection.public_send(http_action, request_url, params: query, body: body, headers: headers, **timeout_options)
45
48
  response = RemoteResource::Response.new(connection_response, connection_options.merge(request: self, connection_request: connection_response.request))
46
49
 
47
50
  if response.success? || response.unprocessable_entity?
@@ -118,47 +121,57 @@ module RemoteResource
118
121
  headers
119
122
  end
120
123
 
124
+ def timeout_options
125
+ connecttimeout = connection_options[:connecttimeout].presence || DEFAULT_CONNECT_TIMEOUT
126
+ timeout = connection_options[:timeout].presence || DEFAULT_READ_TIMEOUT
127
+
128
+ { connecttimeout: connecttimeout, timeout: timeout }
129
+ end
130
+
121
131
  private
122
132
 
123
133
  def raise_http_error(request, response)
134
+ # Special case if a request has a time out, as Typhoeus does not set a 408 response_code
135
+ raise RemoteResource::HTTPRequestTimeout.new(request, response) if response.timed_out?
136
+
124
137
  case response.try(:response_code)
125
138
  when 301, 302, 303, 307 then
126
139
  raise RemoteResource::HTTPRedirectionError.new(request, response)
127
- when 400 then
140
+ when 400
128
141
  raise RemoteResource::HTTPBadRequest.new(request, response)
129
- when 401 then
142
+ when 401
130
143
  raise RemoteResource::HTTPUnauthorized.new(request, response)
131
- when 403 then
144
+ when 403
132
145
  raise RemoteResource::HTTPForbidden.new(request, response)
133
- when 404 then
146
+ when 404
134
147
  raise RemoteResource::HTTPNotFound.new(request, response)
135
- when 405 then
148
+ when 405
136
149
  raise RemoteResource::HTTPMethodNotAllowed.new(request, response)
137
- when 406 then
150
+ when 406
138
151
  raise RemoteResource::HTTPNotAcceptable.new(request, response)
139
- when 408 then
152
+ when 408
140
153
  raise RemoteResource::HTTPRequestTimeout.new(request, response)
141
- when 409 then
154
+ when 409
142
155
  raise RemoteResource::HTTPConflict.new(request, response)
143
- when 410 then
156
+ when 410
144
157
  raise RemoteResource::HTTPGone.new(request, response)
145
- when 418 then
158
+ when 418
146
159
  raise RemoteResource::HTTPTeapot.new(request, response)
147
- when 444 then
160
+ when 444
148
161
  raise RemoteResource::HTTPNoResponse.new(request, response)
149
- when 494 then
162
+ when 494
150
163
  raise RemoteResource::HTTPRequestHeaderTooLarge.new(request, response)
151
- when 495 then
164
+ when 495
152
165
  raise RemoteResource::HTTPCertError.new(request, response)
153
- when 496 then
166
+ when 496
154
167
  raise RemoteResource::HTTPNoCert.new(request, response)
155
- when 497 then
168
+ when 497
156
169
  raise RemoteResource::HTTPToHTTPS.new(request, response)
157
- when 499 then
170
+ when 499
158
171
  raise RemoteResource::HTTPClientClosedRequest.new(request, response)
159
- when 400..499 then
172
+ when 400..499
160
173
  raise RemoteResource::HTTPClientError.new(request, response)
161
- when 500..599 then
174
+ when 500..599
162
175
  raise RemoteResource::HTTPServerError.new(request, response)
163
176
  else
164
177
  raise RemoteResource::HTTPError.new(request, response)
@@ -16,6 +16,10 @@ module RemoteResource
16
16
  @connection_response.success?
17
17
  end
18
18
 
19
+ def timed_out?
20
+ @connection_response.timed_out?
21
+ end
22
+
19
23
  def unprocessable_entity?
20
24
  response_code == 422
21
25
  end
@@ -1,3 +1,3 @@
1
1
  module RemoteResource
2
- VERSION = '1.1.2'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
@@ -6,8 +6,8 @@ require 'remote_resource/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'ddy_remote_resource'
8
8
  spec.version = RemoteResource::VERSION
9
- spec.authors = ['Jan van der Pas']
10
- spec.email = ['jvanderpas@digidentity.eu']
9
+ spec.authors = ['Digidentity', 'Jan van der Pas']
10
+ spec.email = ['development@digidentity.com']
11
11
  spec.summary = %q{RemoteResource, a gem to use resources with REST services.}
12
12
  spec.description = %q{RemoteResource, a gem to use resources with REST services. A replacement for ActiveResource gem.}
13
13
  spec.homepage = ''
@@ -168,7 +168,9 @@ RSpec.describe RemoteResource::Request do
168
168
  let(:expected_connection_options) { request.connection_options }
169
169
 
170
170
  it 'makes a GET request with the connection_options[:params] as query' do
171
- expect(connection).to receive(:get).with(expected_request_url, params: expected_params, body: expected_body, headers: expected_headers).and_call_original
171
+ expect(connection).to receive(:get).with(expected_request_url, params: expected_params,
172
+ body: expected_body, headers: expected_headers,
173
+ connecttimeout: 30, timeout: 120).and_call_original
172
174
  request.perform
173
175
  end
174
176
 
@@ -191,7 +193,9 @@ RSpec.describe RemoteResource::Request do
191
193
  let(:expected_connection_options) { request.connection_options }
192
194
 
193
195
  it 'makes a PUT request with the attributes as body' do
194
- expect(connection).to receive(:put).with(expected_request_url, params: expected_params, body: expected_body, headers: expected_headers).and_call_original
196
+ expect(connection).to receive(:put).with(expected_request_url, params: expected_params,
197
+ body: expected_body, headers: expected_headers,
198
+ connecttimeout: 30, timeout: 120).and_call_original
195
199
  request.perform
196
200
  end
197
201
 
@@ -214,7 +218,9 @@ RSpec.describe RemoteResource::Request do
214
218
  let(:expected_connection_options) { request.connection_options }
215
219
 
216
220
  it 'makes a PATCH request with the attributes as body' do
217
- expect(connection).to receive(:patch).with(expected_request_url, params: expected_params, body: expected_body, headers: expected_headers).and_call_original
221
+ expect(connection).to receive(:patch).with(expected_request_url, params: expected_params,
222
+ body: expected_body, headers: expected_headers,
223
+ connecttimeout: 30, timeout: 120).and_call_original
218
224
  request.perform
219
225
  end
220
226
 
@@ -237,7 +243,9 @@ RSpec.describe RemoteResource::Request do
237
243
  let(:expected_connection_options) { request.connection_options }
238
244
 
239
245
  it 'makes a POST request with the attributes as body' do
240
- expect(connection).to receive(:post).with(expected_request_url, params: expected_params, body: expected_body, headers: expected_headers).and_call_original
246
+ expect(connection).to receive(:post).with(expected_request_url, params: expected_params,
247
+ body: expected_body, headers: expected_headers,
248
+ connecttimeout: 30, timeout: 120).and_call_original
241
249
  request.perform
242
250
  end
243
251
 
@@ -260,7 +268,9 @@ RSpec.describe RemoteResource::Request do
260
268
  let(:expected_connection_options) { request.connection_options }
261
269
 
262
270
  it 'makes a DELETE request with the connection_options[:params] as query' do
263
- expect(connection).to receive(:delete).with(expected_request_url, params: expected_params, body: expected_body, headers: expected_headers).and_call_original
271
+ expect(connection).to receive(:delete).with(expected_request_url, params: expected_params,
272
+ body: expected_body, headers: expected_headers,
273
+ connecttimeout: 30, timeout: 120).and_call_original
264
274
  request.perform
265
275
  end
266
276
 
@@ -586,10 +596,37 @@ RSpec.describe RemoteResource::Request do
586
596
  end
587
597
  end
588
598
 
599
+ describe '#timeout_options' do
600
+ it 'is not given by default' do
601
+ expect(request.connection_options).not_to include :connecttimeout, :timeout
602
+ end
603
+
604
+ context 'with custom timeouts' do
605
+ let(:connection_options) do
606
+ { connecttimeout: 1, timeout: 2 }
607
+ end
608
+
609
+ it 'sets the timeouts from connection_options' do
610
+ aggregate_failures do
611
+ expect(request.connection_options[:connecttimeout]).to eq 1
612
+ expect(request.connection_options[:timeout]).to eq 2
613
+ end
614
+ end
615
+ end
616
+ end
617
+
589
618
  describe '#raise_http_error' do
590
- let(:connection_response) { instance_double(Typhoeus::Response, request: instance_double(Typhoeus::Request)) }
619
+ let(:connection_response) { instance_double(Typhoeus::Response, request: instance_double(Typhoeus::Request), timed_out?: false) }
591
620
  let(:response) { RemoteResource::Response.new(connection_response, connection_options) }
592
621
 
622
+ context 'when the response has timed out' do
623
+ let(:connection_response) { instance_double(Typhoeus::Response, request: instance_double(Typhoeus::Request), timed_out?: true) }
624
+
625
+ it 'raises a RemoteResource::HTTPRequestTimeout' do
626
+ expect { request.send(:raise_http_error, request, response) }.to raise_error RemoteResource::HTTPRequestTimeout
627
+ end
628
+ end
629
+
593
630
  context 'when the response code is 301, 302, 303 or 307' do
594
631
  response_codes = [301, 302, 303, 307]
595
632
  response_codes.each do |response_code|
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe RemoteResource::VERSION do
4
- it { is_expected.to eql '1.1.2' }
4
+ it { is_expected.to eql '1.2.0' }
5
5
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddy_remote_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
+ - Digidentity
7
8
  - Jan van der Pas
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2020-01-28 00:00:00.000000000 Z
12
+ date: 2021-01-08 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
@@ -206,22 +207,22 @@ dependencies:
206
207
  name: typhoeus
207
208
  requirement: !ruby/object:Gem::Requirement
208
209
  requirements:
209
- - - ">="
210
- - !ruby/object:Gem::Version
211
- version: 0.7.0
212
210
  - - "~>"
213
211
  - !ruby/object:Gem::Version
214
212
  version: '0.7'
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: 0.7.0
215
216
  type: :runtime
216
217
  prerelease: false
217
218
  version_requirements: !ruby/object:Gem::Requirement
218
219
  requirements:
219
- - - ">="
220
- - !ruby/object:Gem::Version
221
- version: 0.7.0
222
220
  - - "~>"
223
221
  - !ruby/object:Gem::Version
224
222
  version: '0.7'
223
+ - - ">="
224
+ - !ruby/object:Gem::Version
225
+ version: 0.7.0
225
226
  - !ruby/object:Gem::Dependency
226
227
  name: request_store
227
228
  requirement: !ruby/object:Gem::Requirement
@@ -239,7 +240,7 @@ dependencies:
239
240
  description: RemoteResource, a gem to use resources with REST services. A replacement
240
241
  for ActiveResource gem.
241
242
  email:
242
- - jvanderpas@digidentity.eu
243
+ - development@digidentity.com
243
244
  executables: []
244
245
  extensions: []
245
246
  extra_rdoc_files: []
@@ -319,7 +320,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
319
320
  - !ruby/object:Gem::Version
320
321
  version: '0'
321
322
  requirements: []
322
- rubygems_version: 3.0.4
323
+ rubyforge_project:
324
+ rubygems_version: 2.7.6
323
325
  signing_key:
324
326
  specification_version: 4
325
327
  summary: RemoteResource, a gem to use resources with REST services.