Class: Justimmo::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/justimmo/config.rb

Overview

Holds configuration for the Justimmo API.

Defined Under Namespace

Classes: ConfigurationError, MissingConfiguration, UnsupportedAPIVersion

Constant Summary collapse

DEFAULTS =
{
  base_url: 'https://api.justimmo.at/rest',
  api_ver:  1,
  username: nil,
  password: nil,
  on_mapper_error: :mark,
  debug: false
}.freeze
SUPPORTED_API_VERSIONS =
[1].freeze
REQUIRED =
i[username password].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|config| ... } ⇒ Config

Justimmo API configuration. The configuration options are validated on creation.

Options Hash (options):

  • :base_url (String)

    The first part of the request URL.

  • :api_ver (Integer)

    The API version.

  • :username (String)

    The username for authentication.

  • :password (String)

    The password for authentication.

  • :on_mapper_error (Symbol)

    The action to use when a mapper lookup fails.

  • :debug (Boolean)

    Enable debug mode.

Yields:

Yield Parameters:

  • config (self)

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/justimmo/config.rb', line 89

def initialize(options = {})
  @attributes = DEFAULTS.merge(options)

  @attributes.keys.reject { |k| k == :api_ver }.each do |key|
    define_singleton_method("#{key}=") do |x|
      @attributes[key] = x
    end
  end

  @attributes.each do |key, value|
    meth = [true, false].include?(value) ? "#{key}?" : key
    define_singleton_method(meth) { @attributes[key] }
  end

  yield(self) if block_given?

  validate
end

Class Method Details

.clearObject


49
50
51
# File 'lib/justimmo/config.rb', line 49

def clear
  @config = nil
end

.configure(options = {}) {|config| ... } ⇒ Object

Convenience method that sets configuration.

Yields:

  • The configuration block.

Yield Parameters:


45
46
47
# File 'lib/justimmo/config.rb', line 45

def configure(options = {})
  @config = new(options) { |config| yield(config) if block_given? }
end

.configured?Boolean


61
62
63
# File 'lib/justimmo/config.rb', line 61

def configured?
  @config || false
end

.credentialsObject


57
58
59
# File 'lib/justimmo/config.rb', line 57

def credentials
  @config&.credentials
end

.urlObject


53
54
55
# File 'lib/justimmo/config.rb', line 53

def url
  @config&.url || "#{DEFAULTS[:base_url]}/v#{DEFAULTS[:api_ver]}"
end

Instance Method Details

#api_ver=(version) ⇒ Object


108
109
110
111
112
# File 'lib/justimmo/config.rb', line 108

def api_ver=(version)
  supported_ver = SUPPORTED_API_VERSIONS.include?(version)
  raise UnsupportedAPIVersion, version unless supported_ver
  @attributes[:api_ver] = version
end

#credentialsObject


118
119
120
121
122
# File 'lib/justimmo/config.rb', line 118

def credentials
  username = @attributes[:username]
  password = @attributes[:password]
  @credentials ||= Base64.urlsafe_encode64("#{username}:#{password}")
end

#urlObject


114
115
116
# File 'lib/justimmo/config.rb', line 114

def url
  "#{@attributes[:base_url]}/v#{@attributes[:api_ver]}"
end