Module: Typhoeus

Extended by:
Request::Actions, Request::Callbacks::Types
Defined in:
lib/typhoeus.rb,
lib/typhoeus/pool.rb,
lib/typhoeus/hydra.rb,
lib/typhoeus/errors.rb,
lib/typhoeus/config.rb,
lib/typhoeus/request.rb,
lib/typhoeus/version.rb,
lib/typhoeus/response.rb,
lib/typhoeus/expectation.rb,
lib/typhoeus/hydra/before.rb,
lib/typhoeus/easy_factory.rb,
lib/typhoeus/hydra/addable.rb,
lib/typhoeus/hydra/runnable.rb,
lib/typhoeus/request/before.rb,
lib/typhoeus/errors/no_stub.rb,
lib/typhoeus/response/header.rb,
lib/typhoeus/hydra/queueable.rb,
lib/typhoeus/hydra/stubbable.rb,
lib/typhoeus/request/marshal.rb,
lib/typhoeus/request/actions.rb,
lib/typhoeus/hydra/cacheable.rb,
lib/typhoeus/response/status.rb,
lib/typhoeus/hydra/memoizable.rb,
lib/typhoeus/request/cacheable.rb,
lib/typhoeus/request/callbacks.rb,
lib/typhoeus/request/stubbable.rb,
lib/typhoeus/request/operations.rb,
lib/typhoeus/request/streamable.rb,
lib/typhoeus/response/cacheable.rb,
lib/typhoeus/request/memoizable.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 provide 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

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.8'

Class Method Summary collapse

Methods included from Request::Callbacks::Types

on_complete, on_failure, on_headers, on_success

Methods included from Request::Actions

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

Class 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



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

def self.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 directly.

Typhoeus::Config.verbose = true

Yields:

Returns:

See Also:

Since:

  • 0.5.0



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

def self.configure
  yield Config
end

.stub(base_url, options = {}, &block) ⇒ Typhoeus::Expectation

Stub out a 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

Stub a request and get a lazily-constructed response containing data from actual widgets that exist in the system when the stubbed request is made.

Typhoeus.stub("www.example.com/widgets") do
  actual_widgets = Widget.all
  Typhoeus::Response.new(
    :body => actual_widgets.inject([]) do |ids, widget|
      ids << widget.id
    end.join(",")
  )
end

Stub a request and get a lazily-constructed response in the format requested.

Typhoeus.stub("www.example.com") do |request|
  accept = (request.options[:headers]||{})['Accept'] || "application/json"
  format = accept.split(",").first
  body_obj = { 'things' => [ { 'id' => 'foo' } ] }

  Typhoeus::Response.new(
    :headers => {
      'Content-Type' => format
    },
    :body => SERIALIZERS[format].serialize(body_obj)
  )
end

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



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

def self.stub(base_url, options = {}, &block)
  expectation = Expectation.all.find{ |e| e.base_url == base_url && e.options == options }
  if expectation.nil?
    expectation = Expectation.new(base_url, options)
    Expectation.all << expectation
  end

  expectation.and_return(&block) unless block.nil?
  expectation
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 it's 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 the block.

See Also:

Since:

  • 0.5.0



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

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