Module: Faraday::MiddlewareRegistry

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#lookup_middleware(key) ⇒ Class

Lookup middleware class with a registered Symbol shortcut.

Examples:


module Faraday
  class Whatever < Middleware
    register_middleware(foo: Whatever)
  end
end

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

Parameters:

  • key (Symbol)

    key for the registered middleware.

Returns:

  • (Class)

    a middleware Class.

Raises:



55
56
57
58
# File 'lib/faraday/middleware_registry.rb', line 55

def lookup_middleware(key)
  load_middleware(key) ||
    raise(Faraday::Error, "#{key.inspect} is not registered on #{self}")
end

#register_middleware(**mappings) ⇒ void

This method returns an undefined value.

Register middleware class(es) on the current module.

Examples:

Lookup by a constant


module Faraday
  class Whatever < Middleware
    # Middleware looked up by :foo returns Faraday::Whatever::Foo.
    register_middleware(foo: Whatever)
  end
end

Parameters:

  • mappings (Hash)

    Middleware mappings from a lookup symbol to a middleware class.



26
27
28
29
30
# File 'lib/faraday/middleware_registry.rb', line 26

def register_middleware(**mappings)
  middleware_mutex do
    registered_middleware.update(mappings)
  end
end

#registered_middlewareObject



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

def registered_middleware
  @registered_middleware ||= {}
end

#unregister_middleware(key) ⇒ Object

Unregister a previously registered middleware class.

Parameters:

  • key (Symbol)

    key for the registered middleware.



35
36
37
# File 'lib/faraday/middleware_registry.rb', line 35

def unregister_middleware(key)
  registered_middleware.delete(key)
end