Class: Klarna::API::Client

Inherits:
XMLRPC::Client
  • Object
show all
Includes:
Methods
Defined in:
lib/klarna/api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Methods::CostCalculations

#calculate_monthly_cost, #fetch_pclasses, #monthly_cost, #periodic_cost

Methods included from Methods::Reservation

#activate_reservation, #cancel_reservation, #change_reservation, #make_reservation_address, #reserve_amount, #reserve_ocr_numbers, #split_reservation

Methods included from Methods::Invoicing

#activate_invoice, #add_invoice, #credit_invoice, #delete_invoice, #email_invoice, #invoice_address, #invoice_amount, #invoice_paid?, #make_article, #return_amount, #send_invoice, #update_charge_amount, #update_goods_quantity, #update_order_no

Methods included from Methods::Standard

#get_address, #get_addresses, #has_account?, #make_address, #make_goods

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/klarna/api/client.rb', line 24

def initialize(*args)
  self.store_id = args.shift || ::Klarna.store_id
  self.store_secret = args.shift || ::Klarna.store_secret

  if self.store_id.blank? || self.store_secret.blank?
    raise ::Klarna::API::KlarnaCredentialsError, "Both 'store_id' and 'store_secret' must be set."
  end

  options = args.extract_options!
  self.client_ip = options[:client_ip].presence || '127.0.0.1'
  self.mode = options.key?(:mode) ? options[:mode] : ::Klarna.mode
  self.timeout = options.key?(:timeout) ? options[:timeout] : 10 # seconds

  unless ::Klarna::API::END_POINT.keys.include?(self.mode)
    raise "No such mode: #{self.mode.inspect}. " <<
    "Valid modes: #{::Klarna::API::END_POINT.keys.collect(&:inspect).join(', ')}"
  end

  begin
    ::Klarna.log "Endpoint URI: %s" % self.endpoint_uri.inspect

    super(self.host, '/', self.port, nil, nil, nil, nil, self.use_ssl?, self.timeout)

    self.http_header_extra ||= {}
    self.http_header_extra.merge!(self.content_type_headers)

    self.instance_variable_get(:@http).set_debug_output(::Klarna.logger) if ::Klarna.http_logging?
  rescue ::XMLRPC::FaultException => e
    raise ::Klarna::API::KlarnaServiceError.new(e.faultCode, e.faultString)
  end
end

Instance Attribute Details

#client_ip ⇒ Object

Returns the value of attribute client_ip.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def client_ip
  @client_ip
end

#host ⇒ Object

Returns the value of attribute host.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def host
  @host
end

#last_request_headers ⇒ Object

Returns the value of attribute last_request_headers.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def last_request_headers
  @last_request_headers
end

#last_request_params ⇒ Object

Returns the value of attribute last_request_params.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def last_request_params
  @last_request_params
end

#last_response ⇒ Object

Returns the value of attribute last_response.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def last_response
  @last_response
end

#mode ⇒ Object

Returns the value of attribute mode.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def mode
  @mode
end

#port ⇒ Object

Returns the value of attribute port.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def port
  @port
end

#protocol ⇒ Object

Returns the value of attribute protocol.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def protocol
  @protocol
end

#store_id ⇒ Object

Returns the value of attribute store_id.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def store_id
  @store_id
end

#store_secret ⇒ Object

Returns the value of attribute store_secret.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def store_secret
  @store_secret
end

#timeout ⇒ Object

Returns the value of attribute timeout.



12
13
14
# File 'lib/klarna/api/client.rb', line 12

def timeout
  @timeout
end

Instance Method Details

#call(service_method, *args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/klarna/api/client.rb', line 56

def call(service_method, *args)
  args.collect! { |arg| arg.is_a?(String) ? ::Klarna::API.decode(arg) : arg }
  ::Klarna.log "Method: #{service_method}"
  ::Klarna.log "Params: %s" % args.inspect #self.add_meta_params(*args).inspect

  self.last_request_headers = http_header_extra

  begin
    ::Klarna.log_result("Result: %s") do
      params = args #self.add_meta_params(*args)
      self.last_request_params = params
      self.last_response = super(service_method, *params)
    end
  rescue ::XMLRPC::FaultException => e
    raise ::Klarna::API::KlarnaServiceError.new(e.faultCode, e.faultString)
  end
end

#endpoint_uri ⇒ Object



91
92
93
# File 'lib/klarna/api/client.rb', line 91

def endpoint_uri
  @endpoint_uri = "#{self.protocol}://#{self.host}:#{self.port}"
end

#ssl? ⇒ Boolean Also known as: use_ssl?

Returns:

  • (Boolean)


74
75
76
# File 'lib/klarna/api/client.rb', line 74

def ssl?
  self.protocol == 'https'
end