class Faraday::RackBuilder

A Builder that processes requests into responses by passing through an inner middleware stack (heavily inspired by Rack).

@example

Faraday::Connection.new(url: 'http://httpbingo.org') do |builder|
  builder.request  :url_encoded  # Faraday::Request::UrlEncoded
  builder.adapter  :net_http     # Faraday::Adapter::NetHttp
end

Constants

LOCK_ERR
MISSING_ADAPTER_ERROR
NO_ARGUMENT

Used to detect missing arguments

Attributes

handlers[RW]

Public Class Methods

new(&block) click to toggle source
# File lib/faraday/rack_builder.rb, line 60
def initialize(&block)
  @adapter = nil
  @handlers = []
  build(&block)
end

Public Instance Methods

==(other) click to toggle source
# File lib/faraday/rack_builder.rb, line 178
def ==(other)
  other.is_a?(self.class) &&
    @handlers == other.handlers &&
    @adapter == other.adapter
end
[](idx) click to toggle source
# File lib/faraday/rack_builder.rb, line 78
def [](idx)
  @handlers[idx]
end
adapter(klass = NO_ARGUMENT, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 109
               def adapter(klass = NO_ARGUMENT, *args, &block)
  return @adapter if klass == NO_ARGUMENT || klass.nil?

  klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
  @adapter = self.class::Handler.new(klass, *args, &block)
end
app() click to toggle source

The “rack app” wrapped in middleware. All requests are sent here.

The builder is responsible for creating the app object. After this, the builder gets locked to ensure no further modifications are made to the middleware stack.

Returns an object that responds to `call` and returns a Response.

# File lib/faraday/rack_builder.rb, line 162
def app
  @app ||= begin
    lock!
    ensure_adapter!
    to_app
  end
end
build() { |self| ... } click to toggle source
# File lib/faraday/rack_builder.rb, line 72
def build
  raise_if_locked
  block_given? ? yield(self) : request(:url_encoded)
  adapter(Faraday.default_adapter, **Faraday.default_adapter_options) unless @adapter
end
build_env(connection, request) click to toggle source

ENV Keys :http_method - a symbolized request HTTP method (:get, :post) :body - the request body that will eventually be converted to a string. :url - URI instance for the current request. :status - HTTP response status code :request_headers - hash of HTTP Headers to be sent to the server :response_headers - Hash of HTTP headers from the server :parallel_manager - sent if the connection is in parallel mode :request - Hash of options for configuring the request.

:timeout      - open/read timeout Integer in seconds
:open_timeout - read timeout Integer in seconds
:proxy        - Hash of proxy options
  :uri        - Proxy Server URI
  :user       - Proxy server username
  :password   - Proxy server password

:ssl - Hash of options for configuring SSL requests.

# File lib/faraday/rack_builder.rb, line 200
def build_env(connection, request)
  exclusive_url = connection.build_exclusive_url(
    request.path, request.params,
    request.options.params_encoder
  )

  Env.new(request.http_method, request.body, exclusive_url,
          request.options, request.headers, connection.ssl,
          connection.parallel_manager)
end
build_response(connection, request) click to toggle source

Processes a Request into a Response by passing it through this Builder's middleware stack.

@param connection [Faraday::Connection] @param request [Faraday::Request]

@return [Faraday::Response]

# File lib/faraday/rack_builder.rb, line 151
def build_response(connection, request)
  app.call(build_env(connection, request))
end
delete(handler) click to toggle source
# File lib/faraday/rack_builder.rb, line 139
def delete(handler)
  raise_if_locked
  @handlers.delete(handler)
end
initialize_dup(original) click to toggle source
Calls superclass method
# File lib/faraday/rack_builder.rb, line 66
def initialize_dup(original)
  super
  @adapter = original.adapter
  @handlers = original.handlers.dup
end
insert(index, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 118
               def insert(index, *args, &block)
  raise_if_locked
  index = assert_index(index)
  handler = self.class::Handler.new(*args, &block)
  @handlers.insert(index, handler)
end
Also aliased as: insert_before
insert_after(index, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 127
               def insert_after(index, *args, &block)
  index = assert_index(index)
  insert(index + 1, *args, &block)
end
insert_before(index, *args, &block)
Alias for: insert
lock!() click to toggle source

Locks the middleware stack to ensure no further modifications are made.

# File lib/faraday/rack_builder.rb, line 83
def lock!
  @handlers.freeze
end
locked?() click to toggle source
# File lib/faraday/rack_builder.rb, line 87
def locked?
  @handlers.frozen?
end
request(key, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 101
               def request(key, *args, &block)
  use_symbol(Faraday::Request, key, *args, &block)
end
response(key, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 105
               def response(key, *args, &block)
  use_symbol(Faraday::Response, key, *args, &block)
end
swap(index, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 132
               def swap(index, *args, &block)
  raise_if_locked
  index = assert_index(index)
  @handlers.delete_at(index)
  insert(index, *args, &block)
end
to_app() click to toggle source
# File lib/faraday/rack_builder.rb, line 170
def to_app
  # last added handler is the deepest and thus closest to the inner app
  # adapter is always the last one
  @handlers.reverse.inject(@adapter.build) do |app, handler|
    handler.build(app)
  end
end
use(klass, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 91
               def use(klass, *args, &block)
  if klass.is_a? Symbol
    use_symbol(Faraday::Middleware, klass, *args, &block)
  else
    raise_if_locked
    raise_if_adapter(klass)
    @handlers << self.class::Handler.new(klass, *args, &block)
  end
end

Private Instance Methods

adapter_set?() click to toggle source
# File lib/faraday/rack_builder.rb, line 232
def adapter_set?
  !@adapter.nil?
end
assert_index(index) click to toggle source
# File lib/faraday/rack_builder.rb, line 244
def assert_index(index)
  idx = index.is_a?(Integer) ? index : @handlers.index(index)
  raise "No such handler: #{index.inspect}" unless idx

  idx
end
ensure_adapter!() click to toggle source
# File lib/faraday/rack_builder.rb, line 228
def ensure_adapter!
  raise MISSING_ADAPTER_ERROR unless @adapter
end
is_adapter?(klass) click to toggle source
# File lib/faraday/rack_builder.rb, line 236
def is_adapter?(klass) # rubocop:disable Naming/PredicateName
  klass <= Faraday::Adapter
end
raise_if_adapter(klass) click to toggle source
# File lib/faraday/rack_builder.rb, line 222
def raise_if_adapter(klass)
  return unless is_adapter?(klass)

  raise 'Adapter should be set using the `adapter` method, not `use`'
end
raise_if_locked() click to toggle source
# File lib/faraday/rack_builder.rb, line 218
def raise_if_locked
  raise StackLocked, LOCK_ERR if locked?
end
use_symbol(mod, key, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 240
               def use_symbol(mod, key, *args, &block)
  use(mod.lookup_middleware(key), *args, &block)
end