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.

Parameters:

  • options (Hash) (defaults to: {})

    The configuration attributes.

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)

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/justimmo/config.rb', line 59

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


112
113
114
# File 'lib/justimmo/config.rb', line 112

def clear
  @config = nil
end

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

Convenience method that sets configuration.

Yields:

  • The configuration block.

Yield Parameters:


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

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

.credentialsObject


125
126
127
# File 'lib/justimmo/config.rb', line 125

def credentials
  @config&.credentials
end

.urlObject


121
122
123
# File 'lib/justimmo/config.rb', line 121

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

Instance Method Details

#api_ver=(version) ⇒ Object


78
79
80
81
82
# File 'lib/justimmo/config.rb', line 78

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

#credentialsObject


88
89
90
91
92
# File 'lib/justimmo/config.rb', line 88

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

#urlObject


84
85
86
# File 'lib/justimmo/config.rb', line 84

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