Skip to content

Commit

Permalink
Adding hash params availability for timeout config (umbrellio#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharombolla authored and tycooon committed Oct 28, 2019
1 parent 1afbb80 commit 2396c17
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ inherit_gem:

AllCops:
DisplayCopNames: true
TargetRubyVersion: 2.3
TargetRubyVersion: 2.4
Include:
- bin/console
- Gemfile
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: ruby
sudo: false

rvm:
- 2.3
- 2.4
- 2.5
- 2.6
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Valid client options are:
- `on_retry` – callback called on request retry
- `retry_exceptions` – an array of exception classes to retry
- `ssl_context` – ssl context for requests (an `OpenSSL::SSL::SSLContext` instance)
- `timeout` – timeout for requests in seconds
- `timeout` – timeout for requests in seconds or hash like `{ read: 5, write: 5, connect: 1 }`
- `follow` - enable following redirects (`true` or hash with options – e.g. `{ max_hops: 1, strict: false}`)

All these options are passed to each request made by this client but can be overriden on per-request basis.
Expand Down
2 changes: 1 addition & 1 deletion ezclient.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "ezclient/version"

Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 2.3.8"
spec.required_ruby_version = ">= 2.4.0"

spec.name = "ezclient"
spec.version = EzClient::VERSION
Expand Down
7 changes: 6 additions & 1 deletion lib/ezclient/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ def retry_on_connection_error
end

def timeout
options[:timeout]&.to_f
case options[:timeout]
when Hash
options[:timeout].transform_values! { |value| value&.to_f }
else
options[:timeout]&.to_f
end
end

def on_complete
Expand Down
23 changes: 18 additions & 5 deletions spec/ezclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,26 @@ def self.sign!(*); end
end

context "when timeout client option is provided" do
let(:client_options) { Hash[timeout: 10] }
let(:timeout) { 10.0 }
let(:opts) { request.http_options }

it "uses it for request" do
expect(opts.timeout_class).to eq(HTTP::Timeout::Global)
expect(opts.timeout_options).to eq(global_timeout: timeout)
context "when timeout like integer" do
let(:client_options) { Hash[timeout: 10] }
let(:timeout) { 10.0 }

it "uses it for request" do
expect(opts.timeout_class).to eq(HTTP::Timeout::Global)
expect(opts.timeout_options).to eq(global_timeout: timeout)
end
end

context "when timeout like hash" do
let(:client_options) { Hash[timeout: Hash[read: 5, write: 5, connect: 1]] }
let(:expected_configs) { { write_timeout: 5.0, read_timeout: 5.0, connect_timeout: 1.0 } }

it "uses request option for request" do
expect(opts.timeout_class).to eq(HTTP::Timeout::PerOperation)
expect(opts.timeout_options).to eq(expected_configs)
end
end

context "when timeout request option is provided as well" do
Expand Down

0 comments on commit 2396c17

Please sign in to comment.