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://sushi.com') do |builder| builder.request :url_encoded # Faraday::Request::UrlEncoded builder.adapter :net_http # Faraday::Adapter::NetHttp end
Constants
- LOCK_ERR
- NO_ARGUMENT
Used to detect missing arguments
Attributes
Public Class Methods
# File lib/faraday/rack_builder.rb, line 61 def initialize(handlers = [], adapter = nil, &block) @adapter = adapter @handlers = handlers if block_given? build(&block) elsif @handlers.empty? # default stack, if nothing else is configured request :url_encoded self.adapter Faraday.default_adapter end end
Public Instance Methods
# File lib/faraday/rack_builder.rb, line 179 def ==(other) other.is_a?(self.class) && @handlers == other.handlers && @adapter == other.adapter end
# File lib/faraday/rack_builder.rb, line 80 def [](idx) @handlers[idx] end
# File lib/faraday/rack_builder.rb, line 111 def adapter(klass = NO_ARGUMENT, *args, &block) return @adapter if klass == NO_ARGUMENT klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol) @adapter = self.class::Handler.new(klass, *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 164 def app @app ||= begin lock! to_app end end
# File lib/faraday/rack_builder.rb, line 73 def build(options = {}) raise_if_locked @handlers.clear unless options[:keep] yield(self) if block_given? adapter(Faraday.default_adapter) unless @adapter end
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 205 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
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 153 def build_response(connection, request) app.call(build_env(connection, request)) end
# File lib/faraday/rack_builder.rb, line 141 def delete(handler) raise_if_locked @handlers.delete(handler) end
# File lib/faraday/rack_builder.rb, line 185 def dup self.class.new(@handlers.dup, @adapter.dup) end
# File lib/faraday/rack_builder.rb, line 120 def insert(index, *args, &block) raise_if_locked index = assert_index(index) handler = self.class::Handler.new(*args, &block) @handlers.insert(index, handler) end
# File lib/faraday/rack_builder.rb, line 129 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 made.
# File lib/faraday/rack_builder.rb, line 85 def lock! @handlers.freeze end
# File lib/faraday/rack_builder.rb, line 89 def locked? @handlers.frozen? end
# File lib/faraday/rack_builder.rb, line 103 def request(key, *args, &block) use_symbol(Faraday::Request, key, *args, &block) end
# File lib/faraday/rack_builder.rb, line 107 def response(key, *args, &block) use_symbol(Faraday::Response, key, *args, &block) end
# File lib/faraday/rack_builder.rb, line 134 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 171 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
# File lib/faraday/rack_builder.rb, line 93 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
# File lib/faraday/rack_builder.rb, line 230 def adapter_set? !@adapter.nil? end
# File lib/faraday/rack_builder.rb, line 242 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 234 def is_adapter?(klass) # rubocop:disable Naming/PredicateName klass <= Faraday::Adapter end
# File lib/faraday/rack_builder.rb, line 224 def raise_if_adapter(klass) return unless is_adapter?(klass) raise 'Adapter should be set using the `adapter` method, not `use`' end
# File lib/faraday/rack_builder.rb, line 220 def raise_if_locked raise StackLocked, LOCK_ERR if locked? end
# File lib/faraday/rack_builder.rb, line 238 def use_symbol(mod, key, *args, &block) use(mod.lookup_middleware(key), *args, &block) end