class Typhoeus::Request

This class represents a request.

@example (see initialize)

@example Make a request with the shortcut.

response = Typhoeus.get("www.example.com")

@see (see initialize)

Attributes

base_url[RW]

Returns the provided base url.

@return [ String ]

block_connection[RW]

@return [ Boolean ]

@api private

hydra[RW]

Returns the hydra in which the request ran, if any.

@return [ Typhoeus::Hydra ]

@api private

options[RW]

Returns options, which includes default parameters.

@return [ Hash ]

original_options[RW]

Returns the original options provided.

@return [ Hash ]

@api private

Public Class Methods

new(base_url, options = {}) click to toggle source

Creates a new request.

@example Simplest request.

response = Typhoeus::Request.new("www.example.com").run

@example Request with url parameters.

response = Typhoeus::Request.new(
  "www.example.com",
  params: {a: 1}
).run

@example Request with a body.

response = Typhoeus::Request.new(
  "www.example.com",
  body: {b: 2}
).run

@example Request with parameters and body.

response = Typhoeus::Request.new(
  "www.example.com",
  params: {a: 1},
  body: {b: 2}
).run

@example Create a request and allow follow redirections.

response = Typhoeus::Request.new(
  "www.example.com",
  followlocation: true
).run

@param [ String ] base_url The url to request. @param [ options ] options The options.

@option options [ Hash ] :params Translated

into url parameters.

@option options [ Hash ] :body Translated

into HTTP POST request body.

@return [ Typhoeus::Request ] The request.

@note See {rubydoc.info/github/typhoeus/ethon/Ethon/Easy/Options Ethon::Easy::Options} for more options.

@see Typhoeus::Hydra @see Typhoeus::Response @see Typhoeus::Request::Actions

# File lib/typhoeus/request.rb, line 113
def initialize(base_url, options = {})
  @base_url = base_url
  @original_options = options
  @options = options.dup

  set_defaults
end

Public Instance Methods

cache_key() click to toggle source

Returns a cache key for use with caching methods that required a string for a key. Will get used by ActiveSupport::Cache stores automatically.

@return [ String ] The cache key.

# File lib/typhoeus/request.rb, line 165
def cache_key
  Digest::SHA1.hexdigest "#{self.class.name}#{base_url}#{hashable_string_for(options)}"
end
encoded_body() click to toggle source

Mimics libcurls POST body generation. This is not accurate, but good enough for VCR.

@return [ String ] The encoded body.

otherwise.

@api private

# File lib/typhoeus/request.rb, line 176
def encoded_body
  Ethon::Easy::Form.new(nil, options[:body]).to_s
end
eql?(other) click to toggle source

Returns whether other is equal to self.

@example Are request equal?

request.eql?(other_request)

@param [ Object ] other The object to check.

@return [ Boolean ] Returns true if equal, else false.

@api private

# File lib/typhoeus/request.rb, line 146
def eql?(other)
  self.class == other.class &&
    self.base_url == other.base_url &&
    fuzzy_hash_eql?(self.options, other.options)
end
hash() click to toggle source

Overrides Object#hash.

@return [ Integer ] The integer representing the request.

@api private

# File lib/typhoeus/request.rb, line 157
def hash
  Zlib.crc32 cache_key
end
url() click to toggle source

Return the url. In contrast to base_url which returns the value you specified, url returns the full url including the parameters.

@example Get the url.

request.url

@since 0.5.5

# File lib/typhoeus/request.rb, line 129
def url
  easy = EasyFactory.new(self).get
  url = easy.url
  Typhoeus::Pool.release(easy)
  url
end

Private Instance Methods

fuzzy_hash_eql?(left, right) click to toggle source

Checks if two hashes are equal or not, discarding first-level hash order.

@param [ Hash ] left @param [ Hash ] right hash to check for equality

@return [ Boolean ] Returns true if hashes have

same values for same keys and same length,
even if the keys are given in a different order.
# File lib/typhoeus/request.rb, line 191
def fuzzy_hash_eql?(left, right)
  return true if (left == right)

  (left.count == right.count) && left.inject(true) do |res, kvp|
    res && (kvp[1] == right[kvp[0]])
  end
end
hashable_string_for(obj) click to toggle source
# File lib/typhoeus/request.rb, line 199
def hashable_string_for(obj)
  case obj
  when Hash
    hashable_string_for(obj.sort_by {|sub_obj| sub_obj.first.to_s})
  when Array
    obj.map {|sub_obj| hashable_string_for(sub_obj)}.to_s
  else
    obj.to_s
  end
end
set_defaults() click to toggle source

Sets default header and verbose when turned on.

# File lib/typhoeus/request.rb, line 211
def set_defaults
  default_user_agent = Config.user_agent || Typhoeus::USER_AGENT

  options[:headers] = {'User-Agent' => default_user_agent}.merge(options[:headers] || {})
  options[:headers]['Expect'] ||= ''
  options[:verbose] = Typhoeus::Config.verbose if options[:verbose].nil? && !Typhoeus::Config.verbose.nil?
  options[:timeout] = Typhoeus::Config.timeout if options[:timeout].nil? && !Typhoeus::Config.timeout.nil?
  options[:connecttimeout] = Typhoeus::Config.connecttimeout if options[:connecttimeout].nil? && !Typhoeus::Config.connecttimeout.nil?
  options[:maxredirs] ||= 50
  options[:proxy] = Typhoeus::Config.proxy unless options.has_key?(:proxy) || Typhoeus::Config.proxy.nil?
end