Class: Faraday::Builder
Overview
Possibly going to extend this a bit.
Faraday::Connection.new(:url => ‘sushi.com’) do |builder|
builder.request :url_encoded builder.adapter :net_http
end
Defined Under Namespace
Classes: Handler, StackLocked
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#==(other) ⇒ Object
-
#[](idx) ⇒ Object
-
#adapter(key = nil, *args, &block) ⇒ Object
-
#adapter=(adapter_args) ⇒ Object
-
#build(options = {}) {|_self| ... } ⇒ Object
-
#clear ⇒ Object
-
#delete(handler) ⇒ Object
-
#dup ⇒ Object
-
#has_adapter? ⇒ Boolean
-
#initialize(handlers = []) ⇒ Builder
constructor
A new instance of Builder.
-
#insert(index, *args, &block) ⇒ Object
(also: #insert_before)
methods to push onto the various positions in the stack:.
-
#insert_after(index, *args, &block) ⇒ Object
-
#lock! ⇒ Object
Locks the middleware stack to ensure no further modifications are possible.
-
#locked? ⇒ Boolean
-
#request(key, *args, &block) ⇒ Object
-
#response(key, *args, &block) ⇒ Object
-
#swap(index, *args, &block) ⇒ Object
-
#to_app(inner_app) ⇒ Object
-
#use(klass, *args, &block) ⇒ Object
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?
self.request :url_encoded
self.adapter Faraday.default_adapter
end
end
|
Instance Attribute Details
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
|
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
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
|
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
|
80
81
82
|
# File 'lib/faraday/builder.rb', line 80
def dup
self.class.new(@handlers.dup)
end
|
#has_adapter? ⇒ 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
|
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
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)
@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
|