Skip to content

Commit

Permalink
Introduce private from_ruby_version method to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
springerigor committed Feb 19, 2019
1 parent 1b35b88 commit 4fb8646
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/httparty/connection_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def connection
http.open_timeout = options[:timeout]
http.read_timeout = options[:timeout]

if RUBY_VERSION >= "2.6.0"
from_ruby_version('2.6.0', option: :write_timeout, warn: false) do
http.write_timeout = options[:timeout]
end
end
Expand All @@ -132,10 +132,8 @@ def connection
end

if add_timeout?(options[:write_timeout])
if RUBY_VERSION >= "2.6.0"
from_ruby_version('2.6.0', option: :write_timeout) do
http.write_timeout = options[:write_timeout]
else
Kernel.warn("Warning: option :write_timeout requires Ruby version 2.6.0 or later")
end
end

Expand All @@ -151,18 +149,14 @@ def connection
#
# @see https://bugs.ruby-lang.org/issues/6617
if options[:local_host]
if RUBY_VERSION >= "2.0.0"
from_ruby_version('2.0.0', option: :local_host) do
http.local_host = options[:local_host]
else
Kernel.warn("Warning: option :local_host requires Ruby version 2.0 or later")
end
end

if options[:local_port]
if RUBY_VERSION >= "2.0.0"
from_ruby_version('2.0.0', option: :local_port) do
http.local_port = options[:local_port]
else
Kernel.warn("Warning: option :local_port requires Ruby version 2.0 or later")
end
end

Expand All @@ -171,6 +165,14 @@ def connection

private

def from_ruby_version(ruby_version, option:, warn: true)
if RUBY_VERSION >= ruby_version
yield
elsif warn
Kernel.warn("Warning: option #{ option } requires Ruby version #{ ruby_version } or later")
end
end

def add_timeout?(timeout)
timeout && (timeout.is_a?(Integer) || timeout.is_a?(Float))
end
Expand Down

0 comments on commit 4fb8646

Please sign in to comment.