module Fakturownia::API

Public Class Methods

client(client_id) click to toggle source

Get client based on client_id Return in XML

# File lib/fakturownia/api.rb, line 112
def self.client client_id
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/clients/#{client_id}.xml"
  response = self.make_get_request(endpoint)

  (response.code == '200') ? response.body : nil
end
client_by_email_and_password(email, password) click to toggle source

Get client based on email and password Return in XML

# File lib/fakturownia/api.rb, line 121
def self.client_by_email_and_password email, password
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/clients/check"
  response = self.make_get_request(endpoint, { :email => email, :password => password })

  (response.code == '200' and response.body.to_s != 'brak klienta') ? response.body : nil
end
clients() click to toggle source

Get all clients in XML

# File lib/fakturownia/api.rb, line 103
def self.clients
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/clients.xml"
  response = self.make_get_request(endpoint)

  (response.code == '200') ? response.body : nil
end
create(invoice_json) click to toggle source

Create new invoice

# File lib/fakturownia/api.rb, line 46
def self.create invoice_json
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/invoices.json"
  uri = URI.parse(endpoint) 

  json_params = { 
    "api_token" => Fakturownia.api_token,
    "invoice" => invoice_json
  }

  request = Net::HTTP::Post.new(uri.path) 
  request.body = JSON.generate(json_params) 
  request["Content-Type"] = "application/json" 
  http = Net::HTTP.new(uri.host, uri.port) 
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.start {|h| h.request(request)} 
  (response.code == '201') ? JSON.parse(response.body) : nil
end
delete(invoice_id) click to toggle source

Delete invoice

# File lib/fakturownia/api.rb, line 90
def self.delete invoice_id
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/invoices/#{invoice_id}.json"
  uri = URI.parse(endpoint) 
  whole_url = uri.path + "?api_token=#{Fakturownia.api_token}"

  http = Net::HTTP.new(uri.host, uri.port) 
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.delete(whole_url)
end
invoice(invoice_id) click to toggle source

Get JSON-formatted invoice based on invoice id (from Fakturownia)

# File lib/fakturownia/api.rb, line 6
def self.invoice invoice_id
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/invoices/#{invoice_id}.json"
  response = self.make_get_request(endpoint)

  (response.code == '200') ? JSON.parse(response.body) : nil 
end
invoice_by_order_id(order_id) click to toggle source

Get JSON-formatted invoice based on order id

# File lib/fakturownia/api.rb, line 14
def self.invoice_by_order_id order_id
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/invoices"
  response = self.make_get_request(endpoint, { :oid => order_id })

  (response.code == '200') ? JSON.parse(response.body) : nil 
end
invoices(period, date_from = nil, date_to = nil) click to toggle source

Get JSON-formatted invoices based from given period Available periods = [:all, :this_month, :last_month, :this_year, :last_year, :more] If period = :more than date_from and date_to has to be set

# File lib/fakturownia/api.rb, line 24
def self.invoices period, date_from = nil, date_to = nil
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/invoices.json?period=#{period}"
  additional_params = { :period => period }
  if period.to_s == 'more'
    additional_params[:date_from] = date_from
    additional_params[:date_to] = date_to
  end
  response = self.make_get_request(endpoint, additional_params)

  (response.code == '200') ? JSON.parse(response.body) : nil 
end
make_get_request(endpoint, additional_params = {}) click to toggle source

Make GET request returning response

# File lib/fakturownia/api.rb, line 130
def self.make_get_request endpoint, additional_params = {}
  uri = URI.parse(endpoint)
  whole_url = uri.path + "?api_token=#{Fakturownia.api_token}"
  additional_params.each_pair do |k, v|
    whole_url += "&#{k}=#{v}"
  end

  request = Net::HTTP::Get.new(whole_url) 
  http = Net::HTTP.new(uri.host, uri.port) 
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.start {|h| h.request(request)}
end
pdf(invoice_id) click to toggle source

Get invoice PDF based on invoice id

# File lib/fakturownia/api.rb, line 37
def self.pdf invoice_id
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/invoices/#{invoice_id}.pdf"
  response = self.make_get_request(endpoint)
   
  (response.code == '200') ? response.body : nil
end
update(invoice_id, invoice_json) click to toggle source

Update invoice invoice_id - id of invoice from fakturownia

# File lib/fakturownia/api.rb, line 68
def self.update invoice_id, invoice_json
  endpoint = "https://#{Fakturownia.account_name}.fakturownia.pl/invoices/#{invoice.fakturownia_id}.json"
  uri = URI.parse(endpoint) 

  json_params = { 
    "api_token" => Fakturownia.api_token,
    "invoice" => invoice_json
  }

  request = Net::HTTP::Put.new(uri.path) 
  request.body = JSON.generate(json_params) 
  request["Content-Type"] = "application/json" 
  http = Net::HTTP.new(uri.host, uri.port) 
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.start {|h| h.request(request)} 

  (response.code == '201') ? JSON.parse(response.body) : nil
end