Class: Faraday::RackBuilder
- Inherits:
-
Object
- Object
- Faraday::RackBuilder
- Defined in:
- lib/faraday/rack_builder.rb
Overview
A Builder that processes requests into responses by passing through an inner middleware stack (heavily inspired by Rack).
Defined Under Namespace
Classes: Handler, StackLocked
Constant Summary collapse
- NO_ARGUMENT =
Used to detect missing arguments
Object.new
Instance Attribute Summary collapse
-
#handlers ⇒ Object
Returns the value of attribute handlers.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](idx) ⇒ Object
-
#app ⇒ Object
The “rack app” wrapped in middleware.
- #build ⇒ Object
-
#build_env(connection, request) ⇒ Object
ENV Keys :http_method - a symbolized request HTTP method (:get, :post) :body - the request body that will eventually be converted to a string.
-
#build_response(connection, request) ⇒ Faraday::Response
Processes a Request into a Response by passing it through this Builder’s middleware stack.
- #delete(handler) ⇒ Object
-
#initialize(&block) ⇒ RackBuilder
constructor
A new instance of RackBuilder.
- #initialize_dup(original) ⇒ Object
-
#insert ⇒ Object
(also: #insert_before)
methods to push onto the various positions in the stack:.
-
#lock! ⇒ Object
Locks the middleware stack to ensure no further modifications are made.
- #locked? ⇒ Boolean
- #to_app ⇒ Object
Constructor Details
#initialize(&block) ⇒ RackBuilder
Returns a new instance of RackBuilder.
60 61 62 63 64 |
# File 'lib/faraday/rack_builder.rb', line 60 def initialize(&block) @adapter = nil @handlers = [] build(&block) end |
Instance Attribute Details
#handlers ⇒ Object
Returns the value of attribute handlers.
18 19 20 |
# File 'lib/faraday/rack_builder.rb', line 18 def handlers @handlers end |
Instance Method Details
#==(other) ⇒ Object
178 179 180 181 182 |
# File 'lib/faraday/rack_builder.rb', line 178 def ==(other) other.is_a?(self.class) && @handlers == other.handlers && @adapter == other.adapter end |
#[](idx) ⇒ Object
78 79 80 |
# File 'lib/faraday/rack_builder.rb', line 78 def [](idx) @handlers[idx] end |
#app ⇒ Object
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.
162 163 164 165 166 167 168 |
# File 'lib/faraday/rack_builder.rb', line 162 def app @app ||= begin lock! ensure_adapter! to_app end end |
#build ⇒ Object
72 73 74 75 76 |
# 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.) unless @adapter end |
#build_env(connection, request) ⇒ Object
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.
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/faraday/rack_builder.rb', line 200 def build_env(connection, request) exclusive_url = connection.build_exclusive_url( request.path, request.params, request..params_encoder ) Env.new(request.http_method, request.body, exclusive_url, request., request.headers, connection.ssl, connection.parallel_manager) end |
#build_response(connection, request) ⇒ Faraday::Response
Processes a Request into a Response by passing it through this Builder’s middleware stack.
151 152 153 |
# File 'lib/faraday/rack_builder.rb', line 151 def build_response(connection, request) app.call(build_env(connection, request)) end |
#delete(handler) ⇒ Object
139 140 141 142 |
# File 'lib/faraday/rack_builder.rb', line 139 def delete(handler) raise_if_locked @handlers.delete(handler) end |
#initialize_dup(original) ⇒ Object
66 67 68 69 70 |
# File 'lib/faraday/rack_builder.rb', line 66 def initialize_dup(original) super @adapter = original.adapter @handlers = original.handlers.dup end |
#insert ⇒ Object Also known as: insert_before
methods to push onto the various positions in the stack:
118 119 120 121 122 123 |
# File 'lib/faraday/rack_builder.rb', line 118 ruby2_keywords def insert(index, *args, &block) raise_if_locked index = assert_index(index) handler = self.class::Handler.new(*args, &block) @handlers.insert(index, handler) end |
#lock! ⇒ Object
Locks the middleware stack to ensure no further modifications are made.
83 84 85 |
# File 'lib/faraday/rack_builder.rb', line 83 def lock! @handlers.freeze end |
#locked? ⇒ Boolean
87 88 89 |
# File 'lib/faraday/rack_builder.rb', line 87 def locked? @handlers.frozen? end |
#to_app ⇒ Object
170 171 172 173 174 175 176 |
# 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 |