Module: Klarna

Defined in:
lib/klarna.rb,
lib/klarna/api.rb,
lib/klarna/version.rb,
lib/klarna/api/client.rb,
lib/klarna/api/errors.rb,
lib/klarna/api/methods.rb,
lib/klarna/api/constants.rb,
lib/klarna/api/methods/standard.rb,
lib/klarna/api/methods/invoicing.rb,
lib/klarna/api/methods/reservation.rb,
lib/klarna/api/methods/cost_calculations.rb

Defined Under Namespace

Modules: API Classes: KlarnaConfigError

Constant Summary collapse

DEFAULT_STORE_CONFIG_FILE =
File.join(ENV['HOME'], '.klarna.yml')
VALID_COUNTRIES =
[:SE, :NO, :FI, :DK]
DEFAULT_COUNTRY =
VALID_COUNTRIES.first
DEFAULT_MODE =
:test
VERSION =
'0.1.3'
@@mode =
:test
@@country =
::Klarna::DEFAULT_COUNTRY
@@store_id =
nil
@@store_secret =
nil
@@store_pclasses =
nil
@@store_config_file =
::Klarna::DEFAULT_STORE_CONFIG_FILE
@@logger =
::Logger.new(::STDOUT)
@@logging =
false
@@http_logging =
false

Class Method Summary collapse

Class Method Details

.country=(value) ⇒ Object



155
156
157
# File 'lib/klarna.rb', line 155

def country=(value)
  @@country = value.to_s.upcase.to_sym rescue ::Klarna::DEFAULT_COUNTRY
end

.default_countryObject



167
168
169
# File 'lib/klarna.rb', line 167

def default_country
  ::Klarna::DEFAULT_COUNTRY
end

.load_credentials_from_file(force = false) ⇒ Object

Optional: Try to load credentials from a system file.



136
137
138
139
140
141
142
143
144
145
# File 'lib/klarna.rb', line 136

def load_credentials_from_file(force = false)
  begin
    store_config = File.open(self.store_config_file) { |file| YAML.load(file).with_indifferent_access }
    self.store_id = store_config[self.mode][:store_id] if force || self.store_id.nil?
    self.store_secret = store_config[self.mode][:store_secret] if force || self.store_secret.nil?
    self.store_pclasses = store_config[self.mode][:store_pclasses] if force || self.store_pclasses.nil?
  rescue
    raise KlarnaConfigError, "Could not load store details from: #{self.store_config_file.inspect}"
  end
end

.log(message, level = :info) ⇒ Object

Logging helper for debugging purposes.



119
120
121
122
123
124
# File 'lib/klarna.rb', line 119

def log(message, level = :info)
  return unless self.logging?
  level = :info if level.blank?
  self.logger ||= ::Logger.new(::STDOUT)
  self.logger.send(level.to_sym, "[klarna:]  #{level.to_s.upcase}  #{message}")
end

.log_result(label, &block) ⇒ Object

Logging helper for debugging “log return value”-cases.



128
129
130
131
132
# File 'lib/klarna.rb', line 128

def log_result(label, &block)
  result = block.call
  self.log label % result.inspect
  result
end

.mode=(value) ⇒ Object



159
160
161
# File 'lib/klarna.rb', line 159

def mode=(value)
  @@mode = value.to_s.downcase.to_sym rescue ::Klarna::DEFAULT_MODE
end

.reset!Object

Reset to defaults - mostly usable in specs.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/klarna.rb', line 105

def reset!
  self.mode = ::Klarna::DEFAULT_MODE
  self.country = ::Klarna::DEFAULT_COUNTRY
  self.store_id = nil
  self.store_secret = nil
  self.store_pclasses = nil
  self.store_config_file = ::Klarna::DEFAULT_STORE_CONFIG_FILE
  self.logger = ::Logger.new(::STDOUT)
  self.logging = false
  self.http_logging = false
end

.setup {|_self| ... } ⇒ Object Also known as: configure

Configuration DSL helper method.

Usage/Example:

Klarna::Setup do |config|
  config.country = :SE
  # etc.
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Klarna)

    the object that the method was called on



97
98
99
100
# File 'lib/klarna.rb', line 97

def setup
  yield self
  self.load_credentials_from_file unless self.store_id || self.store_secret
end

.store_id=(value) ⇒ Object



147
148
149
# File 'lib/klarna.rb', line 147

def store_id=(value)
  @@store_id = value ? value.to_i : value
end

.store_secret=(value) ⇒ Object



151
152
153
# File 'lib/klarna.rb', line 151

def store_secret=(value)
  @@store_secret = value ? value.to_s.strip : value
end

.valid_countriesObject



163
164
165
# File 'lib/klarna.rb', line 163

def valid_countries
  ::Klarna::VALID_COUNTRIES
end