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.2.1'
@@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



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

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

.default_countryObject



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

def default_country
  ::Klarna::DEFAULT_COUNTRY
end

.load_credentials_from_file(force = false) ⇒ Object

Optional: Try to load credentials from a system file.



132
133
134
135
136
137
138
139
140
141
# File 'lib/klarna.rb', line 132

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.



115
116
117
118
119
120
# File 'lib/klarna.rb', line 115

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.



124
125
126
127
128
# File 'lib/klarna.rb', line 124

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

.mode=(value) ⇒ Object



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

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

.reset!Object

Reset to defaults - mostly usable in specs.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/klarna.rb', line 101

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



93
94
95
96
# File 'lib/klarna.rb', line 93

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

.store_id=(value) ⇒ Object



143
144
145
# File 'lib/klarna.rb', line 143

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

.store_secret=(value) ⇒ Object



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

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

.valid_countriesObject



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

def valid_countries
  ::Klarna::VALID_COUNTRIES
end