Sha256: 90019c73cfeae12ed535a93801a0b218a2519603bc27e14bb6d7e7ebd4cf5c57

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module Itrigga
  module NetHelper
    class Typhoeus
   
      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
  
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
itrigga-net_helper-0.3.0 lib/itrigga/net_helper/typhoeus.rb