Module: Faraday::MiddlewareRegistry

Included in:
Adapter, Middleware, Request, Response
Defined in:
lib/faraday.rb

Overview

Public: Adds the ability for other modules to register and lookup middleware classes.

Instance Method Summary collapse

Instance Method Details

#lookup_middleware(key) ⇒ Object

Public: Lookup middleware class with a registered Symbol shortcut.

key - The Symbol key for the registered middleware.

Examples

module Faraday
  class Whatever
    register_middleware :foo => Foo
  end
end

Faraday::Whatever.lookup_middleware(:foo)
# => Faraday::Whatever::Foo

Returns a middleware Class.



160
161
162
163
164
165
166
# File 'lib/faraday.rb', line 160

def lookup_middleware(key)
  unless defined? @registered_middleware and found = @registered_middleware[key]
    raise "#{key.inspect} is not registered on #{self}"
  end
  found = @registered_middleware[key] = found.call if found.is_a? Proc
  found.is_a?(Module) ? found : const_get(found)
end

#register_middleware(mapping) ⇒ Object

Public: Register middleware class(es) on the current module.

mapping - A Hash mapping Symbol keys to classes. Classes can be expressed

as fully qualified constant, or a Proc that will be lazily
called to return the former.

Examples

module Faraday
  class Whatever
    # Middleware looked up by :foo returns Faraday::Whatever::Foo.
    register_middleware :foo => Foo
  end
end

Returns nothing.



140
141
142
# File 'lib/faraday.rb', line 140

def register_middleware(mapping)
  (@registered_middleware ||= {}).update(mapping)
end