diff --git a/lib/httparty/connection_adapter.rb b/lib/httparty/connection_adapter.rb index fddcf038..fa7ae643 100644 --- a/lib/httparty/connection_adapter.rb +++ b/lib/httparty/connection_adapter.rb @@ -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 @@ -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 @@ -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 @@ -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