Class: Faraday::Adapter::EMHttp

### Home Classes Methods

### In Files

- lib/faraday/adapter/em_http.rb

### Parent

Faraday::Adapter

### Namespace

- MODULE Faraday::Adapter::EMHttp::Options
- CLASS Faraday::Adapter::EMHttp::Manager

### Methods

- ::setup_parallel_manager
- #call
- #error_message
- #parallel?
- #perform_request
- #perform_single_request
- #raise_error

### Included Modules

- Faraday::Adapter::EMHttp::Options

### Class/Module Index

Quicksearch

- Faraday
- Faraday::Adapter
- Faraday::Adapter::EMHttp
- Faraday::Adapter::EMHttp::Manager
- Faraday::Adapter::EMHttp::Options
- Faraday::Adapter::EMSynchrony
- Faraday::Adapter::EMSynchrony::ParallelManager
- Faraday::Adapter::Excon
- Faraday::Adapter::NetHttp
- Faraday::Adapter::NetHttpPersistent
- Faraday::Adapter::Parallelism
- Faraday::Adapter::Patron
- Faraday::Adapter::Rack
- Faraday::Adapter::Test
- Faraday::Adapter::Test::Stub
- Faraday::Adapter::Test::Stubs
- Faraday::Adapter::Test::Stubs::NotFound
- Faraday::Adapter::Typhoeus
- Faraday::AutoloadHelper
- Faraday::Builder
- Faraday::Builder::Handler
- Faraday::Builder::StackLocked
- Faraday::CompositeReadIO
- Faraday::Connection
- Faraday::Error
- Faraday::Error::ClientError
- Faraday::Error::ConnectionFailed
- Faraday::Error::MissingDependency
- Faraday::Error::ParsingError
- Faraday::Error::ResourceNotFound
- Faraday::Error::TimeoutError
- Faraday::Middleware
- Faraday::MiddlewareRegistry
- Faraday::Request
- Faraday::Request::Authorization
- Faraday::Request::BasicAuthentication
- Faraday::Request::Multipart
- Faraday::Request::Retry
- Faraday::Request::TokenAuthentication
- Faraday::Request::UrlEncoded
- Faraday::Response
- Faraday::Response::Logger
- Faraday::Response::Middleware
- Faraday::Response::RaiseError
- Faraday::Utils
- Faraday::Utils::Headers
- Faraday::Utils::ParamsHash
- Object

No matching classes.

# Faraday::Adapter::EMHttp

EventMachine adapter is useful for either asynchronous requests when in EM reactor loop or for making parallel requests in synchronous code.

### Public Class Methods

setup_parallel_manager(options = nil) click to toggle source

    # File lib/faraday/adapter/em_http.rb, line 64
    def self.setup_parallel_manager(options = nil)
      Manager.new
    end

### Public Instance Methods

call(env) click to toggle source

    # File lib/faraday/adapter/em_http.rb, line 68
    def call(env)
      super
      perform_request env
      @app.call env
    end

error_message(client) click to toggle source

    # File lib/faraday/adapter/em_http.rb, line 125
    def error_message(client)
      client.error or "request failed"
    end

parallel?(env) click to toggle source

    # File lib/faraday/adapter/em_http.rb, line 143
    def parallel?(env)
      !!env[:parallel_manager]
    end

perform_request(env) click to toggle source

    # File lib/faraday/adapter/em_http.rb, line 74
    def perform_request(env)
      if parallel?(env)
        manager = env[:parallel_manager]
        manager.add {
          perform_single_request(env).
            callback { env[:response].finish(env) }
        }
      else
        unless EventMachine.reactor_running?
          error = nil
          # start EM, block until request is completed
          EventMachine.run do
            perform_single_request(env).
              callback { EventMachine.stop }.
              errback { |client|
                error = error_message(client)
                EventMachine.stop
              }
          end
          raise_error(error) if error
        else
          # EM is running: instruct upstream that this is an async request
          env[:parallel_manager] = true
          perform_single_request(env).
            callback { env[:response].finish(env) }.
            errback {
              # TODO: no way to communicate the error in async mode
              raise NotImplementedError
            }
        end
      end
    rescue EventMachine::Connectify::CONNECTError => err
      if err.message.include?("Proxy Authentication Required")
        raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
      else
        raise Error::ConnectionFailed, err
      end
    end

perform_single_request(env) click to toggle source

TODO: reuse the connection to support pipelining

    # File lib/faraday/adapter/em_http.rb, line 114
    def perform_single_request(env)
      req = EventMachine::HttpRequest.new(env[:url], connection_config(env))
      req.setup_request(env[:method], request_config(env)).callback { |client|
        save_response(env, client.response_header.status, client.response) do |resp_headers|
          client.response_header.each do |name, value|
            resp_headers[name.to_sym] = value
          end
        end
      }
    end

raise_error(msg) click to toggle source

    # File lib/faraday/adapter/em_http.rb, line 129
    def raise_error(msg)
      errklass = Faraday::Error::ClientError
      if msg == Errno::ETIMEDOUT
        errklass = Faraday::Error::TimeoutError
        msg = "request timed out"
      elsif msg == Errno::ECONNREFUSED
        errklass = Faraday::Error::ConnectionFailed
        msg = "connection refused"
      elsif msg == "connection closed by server"
        errklass = Faraday::Error::ConnectionFailed
      end
      raise errklass, msg
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.