class Faraday::RackBuilder

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

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

Attributes

handlers[RW]

Public Class Methods

new(handlers = [], &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 52
def initialize(handlers = [], &block)
  @handlers = handlers
  if block_given?
    build(&block)
  elsif @handlers.empty?
    # default stack, if nothing else is configured
    self.request :url_encoded
    self.adapter Faraday.default_adapter
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/faraday/rack_builder.rb, line 170
def ==(other)
  other.is_a?(self.class) && @handlers == other.handlers
end
[](idx) click to toggle source
# File lib/faraday/rack_builder.rb, line 69
def [](idx)
  @handlers[idx]
end
adapter(key, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 100
def adapter(key, *args, &block)
  use_symbol(Faraday::Adapter, key, *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 153
def app
  @app ||= begin
    lock!
    to_app(lambda { |env|
      response = Response.new
      env.response = response
      response.finish(env) unless env.parallel?
      response
    })
  end
end
build(options = {}) { |self| ... } click to toggle source
# File lib/faraday/rack_builder.rb, line 63
def build(options = {})
  raise_if_locked
  @handlers.clear unless options[:keep]
  yield(self) if block_given?
end
build_env(connection, request) click to toggle source

ENV Keys :method - a symbolized request 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 194
def build_env(connection, request)
  Env.new(request.method, request.body,
    connection.build_exclusive_url(request.path, request.params, request.options.params_encoder),
    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.

connection - Faraday::Connection request - Faraday::Request

Returns a Faraday::Response.

# File lib/faraday/rack_builder.rb, line 140
def build_response(connection, request)
  warn 'WARNING: No adapter was configured for this request' unless adapter_set?

  app.call(build_env(connection, request))
end
delete(handler) click to toggle source
# File lib/faraday/rack_builder.rb, line 128
def delete(handler)
  raise_if_locked
  @handlers.delete(handler)
end
dup() click to toggle source
# File lib/faraday/rack_builder.rb, line 174
def dup
  self.class.new(@handlers.dup)
end
insert(index, *args, &block) click to toggle source

methods to push onto the various positions in the stack:

# File lib/faraday/rack_builder.rb, line 106
def insert(index, *args, &block)
  raise_if_locked
  index = assert_index(index)
  warn_middleware_after_adapter if inserting_after_adapter?(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 116
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 possible.

# File lib/faraday/rack_builder.rb, line 74
def lock!
  @handlers.freeze
end
locked?() click to toggle source
# File lib/faraday/rack_builder.rb, line 78
def locked?
  @handlers.frozen?
end
request(key, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 92
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 96
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 121
def swap(index, *args, &block)
  raise_if_locked
  index = assert_index(index)
  @handlers.delete_at(index)
  insert(index, *args, &block)
end
to_app(inner_app) click to toggle source
# File lib/faraday/rack_builder.rb, line 165
def to_app(inner_app)
  # last added handler is the deepest and thus closest to the inner app
  @handlers.reverse.inject(inner_app) { |app, handler| handler.build(app) }
end
use(klass, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 82
def use(klass, *args, &block)
  if klass.is_a? Symbol
    use_symbol(Faraday::Middleware, klass, *args, &block)
  else
    raise_if_locked
    warn_middleware_after_adapter if adapter_set?
    @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 212
def adapter_set?
  @handlers.any? { |handler| is_adapter?(handler) }
end
assert_index(index) click to toggle source
# File lib/faraday/rack_builder.rb, line 231
def assert_index(index)
  idx = index.is_a?(Integer) ? index : @handlers.index(index)
  raise "No such handler: #{index.inspect}" unless idx
  idx
end
inserting_after_adapter?(index) click to toggle source
# File lib/faraday/rack_builder.rb, line 216
def inserting_after_adapter?(index)
  adapter_index = @handlers.find_index { |handler| is_adapter?(handler) }
  return false if adapter_index.nil?

  index > adapter_index
end
is_adapter?(handler) click to toggle source
# File lib/faraday/rack_builder.rb, line 223
def is_adapter?(handler)
  handler.klass.ancestors.include? Faraday::Adapter
end
raise_if_locked() click to toggle source
# File lib/faraday/rack_builder.rb, line 203
def raise_if_locked
  raise StackLocked, "can't modify middleware stack after making a request" if locked?
end
use_symbol(mod, key, *args, &block) click to toggle source
# File lib/faraday/rack_builder.rb, line 227
def use_symbol(mod, key, *args, &block)
  use(mod.lookup_middleware(key), *args, &block)
end
warn_middleware_after_adapter() click to toggle source
# File lib/faraday/rack_builder.rb, line 207
def warn_middleware_after_adapter
  warn "WARNING: Unexpected middleware set after the adapter. " \
    "This won't be supported from Faraday 1.0."
end