Module: Klarna::API
- Includes:
- Constants, Errors
- Defined in:
- lib/klarna.rb,
lib/klarna/api.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: Constants, Errors, Methods
Classes: Client
Constant Summary
collapse
- @@client =
nil
Constants included
from Errors
Errors::ERROR_CODES
Constants included
from Constants
Constants::ADDRESS_FORMATS, Constants::AVERAGE_INTEREST_PERIOD, Constants::CHARGE_TYPES, Constants::COUNTRIES, Constants::COUNTRY_DEFAULTS, Constants::CURRENCIES, Constants::DAYS_IN_A_YEAR, Constants::DEFAULTS, Constants::END_POINT, Constants::GENDERS, Constants::GOODS, Constants::INTEREST_RATES, Constants::INVOICE, Constants::LANGUAGES, Constants::LOWEST_PAYMENT_BY_COUNTRY, Constants::LOWEST_PAYMENT_BY_CURRENCY, Constants::MOBILE, Constants::MONTHLY_COST, Constants::PCLASS, Constants::PNO_FORMATS, Constants::PROTOCOL_ENCODING, Constants::PROTOCOL_VERSION, Constants::SHIPMENT_TYPES
Class Method Summary
collapse
-
.client(force_new = false) ⇒ Object
Re-use or (re-)initialize a new Klarna XML-RPC API client.
-
.decode(string, from_encoding = ::Klarna::API::PROTOCOL_ENCODING) ⇒ Object
-
.digest(*args) ⇒ Object
-
.encode(string, from_encoding = 'utf-8') ⇒ Object
-
.id_for(kind, value) ⇒ Object
-
.key_for(kind, value) ⇒ Object
-
.parse_flags(constant_name, flags) ⇒ Object
-
.validate_arg(value, cast_to, format_expression, strip_expression = nil, &block) ⇒ Object
Parse, validate, and cast a method argument before RPC-call.
-
.validated_kind(kind) ⇒ Object
Validate if specified kind
is a valid constant.
Methods included from Errors
error_message
Class Method Details
.client(force_new = false) ⇒ Object
Re-use or (re-)initialize a new Klarna XML-RPC API client.
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/klarna/api.rb', line 42
def client(force_new = false)
begin
if force_new || @@client.nil?
@@client = ::Klarna::API::Client.new
end
rescue => e
::Klarna.log e, :error
end
@@client
end
|
.decode(string, from_encoding = ::Klarna::API::PROTOCOL_ENCODING) ⇒ Object
175
176
177
178
179
180
181
182
|
# File 'lib/klarna/api.rb', line 175
def decode(string, from_encoding = ::Klarna::API::PROTOCOL_ENCODING)
if string.respond_to?(:encode)
string.encode('utf-8', from_encoding)
else
::Iconv rescue require 'iconv'
::Iconv.conv(from_encoding, ::Klarna::API::PROTOCOL_ENCODING, string)
end
end
|
.digest(*args) ⇒ Object
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/klarna/api.rb', line 156
def digest(*args)
string = args.join(':')
iso_value = self.encode(string)
hex_md5_digest = [*::Digest::MD5.hexdigest(iso_value)].pack('H*')
base64_digest = ::XMLRPC::Base64.encode(hex_md5_digest).strip
hex_sha512_digest = [*Digest::SHA512.hexdigest(iso_value)].pack('H*')
base64_digest = ::XMLRPC::Base64.encode(hex_sha512_digest).strip
end
|
.encode(string, from_encoding = 'utf-8') ⇒ Object
167
168
169
170
171
172
173
|
# File 'lib/klarna/api.rb', line 167
def encode(string, from_encoding = 'utf-8')
if string.respond_to?(:encode)
string.encode(::Klarna::API::PROTOCOL_ENCODING, from_encoding)
else
::Iconv.conv(::Klarna::API::PROTOCOL_ENCODING, from_encoding, string)
end
end
|
.id_for(kind, value) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/klarna/api.rb', line 74
def id_for(kind, value)
begin
kind = validated_kind(kind)
rescue => e
raise e
end
begin
is_correct_format = (value.to_s =~ /^\d+$/)
constants = ::Klarna::API.const_get(kind)
id = is_correct_format ? value.to_i : constants[value.to_s.upcase.to_sym]
rescue
raise ::Klarna::API::KlarnaArgumentError, "Invalid '#{kind}': #{value.inspect}"
end
if !id
raise ::Klarna::API::KlarnaStandardError, "Id not found for '#{kind}': #{value.inspect}"
end
id.to_i
end
|
.key_for(kind, value) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/klarna/api.rb', line 53
def key_for(kind, value)
begin
kind = validated_kind(kind)
rescue => e
raise e
end
begin
is_correct_format = !(value.to_s =~ /^\d+$/)
constants = ::Klarna::API.const_get(kind)
key = is_correct_format ? value : constants.invert[value.to_i]
rescue
raise ::Klarna::API::KlarnaArgumentError, "Invalid '#{kind}': #{value.inspect}"
end
if !key
raise ::Klarna::API::KlarnaStandardError, "Key not found for '#{kind}': #{value.inspect}"
end
key.to_sym
end
|
.parse_flags(constant_name, flags) ⇒ Object
147
148
149
150
151
152
153
154
|
# File 'lib/klarna/api.rb', line 147
def parse_flags(constant_name, flags)
if flags.is_a?(Hash)
flags = flags.sum do |k, v|
v ? ::Klarna::API.const_get(constant_name.to_s.upcase.to_sym)[k.to_s.upcase.to_sym] : 0
end
end
flags.to_i
end
|
.validate_arg(value, cast_to, format_expression, strip_expression = nil, &block) ⇒ Object
Parse, validate, and cast a method argument before RPC-call.
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/klarna/api.rb', line 118
def validate_arg(value, cast_to, format_expression, strip_expression = nil, &block)
raise ::Klarna::API::KlarnaArgumentError,
"Argument cast_to should be Symbol, but was #{cast_to.class.name}." unless cast_to.is_a?(Symbol)
raise ::Klarna::API::KlarnaArgumentError,
"Argument regexp should be Regexp, but was #{format_expression.class.name}." unless format_expression.is_a?(Regexp)
raise ::Klarna::API::KlarnaArgumentError,
"Argument strip should be Regexp, but was #{strip_expression.class.name}." unless strip_expression.is_a?(Regexp)
value = value.to_s.gsub(strip_expression, '') if strip_expression
unless value.to_s =~ format_expression
raise ::Klarna::API::KlarnaArgumentError, "Invalid argument: #{value.inspect}. Expected format: #{format_expression.inspect}"
end
value = block.call(value) if block_given?
value.tap do |v|
case cast_to
when :string then v.to_s
when :integer then v.to_i
when :decimal then v.to_f
when :date then v
else
raise ::Klarna::API::KlarnaArgumentError, "Invalid cast_to value: #{cast_to.inspect}. "
end
end
end
|
.validated_kind(kind) ⇒ Object
Validate if specified kind
is a valid constant.
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/klarna/api.rb', line 97
def validated_kind(kind)
valid_kinds = [:country, :currency, :language, :pno_format, :address_format, :shipment_type, :pclass, :mobile, :invoice, :goods, :monthly_cost]
valid_kinds.collect! do |valid_kind|
[valid_kind.to_s.singularize.to_sym, valid_kind.to_s.pluralize.to_sym]
end
valid_kinds.flatten!
kind = kind.to_s.pluralize.to_sym
unless kind.is_a?(String) || kind.is_a?(Symbol)
raise ::Klarna::API::KlarnaArgumentError, "Not a valid kind: #{kind.inspect}. Expects a symbol or a string: #{valid_kinds.join(', ')}"
end
unless valid_kinds.include?(kind.to_s.downcase.to_sym)
raise ::Klarna::API::KlarnaArgumentError, "Not a valid kind: #{kind.inspect}. Valid kinds: #{valid_kinds.join(', ')}"
end
kind.to_s.upcase.to_sym
end
|