class Faraday::RackBuilder
end
builder.adapter :net_http # Faraday::Adapter::NetHttp
builder.request :url_encoded # Faraday::Request::UrlEncoded
Faraday::Connection.new(:url => ‘sushi.com’) do |builder|
middleware stack (heavily inspired by Rack).
A Builder that processes requests into responses by passing through an inner
def ==(other)
def ==(other) other.is_a?(self.class) && @handlers == other.handlers end
def [](idx)
def [](idx) @handlers[idx] end
def adapter(key, *args, &block)
def adapter(key, *args, &block) use_symbol(Faraday::Adapter, key, *args, &block) end
def app
to the middleware stack.
the builder gets locked to ensure no further modifications are made
The builder is responsible for creating the app object. After this,
The "rack app" wrapped in middleware. All requests are sent here.
def app @app ||= begin lock! to_app(lambda { |env| response = Response.new env.response = response response.finish(env) unless env.parallel? response }) end end
def assert_index(index)
def assert_index(index) idx = index.is_a?(Integer) ? index : @handlers.index(index) raise "No such handler: #{index.inspect}" unless idx idx end
def build(options = {})
def build(options = {}) raise_if_locked @handlers.clear unless options[:keep] yield(self) if block_given? end
def build_env(connection, request)
:password - Proxy server password
:user - Proxy server username
:uri - Proxy Server URI
:proxy - Hash of proxy options
:open_timeout - read timeout Integer in seconds
:timeout - open/read timeout Integer in seconds
:request - Hash of options for configuring the request.
:parallel_manager - sent if the connection is in parallel mode
:response_headers - Hash of HTTP headers from the server
:request_headers - hash of HTTP Headers to be sent to the server
:status - HTTP response status code
:url - URI instance for the current request.
:body - the request body that will eventually be converted to a string.
:method - a symbolized request method (:get, :post)
ENV Keys
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
def build_response(connection, request)
request - Faraday::Request
connection - Faraday::Connection
middleware stack.
Processes a Request into a Response by passing it through this Builder's
def build_response(connection, request) app.call(build_env(connection, request)) end
def delete(handler)
def delete(handler) raise_if_locked @handlers.delete(handler) end
def dup
def dup self.class.new(@handlers.dup) end
def initialize(handlers = [])
def initialize(handlers = []) @handlers = handlers if block_given? build(&Proc.new) elsif @handlers.empty? # default stack, if nothing else is configured self.request :url_encoded self.adapter Faraday.default_adapter end end
def insert(index, *args, &block)
def insert(index, *args, &block) raise_if_locked index = assert_index(index) handler = self.class::Handler.new(*args, &block) @handlers.insert(index, handler) end
def insert_after(index, *args, &block)
def insert_after(index, *args, &block) index = assert_index(index) insert(index + 1, *args, &block) end
def lock!
def lock! @handlers.freeze end
def locked?
def locked? @handlers.frozen? end
def raise_if_locked
def raise_if_locked raise StackLocked, "can't modify middleware stack after making a request" if locked? end
def request(key, *args, &block)
def request(key, *args, &block) use_symbol(Faraday::Request, key, *args, &block) end
def response(key, *args, &block)
def response(key, *args, &block) use_symbol(Faraday::Response, key, *args, &block) end
def swap(index, *args, &block)
def swap(index, *args, &block) raise_if_locked index = assert_index(index) @handlers.delete_at(index) insert(index, *args, &block) end
def to_app(inner_app)
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
def use(klass, *args, &block)
def use(klass, *args, &block) if klass.is_a? Symbol use_symbol(Faraday::Middleware, klass, *args, &block) else raise_if_locked @handlers << self.class::Handler.new(klass, *args, &block) end end
def use_symbol(mod, key, *args, &block)
def use_symbol(mod, key, *args, &block) use(mod.lookup_middleware(key), *args, &block) end