Module: Faraday::MiddlewareRegistry

### Home Classes Methods

### In Files

- lib/faraday.rb

### Methods

- #fetch_middleware
- #load_middleware
- #lookup_middleware
- #middleware_mutex
- #register_middleware

### Class/Module Index

Quicksearch

- Faraday
- Faraday::Adapter
- Faraday::Adapter::EMHttp
- Faraday::Adapter::EMHttp::Manager
- Faraday::Adapter::EMHttp::Options
- Faraday::Adapter::EMSynchrony
- Faraday::Adapter::EMSynchrony::ParallelManager
- Faraday::Adapter::Excon
- Faraday::Adapter::HTTPClient
- Faraday::Adapter::NetHttp
- Faraday::Adapter::NetHttpPersistent
- Faraday::Adapter::Parallelism
- Faraday::Adapter::Patron
- Faraday::Adapter::Rack
- Faraday::Adapter::Test
- Faraday::Adapter::Test::Stub
- Faraday::Adapter::Test::Stubs
- Faraday::Adapter::Test::Stubs::NotFound
- Faraday::Adapter::Typhoeus
- Faraday::AutoloadHelper
- Faraday::ClientError
- Faraday::CompositeReadIO
- Faraday::Connection
- Faraday::ConnectionFailed
- Faraday::ConnectionOptions
- Faraday::Env
- Faraday::Error
- Faraday::FlatParamsEncoder
- Faraday::Middleware
- Faraday::MiddlewareRegistry
- Faraday::MissingDependency
- Faraday::NestedParamsEncoder
- Faraday::Options
- Faraday::ParsingError
- Faraday::ProxyOptions
- Faraday::RackBuilder
- Faraday::RackBuilder::Handler
- Faraday::RackBuilder::StackLocked
- Faraday::Request
- Faraday::Request::Authorization
- Faraday::Request::BasicAuthentication
- Faraday::Request::Instrumentation
- Faraday::Request::Instrumentation::Options
- Faraday::Request::Multipart
- Faraday::Request::Retry
- Faraday::Request::Retry::Options
- Faraday::Request::TokenAuthentication
- Faraday::Request::UrlEncoded
- Faraday::RequestOptions
- Faraday::ResourceNotFound
- Faraday::Response
- Faraday::Response::Logger
- Faraday::Response::Middleware
- Faraday::Response::RaiseError
- Faraday::SSLError
- Faraday::SSLOptions
- Faraday::TimeoutError
- Faraday::Utils
- Faraday::Utils::Headers
- Faraday::Utils::ParamsHash
- EmHttpSslPatch
- Object

No matching classes.

# Faraday::MiddlewareRegistry

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

### Public Instance Methods

fetch_middleware(key) click to toggle source

    # File lib/faraday.rb, line 200
    def fetch_middleware(key)
      defined?(@registered_middleware) && @registered_middleware[key]
    end

load_middleware(key) click to toggle source

    # File lib/faraday.rb, line 204
    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) click to toggle source

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.

    # File lib/faraday.rb, line 187
    def lookup_middleware(key)
      load_middleware(key) ||
        raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
    end

middleware_mutex(&block) click to toggle source

    # File lib/faraday.rb, line 192
    def middleware_mutex(&block)
      @middleware_mutex ||= begin
        require 'monitor'
        Monitor.new
      end
      @middleware_mutex.synchronize(&block)
    end

register_middleware(autoload_path = nil, mapping = nil) click to toggle source

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

        # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
        register_middleware :bar => :Bar

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

Returns nothing.

    # File lib/faraday.rb, line 160
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.