Sha256: 252007096e6e566a3c2bbaa2a74d666bc54f75ed2ab14382b8eb8a213947bbff

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

require 'evrythng/version'

module Evrythng
  # Defines constants and methods related to configuration
  module Config
    # An array of valid keys in the options hash when configuring a {Evrythng::API}
    VALID_OPTIONS_KEYS = [
      :adapter,
      :connection_options,
      :token,
      :endpoint,
      :gateway,
      :proxy,
      :user_agent]

    # The adapter that will be used to connect if none is set
    DEFAULT_ADAPTER = :net_http

    # The Faraday connection options if none is set
    DEFAULT_CONNECTION_OPTIONS = {}

    # By default, don't set a token
    DEFAULT_TOKEN = nil

    # The endpoint that will be used to connect if none is set
    DEFAULT_ENDPOINT = 'http://evrythng.com'

    # By default, don't use a proxy server
    DEFAULT_PROXY = nil

    # The user agent that will be sent to the API endpoint if none is set
    DEFAULT_USER_AGENT = "Evrythng Ruby Gem #{Evrythng::VERSION}"

    # By default, don't use a gateway server
    DEFAULT_GATEWAY = nil

    # @private
    attr_accessor *VALID_OPTIONS_KEYS

    # When this module is extended, set all configuration options to their default values
    def self.extended(base)
      base.reset
    end

    # Convenience method to allow configuration options to be set in a block
    def configure
      yield self
    end

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

    # Reset all configuration options to defaults
    def reset
      self.adapter            = DEFAULT_ADAPTER
      self.connection_options = DEFAULT_CONNECTION_OPTIONS
      self.token              = DEFAULT_TOKEN
      self.endpoint           = DEFAULT_ENDPOINT
      self.proxy              = DEFAULT_PROXY
      self.user_agent         = DEFAULT_USER_AGENT
      self.gateway            = DEFAULT_GATEWAY
      self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
evrythng-0.1.1 lib/evrythng/config.rb
evrythng-0.1.0 lib/evrythng/config.rb