Sha256: 2c983c0d192765aa8db8d4787d4b44368770367efa57f26b7cf879a2ac0ae92a

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Battlenet
  module Adapter; end

  module AdapterManager
    class InvalidAdapter < Exception; end

    extend self

    @adapters = {
      :net_http => "NetHTTP",
      :typhoeus => "Typhoeus"
    }

    def adapters
      @adapters
    end

    def fetch(adapter_name)
      unless adapters.include? adapter_name
        raise InvalidAdapter.new("#{adapter_name.to_s} is not a valid adapter")
      end

      adapter_class = adapters[adapter_name]
      adapter = load_adapter adapter_name, adapter_class
    end

    def register(identifier, klass)
      @adapters[identifier] = klass
    end

    private

      def load_adapter(adapter_name, klass_name)
        begin
          klass = Battlenet::Adapter.const_get("#{klass_name}", false)
        rescue NameError
          begin
            adapter_file = "battlenet/adapter/#{adapter_name.to_s}"
            require adapter_file
            klass = Battlenet::Adapter.const_get("#{klass_name}", false)
          rescue LoadError
            raise InvalidAdapter.new("adapter #{klass_name} does not exist, and file #{adapter_file} does not exist")
          rescue NameError
            raise InvalidAdapter.new("expected #{adapter_file} to define Battlenet::Adapter::#{klass_name}")
          end
        end

        return klass.new
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
battlenet-0.1.1 lib/battlenet/adapter_manager.rb