class Sendregning::Client

Client for the SendRegning Web Services.

Usage example:

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

Public Class Methods

new(username, password, options = {}) click to toggle source
# File lib/sendregning/client.rb, line 18
def initialize(username, password, options = {})
  @auth = { username: username, password: password }
  @test = true if options[:test]
end

Public Instance Methods

find_invoice(invoice_id) click to toggle source

Finds an invoice by invoice number

# 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 = {}) click to toggle source

Performs a GET request

# File lib/sendregning/client.rb, line 24
def get(query = {})
  self.class.get("/ws/butler.do", query_options(query))
end
new_invoice(attributes = {}) click to toggle source

Instances a new invoice

# File lib/sendregning/client.rb, line 46
def new_invoice(attributes = {})
  Sendregning::Invoice.new(attributes.merge({ client: self }))
end
post(query = nil) click to toggle source

Performs a POST request

# 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 = {}) click to toggle source
# 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
recipients() click to toggle source

Returns a list of recipients

# File lib/sendregning/client.rb, line 40
def recipients
  response = get(action: "select", type: "recipient")
  response["recipients"]["recipient"]
end
send_invoice(invoice) click to toggle source

Sends an invoice

# 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

Protected Instance Methods

query_options(query = {}) click to toggle source

Prepares options for a request

# File lib/sendregning/client.rb, line 78
def query_options(query = {})
  query[:test] = "true" if @test
  { basic_auth: @auth, query: query }
end
xml_file(xml) click to toggle source
# File lib/sendregning/client.rb, line 83
def xml_file(xml)
  UploadIO.new(StringIO.new(xml), "text/xml", "request.xml")
end