Class: Faraday::RackBuilder

Inherits:
Object
  • Object
show all
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).

Examples:

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

Defined Under Namespace

Classes: Handler, StackLocked

Constant Summary collapse

NO_ARGUMENT =

Used to detect missing arguments

Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handlers = [], adapter = nil, &block) ⇒ RackBuilder

Returns a new instance of RackBuilder.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/faraday/rack_builder.rb', line 60

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

Instance Attribute Details

#handlersObject

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



79
80
81
# File 'lib/faraday/rack_builder.rb', line 79

def [](idx)
  @handlers[idx]
end

#adapter(klass = NO_ARGUMENT, *args, &block) ⇒ Object



110
111
112
113
114
115
# File 'lib/faraday/rack_builder.rb', line 110

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

#appObject

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.



163
164
165
166
167
168
# File 'lib/faraday/rack_builder.rb', line 163

def app
  @app ||= begin
    lock!
    to_app
  end
end

#build(options = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



72
73
74
75
76
77
# File 'lib/faraday/rack_builder.rb', line 72

def build(options = {})
  raise_if_locked
  @handlers.clear unless options[:keep]
  yield(self) if block_given?
  adapter(Faraday.default_adapter) unless @adapter
end

#build_env(connection, request) ⇒ Object

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.



204
205
206
207
208
209
210
211
212
213
# File 'lib/faraday/rack_builder.rb', line 204

def build_env(connection, request)
  exclusive_url = connection.build_exclusive_url(
    request.path, request.params,
    request.options.params_encoder
  )

  Env.new(request.method, request.body, exclusive_url,
          request.options, 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.

Parameters:

Returns:



152
153
154
# File 'lib/faraday/rack_builder.rb', line 152

def build_response(connection, request)
  app.call(build_env(connection, request))
end

#delete(handler) ⇒ Object



140
141
142
143
# File 'lib/faraday/rack_builder.rb', line 140

def delete(handler)
  raise_if_locked
  @handlers.delete(handler)
end

#dupObject



184
185
186
# File 'lib/faraday/rack_builder.rb', line 184

def dup
  self.class.new(@handlers.dup, @adapter.dup)
end

#insert(index, *args, &block) ⇒ Object Also known as: insert_before

methods to push onto the various positions in the stack:



119
120
121
122
123
124
# File 'lib/faraday/rack_builder.rb', line 119

def insert(index, *args, &block)
  raise_if_locked
  index = assert_index(index)
  handler = self.class::Handler.new(*args, &block)
  @handlers.insert(index, handler)
end

#insert_after(index, *args, &block) ⇒ Object



128
129
130
131
# File 'lib/faraday/rack_builder.rb', line 128

def insert_after(index, *args, &block)
  index = assert_index(index)
  insert(index + 1, *args, &block)
end

#lock!Object

Locks the middleware stack to ensure no further modifications are made.



84
85
86
# File 'lib/faraday/rack_builder.rb', line 84

def lock!
  @handlers.freeze
end

#locked?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/faraday/rack_builder.rb', line 88

def locked?
  @handlers.frozen?
end

#request(key, *args, &block) ⇒ Object



102
103
104
# File 'lib/faraday/rack_builder.rb', line 102

def request(key, *args, &block)
  use_symbol(Faraday::Request, key, *args, &block)
end

#response(key, *args, &block) ⇒ Object



106
107
108
# File 'lib/faraday/rack_builder.rb', line 106

def response(key, *args, &block)
  use_symbol(Faraday::Response, key, *args, &block)
end

#swap(index, *args, &block) ⇒ Object



133
134
135
136
137
138
# File 'lib/faraday/rack_builder.rb', line 133

def swap(index, *args, &block)
  raise_if_locked
  index = assert_index(index)
  @handlers.delete_at(index)
  insert(index, *args, &block)
end

#to_appObject



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

#use(klass, *args, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/faraday/rack_builder.rb', line 92

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