Possibly going to extend this a bit.
Faraday::Connection.new(:url => 'sushi.com') do |builder|
builder.request :url_encoded # Faraday::Request::UrlEncoded builder.adapter :net_http # Faraday::Adapter::NetHttp
end
# File lib/faraday/builder.rb, line 47 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
# File lib/faraday/builder.rb, line 68 def ==(other) other.is_a?(self.class) && @handlers == other.handlers end
# File lib/faraday/builder.rb, line 64 def [](idx) @handlers[idx] end
# File lib/faraday/builder.rb, line 107 def adapter(key, *args, &block) use_symbol(Faraday::Adapter, key, *args, &block) end
# File lib/faraday/builder.rb, line 58 def build(options = {}) raise_if_locked @handlers.clear unless options[:keep] yield self if block_given? end
# File lib/faraday/builder.rb, line 134 def delete(handler) raise_if_locked @handlers.delete(handler) end
# File lib/faraday/builder.rb, line 72 def dup self.class.new(@handlers.dup) end
methods to push onto the various positions in the stack:
# File lib/faraday/builder.rb, line 113 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/builder.rb, line 122 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/builder.rb, line 82 def lock! @handlers.freeze end
# File lib/faraday/builder.rb, line 86 def locked? @handlers.frozen? end
# File lib/faraday/builder.rb, line 99 def request(key, *args, &block) use_symbol(Faraday::Request, key, *args, &block) end
# File lib/faraday/builder.rb, line 103 def response(key, *args, &block) use_symbol(Faraday::Response, key, *args, &block) end
# File lib/faraday/builder.rb, line 127 def swap(index, *args, &block) raise_if_locked index = assert_index(index) @handlers.delete_at(index) insert(index, *args, &block) end
# File lib/faraday/builder.rb, line 76 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/builder.rb, line 90 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