diff options
author | Mark Longair <mhl@pobox.com> | 2013-11-22 13:27:15 +0000 |
---|---|---|
committer | Henare Degan <henare.degan@gmail.com> | 2013-11-22 16:39:44 -0200 |
commit | ea8b8e7cdae9d2a2596df0a643ea4921e17b628f (patch) | |
tree | 41de025c3a4ea3b6f18ac510b4b7bc4f5425e390 | |
parent | 3b86cb6129140fc123dc3aeffcccdb5652f19085 (diff) |
Fix quietly_try_to_purge on Ruby 2.0
The tests of quietly_try_to_purge were failing on Ruby 2.0 due to the
net-http-local gem not working with that Ruby version. When
Net::HTTP::bind is called, it temporarily replaces the open method of
TCPSocket with a version of open that only takes two parameters. The
Ruby 1.9 version of net/http.rb calls TCPSocket.open with two
parameters, but the Ruby 2.0 version calls it with 4, and so fails with
a mismatched number of arguments error.
In fact, net-http-local doesn't seem to be necessary with Ruby 2.0,
where one can supply a :local_port argument to Net::HTTP.start, so this
commit patches lib/quiet_opener.rb to use that approach with Ruby
>= 2.0, and net-http-local on earlier versions.
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | lib/quiet_opener.rb | 37 |
2 files changed, 30 insertions, 9 deletions
@@ -20,7 +20,7 @@ gem 'fastercsv', '>=1.5.5' gem 'jquery-rails', '~> 2.1' gem 'json' gem 'mahoro' -gem 'net-http-local' +gem 'net-http-local', :platforms => [:ruby_18, :ruby_19] gem 'net-purge' gem 'newrelic_rpm' gem 'rack' diff --git a/lib/quiet_opener.rb b/lib/quiet_opener.rb index ae6605c43..16ea27b8e 100644 --- a/lib/quiet_opener.rb +++ b/lib/quiet_opener.rb @@ -1,6 +1,8 @@ require 'open-uri' require 'net-purge' -require 'net/http/local' +if RUBY_VERSION.to_f < 2.0 + require 'net/http/local' +end def quietly_try_to_open(url) begin @@ -12,17 +14,36 @@ def quietly_try_to_open(url) return result end +# On Ruby versions before 2.0, we need to use the net-http-local gem +# to force the use of 127.0.0.1 as the local interface for the +# connection. However, at the time of writing this gem doesn't work +# on Ruby 2.0 and it's not necessary with that Ruby version - one can +# supply a :local_host option to Net::HTTP:start. So, this helper +# function is to abstract away that difference, and can be used as you +# would Net::HTTP.start(host) when passed a block. +def http_from_localhost(host) + if RUBY_VERSION.to_f >= 2.0 + Net::HTTP.start(host, :local_host => '127.0.0.1') do |http| + yield http + end + else + Net::HTTP.bind '127.0.0.1' do + Net::HTTP.start(host) do |http| + yield http + end + end + end +end + def quietly_try_to_purge(host, url) begin result = "" result_body = "" - Net::HTTP.bind '127.0.0.1' do - Net::HTTP.start(host) {|http| - request = Net::HTTP::Purge.new(url) - response = http.request(request) - result = response.code - result_body = response.body - } + http_from_localhost(host) do |http| + request = Net::HTTP::Purge.new(url) + response = http.request(request) + result = response.code + result_body = response.body end rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET, Errno::ENETUNREACH Rails.logger.warn("PURGE: Unable to reach host #{host}") |