Module: Klarna::API
- 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
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::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.
- .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
Class Method Details
.client(force_new = false) ⇒ Object
Re-use or (re-)initialize a new Klarna XML-RPC API client.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/klarna/api.rb', line 44 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 |
.digest(*args) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/klarna/api.rb', line 152 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
163 164 165 |
# File 'lib/klarna/api.rb', line 163 def encode(string, from_encoding = 'utf-8') ::Iconv.conv(::Klarna::API::PROTOCOL_ENCODING, from_encoding, string) end |
.id_for(kind, value) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/klarna/api.rb', line 73 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 id ? id.to_i : nil end |
.key_for(kind, value) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/klarna/api.rb', line 55 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] # BUG: Should lookup the value's key. rescue raise ::Klarna::API::KlarnaArgumentError, "Invalid '#{kind}': #{value.inspect}" end key ? key.to_sym : nil end |
.parse_flags(constant_name, flags) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/klarna/api.rb', line 143 def parse_flags(constant_name, flags) if flags.is_a?(Hash) flags = flags.sum { |k,v| ::Klarna::API.const_get(constant_name.to_s.upcase.to_sym)[k.to_s.upcase.to_sym] } 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.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/klarna/api.rb', line 114 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 # Pass value to block - for type casting, etc. - if given. 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 # number of decimals? when :date then v # TODO 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.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/klarna/api.rb', line 93 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 |