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

#fetch_middleware(key) ⇒ Object


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

def fetch_middleware(key)
  defined?(@registered_middleware) && @registered_middleware[key]
end

#load_middleware(key) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/faraday/middleware_registry.rb', line 103

def load_middleware(key)
  value = fetch_middleware(key)
  case value
  when Module
    value
  when Symbol, String
    middleware_mutex do
      @registered_middleware[key] = const_get(value)
    end
  when Proc
    middleware_mutex do
      @registered_middleware[key] = value.call
    end
  when Array
    middleware_mutex do
      const, path = value
      if (root = @middleware_autoload_path)
        path = "#{root}/#{path}"
      end
      require(path)
      @registered_middleware[key] = const
    end
    load_middleware(key)
  end
end

#lookup_middleware(key) ⇒ Class

Lookup middleware class with a registered Symbol shortcut.

Examples:


module Faraday
  class Whatever
    register_middleware foo: Foo
  end
end

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

Parameters:

  • key (Symbol)

    key for the registered middleware.

Returns:

  • (Class)

    a middleware Class.

Raises:


89
90
91
92
# File 'lib/faraday/middleware_registry.rb', line 89

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

#middleware_mutex(&block) ⇒ Object


94
95
96
97
# File 'lib/faraday/middleware_registry.rb', line 94

def middleware_mutex(&block)
  @middleware_mutex ||= Monitor.new
  @middleware_mutex.synchronize(&block)
end

#register_middleware(autoload_path = nil, mapping = nil) ⇒ void

This method returns an undefined value.

Register middleware class(es) on the current module.

   }] Middleware mapping from a lookup symbol to a reference to the
   middleware.
   Classes can be expressed as:
     - a fully qualified constant
     - a Symbol
     - a Proc that will be lazily called to return the former
     - an array is given, its first element is the constant or symbol,
       and its second is a file to `require`.

Examples:

Lookup by a constant


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

Lookup by a symbol


module Faraday
  class Whatever
    # Middleware looked up by :bar returns
    # Faraday::Whatever.const_get(:Bar)
    register_middleware bar: :Bar
  end
end

Lookup by a symbol and string in an array


module Faraday
  class Whatever
    # Middleware looked up by :baz requires 'baz' and returns
    # Faraday::Whatever.const_get(:Baz)
    register_middleware baz: [:Baz, 'baz']
  end
end

Parameters:

  • autoload_path (String) (defaults to: nil)

    Middleware autoload path

  • mapping (Hash{ Symbol => Module, Symbol => Array<Module, Symbol, String>,) (defaults to: nil)

    apping [Hash{ Symbol => Module, Symbol => Array,


54
55
56
57
58
59
60
61
62
63
# File 'lib/faraday/middleware_registry.rb', line 54

def register_middleware(autoload_path = nil, mapping = nil)
  if mapping.nil?
    mapping = autoload_path
    autoload_path = nil
  end
  middleware_mutex do
    @middleware_autoload_path = autoload_path if autoload_path
    (@registered_middleware ||= {}).update(mapping)
  end
end

#unregister_middleware(key) ⇒ Object

Unregister a previously registered middleware class.

Parameters:

  • key (Symbol)

    key for the registered middleware.


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

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