class Typhoeus::Request
def self.delete(url, params = {})
def self.delete(url, params = {}) run(url, params.merge(:method => :delete)) end
def self.get(url, params = {})
def self.get(url, params = {}) run(url, params.merge(:method => :get)) end
def self.head(url, params = {})
def self.head(url, params = {}) run(url, params.merge(:method => :head)) end
def self.options
def self.options ACCESSOR_OPTIONS end
def self.post(url, params = {})
def self.post(url, params = {}) run(url, params.merge(:method => :post)) end
def self.put(url, params = {})
def self.put(url, params = {}) run(url, params.merge(:method => :put)) end
def self.run(url, params)
def self.run(url, params) r = new(url, params) Typhoeus::Hydra.hydra.queue r Typhoeus::Hydra.hydra.run r.response end
def after_complete(&block)
def after_complete(&block) @after_complete = block end
def after_complete=(proc)
def after_complete=(proc) @after_complete = proc end
def cache_key
def cache_key Digest::SHA1.hexdigest(cache_key_basis || url) end
def call_after_complete
def call_after_complete @after_complete.call(@handled_response) if @after_complete end
def call_handlers
def call_handlers if @on_complete @handled_response = @on_complete.call(response) call_after_complete end end
def handled_response
def handled_response @handled_response || response end
def handled_response=(val)
def handled_response=(val) @handled_response = val end
def host
def host slash_location = @url.index('/', 8) if slash_location @url.slice(0, slash_location) else query_string_location = @url.index('?') return query_string_location ? @url.slice(0, query_string_location) : @url end end
def host_domain
def host_domain parsed_uri.host end
def initialize(url, options = {})
** +:auth_method
** +:password
** +:username
** +:verbose
** +:ssl_capath
** +:ssl_cacert
** +:ssl_key_password
** +:ssl_key_type
** +:ssl_key
** +:ssl_cert_type
** +:ssl_cert
** +:disable_ssl_host_verification
** +:disable_ssl_peer_verification
** +:proxy
** +:max_redirects
** +:follow_location
** +:cache_timeout+ : cache timeout (ms)
** +:headers+ : headers as Hash
** +:connect_timeout+ : connect timeout (ms)
** +:interface+ : interface or ip address (string)
** +:timeout+ : timeout (ms)
** +:body+
** +:params+ : params as a Hash
** +:method+ : :get (default) / :post / :put
* +options+ : A hash containing options among :
* +url+ : Endpoint (URL) of the request
Options:
Initialize a new Request
def initialize(url, options = {}) @url = url @method = options[:method] || :get @params = options[:params] @body = options[:body] @timeout = safe_to_i(options[:timeout]) @connect_timeout = safe_to_i(options[:connect_timeout]) @interface = options[:interface] @headers = options[:headers] || {} @cache_timeout = safe_to_i(options[:cache_timeout]) @follow_location = options[:follow_location] @max_redirects = options[:max_redirects] @proxy = options[:proxy] @proxy_type = options[:proxy_type] @proxy_username = options[:proxy_username] @proxy_password = options[:proxy_password] @proxy_auth_method = options[:proxy_auth_method] @disable_ssl_peer_verification = options[:disable_ssl_peer_verification] @disable_ssl_host_verification = options[:disable_ssl_host_verification] @ssl_cert = options[:ssl_cert] @ssl_cert_type = options[:ssl_cert_type] @ssl_key = options[:ssl_key] @ssl_key_type = options[:ssl_key_type] @ssl_key_password = options[:ssl_key_password] @ssl_cacert = options[:ssl_cacert] @ssl_capath = options[:ssl_capath] @ssl_version = options[:ssl_version] @verbose = options[:verbose] @username = options[:username] @password = options[:password] @auth_method = options[:auth_method] @on_complete = nil @after_complete = nil @handled_response = nil end
def inspect
def inspect result = ":method => #{self.method.inspect},\n" << "\t:url => #{URI.parse(self.url).to_s}" if self.body and !self.body.empty? result << ",\n\t:body => #{self.body.inspect}" end if self.params and !self.params.empty? result << ",\n\t:params => #{self.params.inspect}" end if self.headers and !self.headers.empty? result << ",\n\t:headers => #{self.headers.inspect}" end result end
def localhost?
def localhost? LOCALHOST_ALIASES.include?(parsed_uri.host) end
def marshal_dump
`on_complete` and `after_complete` handlers, since they cannot be
Return the important data needed to serialize this Request, except the
def marshal_dump (instance_variables - ['@on_complete', '@after_complete', :@on_complete, :@after_complete]).map do |name| [name, instance_variable_get(name)] end end
def marshal_load(attributes)
def marshal_load(attributes) attributes.each { |name, value| instance_variable_set(name, value) } end
def on_complete(&block)
def on_complete(&block) @on_complete = block end
def on_complete=(proc)
def on_complete=(proc) @on_complete = proc end
def params_string
def params_string return nil unless params traversal = Typhoeus::Utils.traverse_params_hash(params) Typhoeus::Utils.traversal_to_param_string(traversal) end
def parsed_uri
def parsed_uri @parsed_uri ||= URI.parse(@url) end
def safe_to_i(value)
def safe_to_i(value) return value if value.is_a?(Fixnum) return nil if value.nil? return nil if value.respond_to?(:empty?) && value.empty? value.to_i end
def url
def url if [:post, :put].include?(@method) @url else url = "#{@url}?#{params_string}" url += "&#{URI.escape(@body)}" if @body url.gsub("?&", "?").gsub(/\?$/, '') end end
def user_agent
def user_agent headers['User-Agent'] end