Class: Collector::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/collector/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(user_name, password, sandbox = false) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
30
31
32
33
# File 'lib/collector/client.rb', line 25

def initialize(user_name, password, sandbox = false)
  @header = {"ClientIpAddress" => "?",
                  "Username" => user_name,
                  "Password" => password }
  url = sandbox ? COLLECTOR_URL_TEST : COLLECTOR_URL
  http = Savon::HTTPClient.new
  http.client.ssl_config.ssl_version = 'TLSv1'
  @savon = Savon.new(url, http)
end

Instance Method Details

#activate_invoice(options) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/collector/client.rb', line 111

def activate_invoice(options)
  request = ActivateInvoiceRequest.new(options)
  validate_attributes(request)
  operation = nil
  if request.article_list.nil?
    operation = :ActivateInvoice
    request = ActivateInvoiceRequestRepresenter.new(request)
  else
    operation = :PartActivateInvoice
    request = PartActivateInvoiceRequestRepresenter.new(request)
  end
  resp = perform_operation(operation, request)
  InvoiceResponse.new(resp)
end

#add_invoice(invoice_request) ⇒ Object



75
76
77
78
79
# File 'lib/collector/client.rb', line 75

def add_invoice(invoice_request)
  validate_attributes(invoice_request)
  resp = perform_operation(:AddInvoice, InvoiceRequestRepresenter.new(invoice_request))
  InvoiceResponse.new(resp)
end

#adjust_invoice(options) ⇒ Object



97
98
99
100
101
102
# File 'lib/collector/client.rb', line 97

def adjust_invoice(options)
  request = AdjustInvoiceRequest.new(options)
  validate_attributes(request)
  resp = perform_operation(:AdjustInvoice, AdjustInvoiceRequestRepresenter.new(request))
  resp[:correlation_id]
end

#cancel_invoice(options) ⇒ Object



90
91
92
93
94
95
# File 'lib/collector/client.rb', line 90

def cancel_invoice(options)
  request = CancelInvoiceRequest.new(options)
  validate_attributes(request)
  resp = perform_operation(:CancelInvoice, CancelInvoiceRequestRepresenter.new(request))
  resp[:correlation_id]
end

#get_address(options) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/collector/client.rb', line 81

def get_address(options)
  request = GetAddressRequest.new(options.merge({country_code: "SE"}))
  validate_attributes(request)
  resp = perform_operation(:GetAddress, GetAddressRequestRepresenter.new(request))
  user = User.new
  UserRepresenter.new(user).from_hash(resp)
  user
end

#operation_with_name(operation_name) ⇒ Object



35
36
37
38
39
# File 'lib/collector/client.rb', line 35

def operation_with_name(operation_name)
  @savon.operation(SERVICE_NAME, PORT_NAME, operation_name).tap do |operation|
    operation.header = @header
  end
end

#part_activate_invoice(options) ⇒ Object



126
127
128
129
130
131
# File 'lib/collector/client.rb', line 126

def part_activate_invoice(options)
  if options[:article_list].nil?
    raise ArgumentError.new("Required parameter article_list missing.")
  end
  activate_invoice(options)
end

#perform_operation(operation_name, request) ⇒ Object

operation_name is a symbol named as in the Collector API, eg :AddInvoice request is a hash or an object responding to to_hash



66
67
68
69
70
71
72
73
# File 'lib/collector/client.rb', line 66

def perform_operation(operation_name, request)
  operation = operation_with_name operation_name
  operation.body = request.to_hash.with_indifferent_access
  response = operation.call.body
  raise_error(response) unless response[:fault].nil?
  namespace = response.keys.first
  response[namespace].with_indifferent_access
end

#raise_error(response_hash) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/collector/client.rb', line 41

def raise_error(response_hash)
  fault = response_hash[:fault]
  err_class = CollectorError
  case fault[:faultcode]
  when "s:INVOICE_NOT_FOUND"
    err_class = InvoiceNotFoundError
  when "s:INVALID_INVOICE_STATUS"
    err_class = InvalidInvoiceStatusError
  when "s:INVALID_TRANSACTION_AMOUNT"
    err_class = InvalidTransactionAmountError
  when "s:AUTHORIZATION_FAILED"
    err_class = AuthorizationFailedError
  end
  faultcode = fault[:faultcode].split(":").last
  raise err_class.send(:new, faultcode, fault[:faultstring])
end

#replace_invoice(options) ⇒ Object



104
105
106
107
108
109
# File 'lib/collector/client.rb', line 104

def replace_invoice(options)
  request = ReplaceInvoiceRequest.new(options)
  validate_attributes(request)
  resp = perform_operation(:ReplaceInvoice, ReplaceInvoiceRequestRepresenter.new(request))
  InvoiceResponse.new(resp)
end

#validate_attributes(request_object) ⇒ Object



58
59
60
61
62
# File 'lib/collector/client.rb', line 58

def validate_attributes(request_object)
  unless request_object.has_required_attributes?
    raise ArgumentError.new(request_object.missing_attributes_human_readable)
  end
end