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
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(other) ⇒ Object
-
#[](idx) ⇒ Object
-
#adapter(key, *args) ⇒ Object
-
#build(options = {}) {|_self| ... } ⇒ Object
-
#delete(handler) ⇒ Object
-
#dup ⇒ Object
-
#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) ⇒ Object
-
#response(key, *args) ⇒ Object
-
#swap(index, *args, &block) ⇒ Object
-
#to_app(inner_app) ⇒ Object
-
#use(klass, *args) ⇒ 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
|
Class Method Details
11
12
13
|
# File 'lib/faraday/builder.rb', line 11
def self.create
new { |builder| yield builder }
end
|
Instance Method Details
#==(other) ⇒ Object
72
73
74
|
# File 'lib/faraday/builder.rb', line 72
def ==(other)
other.is_a?(self.class) && @handlers == other.handlers
end
|
68
69
70
|
# File 'lib/faraday/builder.rb', line 68
def [](idx)
@handlers[idx]
end
|
#adapter(key, *args) ⇒ Object
110
111
112
113
|
# File 'lib/faraday/builder.rb', line 110
def adapter(key, *args)
block = block_given? ? Proc.new : nil
use_symbol(Faraday::Adapter, key, *args, &block)
end
|
#build(options = {}) {|_self| ... } ⇒ Object
62
63
64
65
66
|
# File 'lib/faraday/builder.rb', line 62
def build(options = {})
raise_if_locked
@handlers.clear unless options[:keep]
yield self if block_given?
end
|
#delete(handler) ⇒ Object
138
139
140
141
|
# File 'lib/faraday/builder.rb', line 138
def delete(handler)
raise_if_locked
@handlers.delete(handler)
end
|
76
77
78
|
# File 'lib/faraday/builder.rb', line 76
def dup
self.class.new(@handlers.dup)
end
|
#insert(index, *args, &block) ⇒ Object
Also known as:
insert_before
methods to push onto the various positions in the stack:
117
118
119
120
121
122
|
# File 'lib/faraday/builder.rb', line 117
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
126
127
128
129
|
# File 'lib/faraday/builder.rb', line 126
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.
86
87
88
|
# File 'lib/faraday/builder.rb', line 86
def lock!
@handlers.freeze
end
|
#locked? ⇒ Boolean
90
91
92
|
# File 'lib/faraday/builder.rb', line 90
def locked?
@handlers.frozen?
end
|
#request(key, *args) ⇒ Object
100
101
102
103
|
# File 'lib/faraday/builder.rb', line 100
def request(key, *args)
block = block_given? ? Proc.new : nil
use_symbol(Faraday::Request, key, *args, &block)
end
|
#response(key, *args) ⇒ Object
105
106
107
108
|
# File 'lib/faraday/builder.rb', line 105
def response(key, *args)
block = block_given? ? Proc.new : nil
use_symbol(Faraday::Response, key, *args, &block)
end
|
#swap(index, *args, &block) ⇒ Object
131
132
133
134
135
136
|
# File 'lib/faraday/builder.rb', line 131
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
80
81
82
83
|
# File 'lib/faraday/builder.rb', line 80
def to_app(inner_app)
@handlers.reverse.inject(inner_app) { |app, handler| handler.build(app) }
end
|
#use(klass, *args) ⇒ Object
94
95
96
97
98
|
# File 'lib/faraday/builder.rb', line 94
def use(klass, *args)
raise_if_locked
block = block_given? ? Proc.new : nil
@handlers << self.class::Handler.new(klass, *args, &block)
end
|