module Typhoeus

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 {github.com/myronmarston/vcr VCR}, {github.com/bblimke/webmock Webmock} or {github.com/technoweenie/faraday Faraday}. VCR and Webmock are providing their own adapter whereas Faraday relies on {Faraday::Adapter::Typhoeus} since Typhoeus version 0.5.

@example (see Typhoeus::Request) @example (see Typhoeus::Hydra)

@see Typhoeus::Request @see Typhoeus::Hydra @see Faraday::Adapter::Typhoeus

@since 0.5.0

Constants

USER_AGENT

The default typhoeus user agent.

VERSION

The current Typhoeus version.

Public Instance Methods

before(&block) click to toggle source

Add before callbacks.

@example Add before callback.

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

@param [ Block ] block The callback.

@yield [ Typhoeus::Request ]

@return [ Array<Block> ] All before blocks.

# File lib/typhoeus.rb, line 95
def before(&block)
  @before ||= []
  @before << block if block_given?
  @before
end
configure() { |Config| ... } click to toggle source

Set the Typhoeus configuration options by passing a block.

@example (see Typhoeus::Config)

@yield [ Typhoeus::Config ]

@return [ Typhoeus::Config ] The configuration.

@see Typhoeus::Config

# File lib/typhoeus.rb, line 62
def configure
  yield Config
end
stub(base_url, options = {}) click to toggle source

Stub out specific request.

@example (see Typhoeus::Expectation)

@param [ String ] base_url The url to stub out. @param [ Hash ] options The options to stub out.

@return [ Typhoeus::Expectation ] The expecatation.

@see Typhoeus::Expectation

# 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_connection() { || ... } click to toggle source

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

@example 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

@param [ Block ] block The block to execute.

@return [ Object ] Returns the return value of block.

@see Typhoeus::Config#block_connection

# 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