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 |builder|

builder.request  :url_encoded  # Faraday::Request::UrlEncoded
builder.adapter  :net_http     # Faraday::Adapter::NetHttp

end

Defined Under Namespace

Classes: Handler, StackLocked

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handlers = []) ⇒ Builder

Returns a new instance of Builder.



51
52
53
54
55
56
57
58
59
60
# File 'lib/faraday/builder.rb', line 51

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

Instance Attribute Details

#handlersObject

Returns the value of attribute handlers.



9
10
11
# File 'lib/faraday/builder.rb', line 9

def handlers
  @handlers
end

Instance Method Details

#==(other) ⇒ Object



76
77
78
# File 'lib/faraday/builder.rb', line 76

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

#[](idx) ⇒ Object



72
73
74
# File 'lib/faraday/builder.rb', line 72

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

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



115
116
117
118
119
120
121
# File 'lib/faraday/builder.rb', line 115

def adapter(key=nil, *args, &block)
  if [key, *args, block].none?
    find_adapter
  else
    use_symbol(Faraday::Adapter, key, *args, &block)
  end
end

#adapter=(adapter_args) ⇒ Object



155
156
157
158
# File 'lib/faraday/builder.rb', line 155

def adapter=(adapter_args)
  clear_adapters
  adapter(*adapter_args)
end

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

Yields:

  • (_self)

Yield Parameters:



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

def build(options = {})
  raise_if_locked
  clear unless options[:keep]
  yield self if block_given?
end

#clearObject



68
69
70
# File 'lib/faraday/builder.rb', line 68

def clear
  @handlers.clear
end

#delete(handler) ⇒ Object



150
151
152
153
# File 'lib/faraday/builder.rb', line 150

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

#dupObject



80
81
82
# File 'lib/faraday/builder.rb', line 80

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

#has_adapter?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/faraday/builder.rb', line 123

def has_adapter?
  !!find_adapter
end

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

methods to push onto the various positions in the stack:



129
130
131
132
133
134
# File 'lib/faraday/builder.rb', line 129

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



138
139
140
141
# File 'lib/faraday/builder.rb', line 138

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 possible.



90
91
92
# File 'lib/faraday/builder.rb', line 90

def lock!
  @handlers.freeze
end

#locked?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/faraday/builder.rb', line 94

def locked?
  @handlers.frozen?
end

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



107
108
109
# File 'lib/faraday/builder.rb', line 107

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

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



111
112
113
# File 'lib/faraday/builder.rb', line 111

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

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



143
144
145
146
147
148
# File 'lib/faraday/builder.rb', line 143

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

#to_app(inner_app) ⇒ Object



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

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

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



98
99
100
101
102
103
104
105
# File 'lib/faraday/builder.rb', line 98

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