Module: Fakturownia::API

Defined in:
lib/fakturownia/api.rb

Class Method Summary collapse

Class Method Details

.client(client_id) ⇒ Object

Get client based on client_id Return in XML



112
113
114
115
116
117
# File 'lib/fakturownia/api.rb', line 112

def self.client client_id
  endpoint = "https://#{Fakturownia.}.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) ⇒ Object

Get client based on email and password Return in XML



121
122
123
124
125
126
# File 'lib/fakturownia/api.rb', line 121

def self.client_by_email_and_password email, password
  endpoint = "https://#{Fakturownia.}.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

.clientsObject

Get all clients in XML



103
104
105
106
107
108
# File 'lib/fakturownia/api.rb', line 103

def self.clients
  endpoint = "https://#{Fakturownia.}.fakturownia.pl/clients.xml"
  response = self.make_get_request(endpoint)

  (response.code == '200') ? response.body : nil
end

.create(invoice_json) ⇒ Object

Create new invoice



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fakturownia/api.rb', line 46

def self.create invoice_json
  endpoint = "https://#{Fakturownia.}.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) ⇒ Object

Delete invoice



90
91
92
93
94
95
96
97
98
99
# File 'lib/fakturownia/api.rb', line 90

def self.delete invoice_id
  endpoint = "https://#{Fakturownia.}.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) ⇒ Object

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



6
7
8
9
10
11
# File 'lib/fakturownia/api.rb', line 6

def self.invoice invoice_id
  endpoint = "https://#{Fakturownia.}.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) ⇒ Object

Get JSON-formatted invoice based on order id



14
15
16
17
18
19
# File 'lib/fakturownia/api.rb', line 14

def self.invoice_by_order_id order_id
  endpoint = "https://#{Fakturownia.}.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) ⇒ Object

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



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fakturownia/api.rb', line 24

def self.invoices period, date_from = nil, date_to = nil
  endpoint = "https://#{Fakturownia.}.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 = {}) ⇒ Object

Make GET request returning response



130
131
132
133
134
135
136
137
138
139
140
141
142
# 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) ⇒ Object

Get invoice PDF based on invoice id



37
38
39
40
41
42
# File 'lib/fakturownia/api.rb', line 37

def self.pdf invoice_id
  endpoint = "https://#{Fakturownia.}.fakturownia.pl/invoices/#{invoice_id}.pdf"
  response = self.make_get_request(endpoint)
   
  (response.code == '200') ? response.body : nil
end

.update(invoice_id, invoice_json) ⇒ Object

Update invoice invoice_id - id of invoice from fakturownia



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fakturownia/api.rb', line 68

def self.update invoice_id, invoice_json
  endpoint = "https://#{Fakturownia.}.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