Home

Awesome

Faraday Typhoeus Adapter

This is a Faraday 2 adapter for the Typhoeus parallel HTTP client. It supports parallel HTTP requests and streaming.

Installation

Add this line to your application's Gemfile:

gem 'faraday-typhoeus'

And then execute:

$ bundle install

Or install it yourself with gem install faraday-typhoeus and require it in your ruby code with require 'faraday/typhoeus'

Usage

Basic

conn = Faraday.new(...) do |f|
  f.adapter :typhoeus
end

Typhoeus Options

You can also include options for Typhoeus/Ethon that will be used in every request:

Note that block-style configuration for the adapter is not currently supported.

conn = Faraday.new(...) do |f|
  f.adapter :typhoeus, forbid_reuse: true, maxredirs: 1
end

Parallel Requests

The adapter supports Typhoeus's parallel request functionality:

conn = Faraday.new(...) do |f|
  f.request  :url_encoded
  f.response :logger
  f.adapter  :typhoeus
end

responses = []

conn.in_parallel do
  # responses[0].body will be null here since the requests have not been 
  # completed
  responses = [
    conn.get('/first'), 
    conn.get('/second'),
  ]
end

# after it has been completed the response information is fully available in
# response[0].status, etc
responses[0].body
responses[1].body

Streaming Responses

The adapter supports streamed responses via the on_data option:

conn = Faraday.new(...) do |f|
  f.adapter :typhoeus
end

# Streaming

chunks = []

conn.get('/stream') do |req|
  req.options.on_data proc do |chunk, received_bytes|
    chunks << chunk
  end
end

body = chunks.join

# Server-Sent Event Polling

body = nil

begin
  conn.get('/events') do |req|
    req.options.timeout = 30

    req.options.on_data = proc do |chunk|
      # stop listening once we get some data (YMMV)
      if chunk.start_with?('data: ')
        body = chunk
        :abort # abort the request, we're done
      end
    end
  end
rescue Faraday::ConnectionFailed => ex
  raise ex unless body
end

Resources

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

TODO

Contributing

Bug reports and pull requests are welcome on GitHub.

License

The gem is available as open source under the terms of the license.