Class: Sendregning::Client

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/sendregning/client.rb

Overview

Client for the SendRegning Web Services.

Usage example:

client = SendRegning::Client.new(my_username, my_password)

Instance Method Summary collapse

Constructor Details

#initialize(username, password, options = {}) ⇒ Client

Returns a new instance of Client.



18
19
20
21
# File 'lib/sendregning/client.rb', line 18

def initialize(username, password, options = {})
  @auth = { username: username, password: password }
  @test = true if options[:test]
end

Instance Method Details

#find_invoice(invoice_id) ⇒ Object

Finds an invoice by invoice number



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

def find_invoice(invoice_id)
  builder = Builder::XmlMarkup.new(indent: 2)
  builder.instruct! :xml, version: "1.0", encoding: "UTF-8"
  request_xml = builder.select do |select|
    select.invoiceNumbers do |numbers|
      numbers.invoiceNumber invoice_id
    end
  end
  response = post_xml(request_xml,
                      { action: "select", type: "invoice" })
  begin
    InvoiceParser.parse(response)
  rescue StandardError
    nil
  end
end

#get(query = {}) ⇒ Object

Performs a GET request



24
25
26
# File 'lib/sendregning/client.rb', line 24

def get(query = {})
  self.class.get("/ws/butler.do", query_options(query))
end

#new_invoice(attributes = {}) ⇒ Object

Instances a new invoice



46
47
48
# File 'lib/sendregning/client.rb', line 46

def new_invoice(attributes = {})
  Sendregning::Invoice.new(attributes.merge({ client: self }))
end

#post(query = nil) ⇒ Object

Performs a POST request



29
30
31
# File 'lib/sendregning/client.rb', line 29

def post(query = nil)
  self.class.post("/ws/butler.do", query_options(query))
end

#post_xml(xml, query = {}) ⇒ Object



33
34
35
36
37
# File 'lib/sendregning/client.rb', line 33

def post_xml(xml, query = {})
  query[:xml] = xml_file(xml)
  self.class.post("/ws/butler.do",
                  query_options(query).merge(detect_mime_type: true))
end

#recipientsObject

Returns a list of recipients



40
41
42
43
# File 'lib/sendregning/client.rb', line 40

def recipients
  response = get(action: "select", type: "recipient")
  response["recipients"]["recipient"]
end

#send_invoice(invoice) ⇒ Object

Sends an invoice



51
52
53
54
55
# File 'lib/sendregning/client.rb', line 51

def send_invoice(invoice)
  response = post_xml(invoice.to_xml,
                      { action: "send", type: "invoice" })
  InvoiceParser.parse(response, invoice)
end