lib/itrigga/net_helper.rb in itrigga-net_helper-0.1.0 vs lib/itrigga/net_helper.rb in itrigga-net_helper-0.2.0
- old
+ new
@@ -9,11 +9,15 @@
module NetHelper
module_function
def typhoeus_present?
- defined?(Typhoeus)
+ if defined?(Typhoeus)
+ true
+ else
+ false
+ end
end
def do_get(url, time_out=5, retries_on_timeout=5, max_redirects = 3)
retrycount = 0
resp = nil
@@ -32,64 +36,64 @@
end
end
def typhoeus_request( opts={} )
- opts[:timeout] ||= 30000 # ms
+ opts[:timeout] ||= 30
+ opts[:timeout] *= 1000 # Typhoeus does its timeouts in ms
opts[:follow_location] ||= true
opts[:disable_ssl_peer_verification] = true if opts[:disable_ssl_peer_verification].nil?
request = Typhoeus::Request.new(opts[:url], opts)
request.on_complete do |response|
if response.success?
return response.body
elsif response.timed_out?
# aw hell no
- log("got a time out")
+ raise IOError.new("Timed out request to #{opts[:url]} after #{opts[:timeout]}ms")
elsif response.code == 0
# Could not get an http response, something's wrong.
- log(response.curl_error_message)
+ raise IOError.new(response.curl_error_message)
else
# Received a non-successful http response.
- log("HTTP request failed: " + response.code.to_s)
+ raise IOError.new("HTTP request failed: " + response.code.to_s)
end
end
+ hydra = Typhoeus::Hydra.new
+ hydra.queue(request)
+ hydra.run
end
- def get_response( url )
+ def get_response( url, timeout=nil )
if typhoeus_present?
- typhoeus_request( url )
+ typhoeus_request( :url=>url, :timeout=>timeout )
else
Net::HTTP.get_response(URI.parse(url))
end
end
def get_with_timeout( url, time_out)
resp = nil
if defined?(SystemTimer)
resp = SystemTimer.timeout_after(time_out) do
- get_response(url)
+ get_response(url, time_out)
end
else
resp = timeout(time_out) do
- get_response(url)
+ get_response(url, time_out)
end
end
resp
end
- resp = timeout(time_out) do
-
- end
- resp
- end
def handle_response( url, resp, retries_on_timeout=5, max_redirects = 3 )
if resp.is_a? Net::HTTPSuccess then resp.body
+ elsif resp.is_a? String then resp
elsif resp.is_a? Net::HTTPRedirection
if max_redirects > 0
do_get( URI.parse(url).merge(resp['location']).to_s, retries_on_timeout, max_redirects - 1 )
else
raise IOError.new("too many redirects!")
@@ -108,12 +112,18 @@
do_get(opts[:url], opts[:timeout], opts[:retries_on_timeout], opts[:max_redirects])
else
retrycount = 0
resp = begin
- timeout( opts[:timeout] ) do
- raw_get(opts)
+ if defined?(SystemTimer)
+ SystemTimer.timeout_after(opts[:timeout]) do
+ raw_get(opts)
+ end
+ else
+ timeout( opts[:timeout] ) do
+ raw_get(opts)
+ end
end
rescue TimeoutError
if(retrycount < opts[:retries_on_timeout])
retrycount+=1
retry
@@ -124,9 +134,14 @@
resp
end
end
def raw_get(opts)
+
+ if typhoeus_present?
+ return typhoeus_request(opts)
+ end
+
resp = nil
establish_session_if_needed(opts)
if opts[:username]
resp = get_with_auth(opts)