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
Public Class Methods
# File lib/faraday/rack_builder.rb, line 52 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
Public Instance Methods
# File lib/faraday/rack_builder.rb, line 170 def ==(other) other.is_a?(self.class) && @handlers == other.handlers end
# File lib/faraday/rack_builder.rb, line 69 def [](idx) @handlers[idx] end
# File lib/faraday/rack_builder.rb, line 100 def adapter(key, *args, &block) use_symbol(Faraday::Adapter, key, *args, &block) end
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
# 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
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
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
# File lib/faraday/rack_builder.rb, line 128 def delete(handler) raise_if_locked @handlers.delete(handler) end
# File lib/faraday/rack_builder.rb, line 174 def dup self.class.new(@handlers.dup) end
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
# File lib/faraday/rack_builder.rb, line 116 def insert_after(index, *args, &block) index = assert_index(index) insert(index + 1, *args, &block) end
Locks the middleware stack to ensure no further modifications are possible.
# File lib/faraday/rack_builder.rb, line 74 def lock! @handlers.freeze end
# File lib/faraday/rack_builder.rb, line 78 def locked? @handlers.frozen? end
# File lib/faraday/rack_builder.rb, line 92 def request(key, *args, &block) use_symbol(Faraday::Request, key, *args, &block) end
# File lib/faraday/rack_builder.rb, line 96 def response(key, *args, &block) use_symbol(Faraday::Response, key, *args, &block) end
# 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
# 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
# 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
# File lib/faraday/rack_builder.rb, line 212 def adapter_set? @handlers.any? { |handler| is_adapter?(handler) } end
# 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
# 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
# File lib/faraday/rack_builder.rb, line 223 def is_adapter?(handler) handler.klass.ancestors.include? Faraday::Adapter end
# 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
# File lib/faraday/rack_builder.rb, line 227 def use_symbol(mod, key, *args, &block) use(mod.lookup_middleware(key), *args, &block) end
# 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