Sha256: 8ac4bfb2b0713eee6bf3f1da8d6ded5d897a7c0e0a2c10f7e58ff776a3ca247c

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require 'logger'

module WialonApi
  module Configuration
    OPTION_NAMES = [
      :wialon_host,
      :wialon_edition,
      :http_verb,
      :max_retries,
      :faraday_options,
      :adapter,
      :logger,
      :log_requests,
      :log_errors,
      :log_responses
    ]

    attr_accessor(*OPTION_NAMES)

    READ_ONLY_OPTIONS = [
      :wialon_session_identifier,
      :wialon_client_session_identifier
    ]

    attr_reader(*READ_ONLY_OPTIONS)

    alias_method :log_requests?,  :log_requests
    alias_method :log_errors?,    :log_errors
    alias_method :log_responses?, :log_responses

    DEFAULT_WIALON_HOST = 'https://hst-api.wialon.com/wialon/ajax.html'
    DEFAULT_WIALON_EDITION = :hosting
    DEFAULT_SESSION_IDENTIFIER = :sid
    DEFAULT_CLIENT_SESSION_IDENTIFIER = :eid
    DEFAULT_HTTP_VERB = :post
    DEFAULT_MAX_RETRIES = 1
    DEFAULT_ADAPTER = Faraday.default_adapter
    DEFAULT_LOGGER_OPTIONS = {
      requests:  true,
      errors:    true,
      responses: false
    }

    def configure
      yield self if block_given?
      self
    end

    def reset
      @wialon_host = DEFAULT_WIALON_HOST
      @wialon_edition = DEFAULT_WIALON_EDITION
      @wialon_session_identifier = DEFAULT_SESSION_IDENTIFIER
      @wialon_client_session_identifier = DEFAULT_CLIENT_SESSION_IDENTIFIER
      @http_verb   = DEFAULT_HTTP_VERB
      @max_retries = DEFAULT_MAX_RETRIES
      @faraday_options = {}
      @adapter         = DEFAULT_ADAPTER
      @logger          = ::Logger.new(STDOUT)
      @log_requests    = DEFAULT_LOGGER_OPTIONS[:requests]
      @log_errors      = DEFAULT_LOGGER_OPTIONS[:errors]
      @log_responses   = DEFAULT_LOGGER_OPTIONS[:responses]
    end

    def self.extended(base)
      base.reset
    end

    def wialon_edition=(edition)
      @wialon_edition = edition
      if edition == :pro
        @wialon_session_identifier = :ssid
        @wialon_client_session_identifier = :ssid
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wialon_api-0.0.8 lib/wialon_api/configuration.rb
wialon_api-0.0.7 lib/wialon_api/configuration.rb
wialon_api-0.0.6 lib/wialon_api/configuration.rb