Class: Itrigga::NetHelper::Typhoeus

Inherits:
Object
  • Object
show all
Defined in:
lib/itrigga/net_helper/typhoeus.rb

Class Method Summary collapse

Class Method Details

.get(opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/itrigga/net_helper/typhoeus.rb', line 5

def self.get( opts={} )
  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
      raise ::TimeoutError.new("Timed out request to #{opts[:url]} after #{opts[:timeout]}ms") 
    elsif response.code == 0
      # Could not get an http response, something's wrong.
      raise IOError.new(response.curl_error_message)
    else
      # Received a non-successful http response.
      raise IOError.new("HTTP request failed: " + response.code.to_s)
    end
  end
  
  hydra = ::Typhoeus::Hydra.new
  hydra.queue(request)
  hydra.run
end