Class: Faraday::Builder
Overview
Possibly going to extend this a bit.
Faraday::Connection.new(:url => ‘sushi.com’) do |b|
b.request :yajl # Faraday::Request::Yajl
b.adapter :logger # Faraday::Adapter::Logger
b.response :yajl # Faraday::Response::Yajl
end
Instance Attribute Summary collapse
-
#handlers ⇒ Object
Returns the value of attribute handlers.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](index) ⇒ Object
- #adapter(key, *args, &block) ⇒ Object
- #build(&block) ⇒ Object
- #dup ⇒ Object
-
#initialize(handlers = [], &block) ⇒ Builder
constructor
A new instance of Builder.
- #request(key, *args, &block) ⇒ Object
- #response(key, *args, &block) ⇒ Object
- #run(app) ⇒ Object
- #to_app ⇒ Object
- #use(klass, *args, &block) ⇒ Object
- #use_symbol(mod, key, *args, &block) ⇒ Object
Constructor Details
#initialize(handlers = [], &block) ⇒ Builder
Returns a new instance of Builder.
22 23 24 25 |
# File 'lib/faraday/builder.rb', line 22 def initialize(handlers = [], &block) @handlers = handlers build(&block) if block_given? end |
Instance Attribute Details
#handlers ⇒ Object
Returns the value of attribute handlers.
10 11 12 |
# File 'lib/faraday/builder.rb', line 10 def handlers @handlers end |
Class Method Details
.create(&block) ⇒ Object
12 13 14 |
# File 'lib/faraday/builder.rb', line 12 def self.create(&block) Builder.new(&block) end |
.inner_app ⇒ Object
16 17 18 19 20 |
# File 'lib/faraday/builder.rb', line 16 def self.inner_app lambda do |env| env[:parallel_manager] ? env[:response] : env[:response].finish(env) end end |
Instance Method Details
#==(other) ⇒ Object
66 67 68 |
# File 'lib/faraday/builder.rb', line 66 def ==(other) other.is_a?(self.class) && @handlers == other.handlers end |
#[](index) ⇒ Object
32 33 34 35 |
# File 'lib/faraday/builder.rb', line 32 def [](index) # @handlers are stored in reverse order @handlers[-(index+1)] end |
#adapter(key, *args, &block) ⇒ Object
58 59 60 |
# File 'lib/faraday/builder.rb', line 58 def adapter(key, *args, &block) use_symbol Faraday::Adapter, key, *args, &block end |
#build(&block) ⇒ Object
27 28 29 30 |
# File 'lib/faraday/builder.rb', line 27 def build(&block) block.call(self) run(self.class.inner_app) end |
#dup ⇒ Object
70 71 72 |
# File 'lib/faraday/builder.rb', line 70 def dup self.class.new @handlers.dup end |
#request(key, *args, &block) ⇒ Object
50 51 52 |
# File 'lib/faraday/builder.rb', line 50 def request(key, *args, &block) use_symbol Faraday::Request, key, *args, &block end |
#response(key, *args, &block) ⇒ Object
54 55 56 |
# File 'lib/faraday/builder.rb', line 54 def response(key, *args, &block) use_symbol Faraday::Response, key, *args, &block end |
#run(app) ⇒ Object
37 38 39 |
# File 'lib/faraday/builder.rb', line 37 def run(app) @handlers.unshift app end |
#to_app ⇒ Object
41 42 43 44 |
# File 'lib/faraday/builder.rb', line 41 def to_app inner_app = @handlers.first @handlers[1..-1].inject(inner_app) { |app, middleware| middleware.call(app) } end |
#use(klass, *args, &block) ⇒ Object
46 47 48 |
# File 'lib/faraday/builder.rb', line 46 def use(klass, *args, &block) run(lambda { |app| klass.new(app, *args, &block) }) end |
#use_symbol(mod, key, *args, &block) ⇒ Object
62 63 64 |
# File 'lib/faraday/builder.rb', line 62 def use_symbol(mod, key, *args, &block) use mod.lookup_module(key), *args, &block end |