Sha256: 3fe7a16fd0a7f96aec8ed26816669ea679fa8ed8d6b494aa3638277591709753

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'regaliator/configuration'
require 'regaliator/api_version_error'
require 'regaliator/v15'
require 'regaliator/v30'
require 'regaliator/v31'

module Regaliator
  API_VERSIONS = {
    V15::API_VERSION => V15::Client,
    V30::API_VERSION => V30::Client,
    V31::API_VERSION => V31::Client
  }.freeze

  class << self
    def configuration
      @config ||= Configuration.new
    end

    def configure
      yield(configuration)
    end

    def new(arg = nil)
      config = get_configuration(arg)
      yield(config) if block_given?

      unless API_VERSIONS.key?(config.version)
        raise APIVersionError.new(config.version)
      end

      API_VERSIONS[config.version].new(config)
    end

    def method_missing(method_name, *args)
      client = new
      if client.respond_to?(method_name)
        client.send(method_name, *args)
      else
        super
      end
    end

    private

    def get_configuration(arg)
      return arg if arg.is_a?(Configuration)

      if arg.is_a?(Hash)
        configuration.dup.tap do |config|
          arg.each do |key, value|
            config.send("#{key}=", value) if config.respond_to?("#{key}=")
          end
        end
      else
        configuration
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
regaliator-4.0.1 lib/regaliator.rb
regaliator-4.0.0 lib/regaliator.rb