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

- (Builder) initialize(handlers = [])



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

- (Object) handlers

Returns the value of attribute handlers



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

def handlers
  @handlers
end

Instance Method Details

- (Object) ==(other)



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

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

- (Object) [](idx)



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

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

- (Object) adapter(key, *args, &block)



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

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

- (Object) build(options = {}) {|_self| ... }

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

- (Object) delete(handler)



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

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

- (Object) dup



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

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

- (Object) insert(index, *args, &block) 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

- (Object) insert_after(index, *args, &block)



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

- (Object) lock!

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

- (Boolean) locked?



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

def locked?
  @handlers.frozen?
end

- (Object) request(key, *args, &block)



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

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

- (Object) response(key, *args, &block)



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

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

- (Object) swap(index, *args, &block)



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

- (Object) to_app(inner_app)



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

- (Object) use(klass, *args, &block)



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