Sha256: 2da0400cb9e92d248685ef4df70e3e581b2489d61c4d16cd409467008577a6cd

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

require 'yellow_api/version'

module YellowApi
  # Defines constants for default configuration
  module Config
    DEFAULT_APIKEY = nil

    # The endpoint that will be used to connect if none is set and
    # mode is not set to sandbox
    #
    # @see http://www.yellowapi.com/docs/places/#doc_char
    DEFAULT_ENDPOINT = 'http://api.yellowapi.com'.freeze

    # The endpoint that will be used to connect if none is set and
    # mode is set to sandbox
    #
    # @see http://www.yellowapi.com/docs/places/#doc_char
    DEFAULT_SANDBOX_ENDPOINT = 'http://api.sandbox.yellowapi.com'.freeze

    # Sets the configuration for which type of API key is being used
    # @note Changing this to be enabled will automatically switch to using the DEFAULT_SANDBOX_ENDPOINT
    # @see http://www.yellowapi.com/docs/places/#doc_char
    DEFAULT_SANDBOX_ENABLED = false

    # Content type of response to be consumed
    # @note This is currently fixed at JSON, as there is no XML support at this time
    DEFAULT_FMT = 'JSON'.freeze

    # Keys which can be configured
    VALID_OPTIONS_KEYS = [
      :apikey,
      :endpoint,
      :sandbox_endpoint,
      :sandbox_enabled,
      :uid,
      :fmt
    ].freeze

    attr_accessor *VALID_OPTIONS_KEYS

    # When this module is extended, set all configurations back to defaults
    def self.extend(base)
      base.reset
    end

    # Allows configuration options to be set in a block
    def configure
      yield self
      self
    end

    # Create a hash of options and their values
    def options
      options = {}
      VALID_OPTIONS_KEYS.each do |k|
        options[k] = send(k)
      end
      options
    end

    # Reset all configurations back to defaults
    def reset
      self.apikey = DEFAULT_APIKEY
      self.endpoint = DEFAULT_ENDPOINT
      self.sandbox_endpoint = DEFAULT_SANDBOX_ENDPOINT
      self.sandbox_enabled = DEFAULT_SANDBOX_ENABLED
      self.fmt = DEFAULT_FMT
      self.uid = UUID.new.generate(:compact)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yellow-api-wrapper-0.0.2 lib/yellow_api/config.rb
yellow-api-wrapper-0.0.1 lib/yellow_api/config.rb