Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
j_platform / lib / j_platform / configurable.rb
Size: Mime:
require 'forwardable'

module JPlatform
  module Configurable
    extend Forwardable
    attr_writer :client_id, :client_secret
    attr_accessor :connection_options, :endpoint
    def_delegator :options, :hash

    class << self

      def keys
        @keys ||= [
          :client_id,
          :client_secret,
          :endpoint,
          :connection_options,
        ]
      end

    end

    # Convenience method to allow configuration options to be set in a block
    #
    # @raise [JPlatform::Error::ConfigurationError] Error is raised when supplied
    #   j_platform credentials are not a String or Symbol.
    def configure
      yield self
      validate_credential_type!
      self
    end

    # @return [Boolean]
    def credentials?
      credentials.values.all?
    end

    def reset!
      JPlatform::Configurable.keys.each do |key|
        instance_variable_set(:"@#{key}", JPlatform::Default.options[key])
      end
      self
    end
    alias setup reset!

  private

    # @return [Hash]
    def credentials
      {
        client_id: @client_id,
        client_secret: @client_secret,
      }
    end

    # @return [Hash]
    def options
      Hash[JPlatform::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
    end

    # Ensures that all credentials set during configuration are of a
    # valid type. Valid types are String and Symbol.
    #
    # @raise [JPlatform::Error::ConfigurationError] Error is raised when
    #   supplied j_platform credentials are not a String or Symbol.
    def validate_credential_type!
      credentials.each do |credential, value|
        next if value.nil?

        unless value.is_a?(String) || value.is_a?(Symbol)
          raise "Invalid #{credential} specified: #{value} must be a string or symbol."
        end
      end
    end

  end
end