Module: Typhoeus

Extended by:
Typhoeus, Request::Actions, Request::Callbacks::Types
Included in:
Typhoeus
Defined in:
lib/typhoeus.rb,
lib/typhoeus/pool.rb,
lib/typhoeus/hydra.rb,
lib/typhoeus/config.rb,
lib/typhoeus/errors.rb,
lib/typhoeus/request.rb,
lib/typhoeus/version.rb,
lib/typhoeus/response.rb,
lib/typhoeus/expectation.rb,
lib/typhoeus/easy_factory.rb,
lib/typhoeus/hydra/before.rb,
lib/typhoeus/hydra/addable.rb,
lib/typhoeus/errors/no_stub.rb,
lib/typhoeus/hydra/runnable.rb,
lib/typhoeus/request/before.rb,
lib/typhoeus/hydra/queueable.rb,
lib/typhoeus/hydra/stubbable.rb,
lib/typhoeus/request/actions.rb,
lib/typhoeus/request/marshal.rb,
lib/typhoeus/response/header.rb,
lib/typhoeus/response/status.rb,
lib/typhoeus/hydra/memoizable.rb,
lib/typhoeus/request/callbacks.rb,
lib/typhoeus/request/stubbable.rb,
lib/typhoeus/request/memoizable.rb,
lib/typhoeus/request/operations.rb,
lib/typhoeus/request/responseable.rb,
lib/typhoeus/errors/typhoeus_error.rb,
lib/typhoeus/response/informations.rb,
lib/typhoeus/hydra/block_connection.rb,
lib/typhoeus/request/block_connection.rb

Overview

Typhoeus is a http client library based on Ethon which wraps libcurl. Sitting on top of libcurl makes Typhoeus very reliable and fast.

There are some gems using Typhoeus like VCR, Webmock or Faraday. VCR and Webmock are providing their own adapter whereas Faraday relies on Faraday::Adapter::Typhoeus since Typhoeus version 0.5.

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

Use the hydra to do multiple requests.

hydra = Typhoeus::Hydra.new
requests = (0..9).map{ Typhoeus::Request.new("www.example.com") }
requests.each{ |request| hydra.queue(request) }
hydra.run

See Also:

Since:

  • 0.5.0

Defined Under Namespace

Modules: Config, Errors, Pool Classes: EasyFactory, Expectation, Hydra, Request, Response

Constant Summary collapse

USER_AGENT =

The default typhoeus user agent.

Since:

  • 0.5.0

"Typhoeus - https://github.com/typhoeus/typhoeus"
VERSION =

The current Typhoeus version.

Since:

  • 0.5.0

'0.6.1'

Instance Method Summary collapse

Methods included from Request::Actions

delete, get, head, options, patch, post, put

Methods included from Request::Callbacks::Types

on_complete, on_failure, on_success

Instance Method Details

#before(&block) {|Typhoeus::Request| ... } ⇒ Array<Block>

Add before callbacks.

Examples:

Add before callback.

Typhoeus.before { |request| p request.base_url }

Parameters:

  • block (Block)

    The callback.

Yields:

Returns:

  • (Array<Block>)

    All before blocks.

Since:

  • 0.5.0



95
96
97
98
99
# File 'lib/typhoeus.rb', line 95

def before(&block)
  @before ||= []
  @before << block if block_given?
  @before
end

#configure {|Typhoeus::Config| ... } ⇒ Typhoeus::Config

Set the Typhoeus configuration options by passing a block.

Examples:

Set the configuration options within a block.

Typhoeus.configure do |config|
  config.verbose = true
end

Set the configuration direct.

Typhoeus::Config.verbose = true

Yields:

Returns:

See Also:

Since:

  • 0.5.0



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

def configure
  yield Config
end

#stub(base_url, options = {}) ⇒ Typhoeus::Expectation

Stub out specific request.

Examples:

Stub a request and get specified response.

expected = Typhoeus::Response.new
Typhoeus.stub("www.example.com").and_return(expected)

actual = Typhoeus.get("www.example.com")
expected == actual
#=> true

Parameters:

  • base_url (String)

    The url to stub out.

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

    The options to stub out.

Returns:

See Also:

Since:

  • 0.5.0



76
77
78
79
80
81
82
83
# File 'lib/typhoeus.rb', line 76

def stub(base_url, options = {})
  expectation = Expectation.all.find{ |e| e.base_url == base_url && e.options == options }
  return expectation if expectation

  Expectation.new(base_url, options).tap do |new_expectation|
    Expectation.all << new_expectation
  end
end

#with_connectionObject

Execute given block as if block connection is turned off. The old block connection state is restored afterwards.

Examples:

Make a real request, no matter if its blocked.

Typhoeus::Config.block_connection = true
Typhoeus.get("www.example.com").code
#=> raise Typhoeus::Errors::NoStub

Typhoeus.with_connection do
  Typhoeus.get("www.example.com").code
  #=> :ok
end

Parameters:

  • block (Block)

    The block to execute.

Returns:

  • (Object)

    Returns the return value of block.

See Also:

Since:

  • 0.5.0



119
120
121
122
123
124
125
# File 'lib/typhoeus.rb', line 119

def with_connection
  old = Config.block_connection
  Config.block_connection = false
  result = yield if block_given?
  Config.block_connection = old
  result
end