Class: Typhoeus::Request

Inherits:
Object
  • Object
show all
Extended by:
Actions
Includes:
Before, BlockConnection, Cacheable, Callbacks, Callbacks::Types, Marshal, Memoizable, Operations, Responseable, Streamable, Stubbable
Defined in:
lib/typhoeus/request.rb,
lib/typhoeus/request/before.rb,
lib/typhoeus/request/actions.rb,
lib/typhoeus/request/marshal.rb,
lib/typhoeus/request/cacheable.rb,
lib/typhoeus/request/callbacks.rb,
lib/typhoeus/request/stubbable.rb,
lib/typhoeus/request/memoizable.rb,
lib/typhoeus/request/operations.rb,
lib/typhoeus/request/streamable.rb,
lib/typhoeus/request/responseable.rb,
lib/typhoeus/request/block_connection.rb

Overview

This class represents a request.

Examples:

Make a request with the shortcut.

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

Simplest request.

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

Request with url parameters.

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

Request with a body.

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

Request with parameters and body.

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

Create a request and allow follow redirections.

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

See Also:

Since:

  • 0.5.0

Defined Under Namespace

Modules: Actions, Before, BlockConnection, Cacheable, Callbacks, Marshal, Memoizable, Operations, Responseable, Streamable, Stubbable

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Actions

delete, get, head, patch, post, put

Methods included from Before

#run

Methods included from Stubbable

#run

Methods included from BlockConnection

#blocked?, #run

Methods included from Cacheable

#cache_ttl, #cacheable?, #cached_response, #response=, #run

Methods included from Memoizable

#memoizable?, #response=

Methods included from Responseable

#response, #response=

Methods included from Operations

#finish, #run

Methods included from Marshal

#marshal_dump, #marshal_load

Methods included from Streamable

#on_body, #streaming?

Methods included from Callbacks

#execute_callbacks, #execute_headers_callbacks

Methods included from Callbacks::Types

#on_complete, #on_failure, #on_headers, #on_progress, #on_success

Constructor Details

#initialize(base_url, options = {}) ⇒ Typhoeus::Request

Note:

See Ethon::Easy::Options for more options.

Creates a new request.

Examples:

Simplest request.

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

Request with url parameters.

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

Request with a body.

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

Request with parameters and body.

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

Create a request and allow follow redirections.

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

Parameters:

  • base_url (String)

    The url to request.

  • options (options) (defaults to: {})

    The options.

Options Hash (options):

  • :params (Hash)

    Translated into url parameters.

  • :body (Hash)

    Translated into HTTP POST request body.

See Also:

Since:

  • 0.5.0



113
114
115
116
117
118
119
# 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

Instance Attribute Details

#base_urlString

Returns the provided base url.

Returns:

  • (String)

Since:

  • 0.5.0



42
43
44
# File 'lib/typhoeus/request.rb', line 42

def base_url
  @base_url
end

#block_connectionBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 0.5.0



66
67
68
# File 'lib/typhoeus/request.rb', line 66

def block_connection
  @block_connection
end

#hydraTyphoeus::Hydra

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

Since:

  • 0.5.0



54
55
56
# File 'lib/typhoeus/request.rb', line 54

def hydra
  @hydra
end

#optionsHash

Returns options, which includes default parameters.

Returns:

  • (Hash)

Since:

  • 0.5.0



47
48
49
# File 'lib/typhoeus/request.rb', line 47

def options
  @options
end

#original_optionsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the original options provided.

Returns:

  • (Hash)

Since:

  • 0.5.0



61
62
63
# File 'lib/typhoeus/request.rb', line 61

def original_options
  @original_options
end

Instance Method Details

#cache_keyString

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

Returns:

  • (String)

    The cache key.

Since:

  • 0.5.0



165
166
167
# File 'lib/typhoeus/request.rb', line 165

def cache_key
  Digest::SHA1.hexdigest "#{self.class.name}#{base_url}#{hashable_string_for(options)}"
end

#encoded_bodyString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (String)

    The encoded body. otherwise.

Since:

  • 0.5.0



176
177
178
# File 'lib/typhoeus/request.rb', line 176

def encoded_body
  Ethon::Easy::Form.new(nil, options[:body]).to_s
end

#eql?(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether other is equal to self.

Examples:

Are request equal?

request.eql?(other_request)

Parameters:

  • other (Object)

    The object to check.

Returns:

  • (Boolean)

    Returns true if equal, else false.

Since:

  • 0.5.0



146
147
148
149
150
# 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

#hashInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Overrides Object#hash.

Returns:

  • (Integer)

    The integer representing the request.

Since:

  • 0.5.0



157
158
159
# File 'lib/typhoeus/request.rb', line 157

def hash
  Zlib.crc32 cache_key
end

#urlObject

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

Examples:

Get the url.

request.url

Since:

  • 0.5.5



129
130
131
132
133
134
# File 'lib/typhoeus/request.rb', line 129

def url
  easy = EasyFactory.new(self).get
  url = easy.url
  Typhoeus::Pool.release(easy)
  url
end