Sha256: ba6e699ce62fdc1ed77af6bb1e415d2d77dc19b39a149b5f44712f662345293f

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

module HTTPI

  # = HTTPI::Adapter
  #
  # Manages the adapter classes. Currently supports:
  #
  # * httpclient
  # * curb
  # * em_http
  # * net/http
  module Adapter

    ADAPTERS = {}
    ADAPTER_CLASS_MAP = {}

    LOAD_ORDER = [:httpclient, :curb, :em_http, :excon, :http, :net_http, :net_http_persistent]

    class << self

      def register(name, adapter_class, deps)
        ADAPTERS[name] = { :class => adapter_class, :deps => deps }
        ADAPTER_CLASS_MAP[adapter_class] = name
      end

      def use=(adapter)
        return @adapter = nil if adapter.nil?

        validate_adapter! adapter
        load_adapter adapter
        @adapter = adapter
      end

      def use
        @adapter ||= default_adapter
      end

      def identify(adapter_class)
        ADAPTER_CLASS_MAP[adapter_class]
      end

      def load(adapter)
        adapter ||= use

        validate_adapter!(adapter)
        load_adapter(adapter)
        ADAPTERS[adapter][:class]
      end

      def load_adapter(adapter)
        ADAPTERS[adapter][:deps].each do |dep|
          require dep
        end
      end

      private

      def validate_adapter!(adapter)
        raise ArgumentError, "Invalid HTTPI adapter: #{adapter}" unless ADAPTERS[adapter]
      end

      def default_adapter
        LOAD_ORDER.each do |adapter|
          begin
            load_adapter adapter
            return adapter
          rescue LoadError
            next
          end
        end
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
httpi-3.0.1 lib/httpi/adapter.rb
httpi-3.0.0 lib/httpi/adapter.rb
httpi-2.5.0 lib/httpi/adapter.rb
httpi-2.4.5 lib/httpi/adapter.rb
httpi-2.4.4 lib/httpi/adapter.rb
httpi-2.4.3 lib/httpi/adapter.rb
httpi-2.4.2 lib/httpi/adapter.rb
httpi-2.4.1 lib/httpi/adapter.rb