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.



47
48
49
50
51
52
53
54
55
56
# File 'lib/faraday/builder.rb', line 47

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



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

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

#[](idx) ⇒ Object



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

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

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



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

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

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

Yields:

  • (_self)

Yield Parameters:



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

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

#delete(handler) ⇒ Object



134
135
136
137
# File 'lib/faraday/builder.rb', line 134

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

#dupObject



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

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:



113
114
115
116
117
118
# File 'lib/faraday/builder.rb', line 113

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



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

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.



82
83
84
# File 'lib/faraday/builder.rb', line 82

def lock!
  @handlers.freeze
end

#locked?Boolean

Returns:

  • (Boolean)


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

def locked?
  @handlers.frozen?
end

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



99
100
101
# File 'lib/faraday/builder.rb', line 99

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

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



103
104
105
# File 'lib/faraday/builder.rb', line 103

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

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



127
128
129
130
131
132
# File 'lib/faraday/builder.rb', line 127

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



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

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



90
91
92
93
94
95
96
97
# File 'lib/faraday/builder.rb', line 90

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