Class: Faraday::Builder

Inherits:
Object show all
Defined in:
lib/faraday/builder.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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

#handlersObject

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_appObject



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



74
75
76
# File 'lib/faraday/builder.rb', line 74

def ==(other)
  other.is_a?(self.class) && @handlers == other.handlers
end

#[](index) ⇒ Object



36
37
38
39
# File 'lib/faraday/builder.rb', line 36

def [](index)
  # @handlers are stored in reverse order
  @handlers[-(index+1)]
end

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



66
67
68
# File 'lib/faraday/builder.rb', line 66

def adapter(key, *args, &block)
  use_symbol(Faraday::Adapter, key, *args, &block)
end

#build(options = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/faraday/builder.rb', line 27

def build(options = {}, &block)
  inner = @handlers.shift
  if !options[:keep]
    @handlers.clear
  end
  block.call(self)
  run(inner || self.class.inner_app)
end

#dupObject



78
79
80
# File 'lib/faraday/builder.rb', line 78

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

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



58
59
60
# File 'lib/faraday/builder.rb', line 58

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

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



62
63
64
# File 'lib/faraday/builder.rb', line 62

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

#run(app) ⇒ Object



41
42
43
# File 'lib/faraday/builder.rb', line 41

def run(app)
  @handlers.unshift app
end

#to_appObject



45
46
47
48
49
50
51
52
# File 'lib/faraday/builder.rb', line 45

def to_app
  if @handlers.empty?
    build { |b| b.adapter Faraday.default_adapter }
  end

  inner_app = @handlers.first
  @handlers[1..-1].inject(inner_app) { |app, middleware| middleware.call(app) }
end

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



54
55
56
# File 'lib/faraday/builder.rb', line 54

def use(klass, *args, &block)
  run(lambda { |app| klass.new(app, *args, &block) })
end

#use_symbol(mod, key, *args, &block) ⇒ Object



70
71
72
# File 'lib/faraday/builder.rb', line 70

def use_symbol(mod, key, *args, &block)
  use(mod.lookup_module(key), *args, &block)
end