class Debitoor::Client

Public Class Methods

new(token) click to toggle source
# File lib/debitoor/client.rb, line 9
def initialize(token)
  @token = token
  @options = { headers: {
    "x-token" => token,
    "Content-Type" => 'application/json'
  } }
end

Public Instance Methods

book_draft_invoice(id_or_response_invoice) click to toggle source
# File lib/debitoor/client.rb, line 28
def book_draft_invoice(id_or_response_invoice)
  options = {
    query: {updateAutonumber: true, autonumbering: true, autonumber: true}
  }.merge!(@options)

  response = nil
  id = invoice_id(id_or_response_invoice)

  response = self.class.post("/api/sales/draftinvoices/#{id}/book/v3", options) if id.present?
  respond_with!(response)
end
create_draft_invoice(options={}) click to toggle source

Public: Creates a draft invoice and accepts a body containing

# File lib/debitoor/client.rb, line 18
def create_draft_invoice(options={})
  options = options.merge!(@options)
  response = self.class.post("/api/sales/draftinvoices/v3?autonumber=true", options)
  respond_with!(response)
end
email_invoice(id_or_response_invoice, email, subject="Here's your receipt", message="") click to toggle source
# File lib/debitoor/client.rb, line 52
def email_invoice(id_or_response_invoice, email, subject="Here's your receipt", message="")
  options = {
    body: {recipient: email, subject: subject, message: message}.to_json
  }.merge!(@options)

  response = nil
  id = invoice_id(id_or_response_invoice)

  response = self.class.post("/api/sales/invoices/#{id}/email/v2", options) if id.present?
  respond_with!(response)
end
get_last_invoice() click to toggle source
# File lib/debitoor/client.rb, line 24
def get_last_invoice
  self.class.get("/api/sales/invoices/v3?limit=1", @options)
end
share_invoice(id_or_response_invoice, email, subject="Here's your receipt") click to toggle source
# File lib/debitoor/client.rb, line 40
def share_invoice(id_or_response_invoice, email, subject="Here's your receipt")
  options = {
    body: {recipient: email, subject: subject}.to_json
  }.merge!(@options)

  response = nil
  id = invoice_id(id_or_response_invoice)

  response = self.class.post("/api/sales/invoices/#{id}/share/v1", options) if id.present?
  respond_with!(response)
end

Private Instance Methods

invoice_id(id_or_response_invoice) click to toggle source
# File lib/debitoor/client.rb, line 66
def invoice_id(id_or_response_invoice)
  id = nil
  id = id_or_response_invoice if id_or_response_invoice.kind_of?(String)

  if (id_or_response_invoice.class == HTTParty::Response) && id_or_response_invoice.code.to_s.start_with?("2")
    id = id_or_response_invoice.parsed_response["id"]
  end

  return id
end
respond_with!(response) click to toggle source
# File lib/debitoor/client.rb, line 77
def respond_with!(response)
  raise(response.inspect) unless response.code.to_s.start_with?("2")
  return response
end