Module: Fattura24::Api

Defined in:
lib/fattura24/api/client.rb,
lib/fattura24/api/response.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

API_HOST =

This library uses fattura24.com v0.3 apis. Check their docs here.

'https://www.app.fattura24.com/api/v0.3'

Class Method Summary collapse

Class Method Details

.get_file(id) ⇒ Object

Donwloads a pdf file for an existing document. Requires an existing document id, throws MissingInput when passing a nil id. Returns a Response object: refer to it’s documentation to detect a binary file and instruction to save it to disk.



71
72
73
74
75
# File 'lib/fattura24/api/client.rb', line 71

def self.get_file(id)
  raise(Fattura24::MissingInput, 'You need to provide an id') unless id

  request('/GetFile', { docId: id })
end

.get_numeratorObject

Gets numerator list. Returns a Response object.



60
61
62
# File 'lib/fattura24/api/client.rb', line 60

def self.get_numerator
  request('/GetNumerator')
end

.get_pdcObject

Gets ‘piano dei conti’ Returns a Response object.



53
54
55
# File 'lib/fattura24/api/client.rb', line 53

def self.get_pdc
  request('/GetPdc')
end

.get_product(options = {}) ⇒ Object

Gets a list of products. You can pass a Hash containing a code or category to filter your existing products by them. Throws a InvalidParams when passing an hash containing unrecognized options. Returns a Response object.



85
86
87
88
# File 'lib/fattura24/api/client.rb', line 85

def self.get_product(options = {})
  validate_params(options, %i[code category])
  request('/GetProduct', options)
end

.get_templateObject

Gets a list of document templates. Returns a Response object.



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

def self.get_template
  request('/GetTemplate')
end

.request(path, body = {}) ⇒ Object

Performs a generic request on the api endpoint using Ruby’s Net::HTTP. All the other api methods call this one. Parameter path should always be prepended with ‘/’. Body will default to an empty hash. Returns a Response object.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fattura24/api/client.rb', line 19

def self.request(path, body = {})
  raise Fattura24::MissingApiKey unless Fattura24.configuration.api_key

  uri = URI.parse("#{API_HOST}#{path}")
  request = Net::HTTP::Post.new(uri)
  request.set_form_data(inject_api_key(body))

  req_options = { use_ssl: uri.scheme == 'https' }
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  Response.new(response)
end

.save_customer(data = {}) ⇒ Object

Saves a customer in your contact list. Any nil parameter will be deeply removed by using the crush utility. Returns a Response object.



97
98
99
100
101
# File 'lib/fattura24/api/client.rb', line 97

def self.save_customer(data = {})
  request('/SaveCustomer', {
    xml: hash_to_xml(data)
  })
end

.save_document(data = {}) ⇒ Object

Use this to create documents. Pass a hash with the data, check the README file for examples. Use DocumentType enums to specify document type. Any nil parameter will be deeply removed by using the crush utility. Returns a Response object.



111
112
113
114
115
# File 'lib/fattura24/api/client.rb', line 111

def self.save_document(data = {})
  request('/SaveDocument', {
    xml: hash_to_xml(data)
  })
end

.save_item(data = {}) ⇒ Object

Creates a credit. Any nil parameter will be deeply removed by using the crush utility. Returns a Response object.



122
123
124
125
126
# File 'lib/fattura24/api/client.rb', line 122

def self.save_item(data = {})
  request('/SaveItem', {
    xml: hash_to_xml(data)
  })
end

.test_keyObject

Tests validity of your api key. Returns a Response object.



37
38
39
# File 'lib/fattura24/api/client.rb', line 37

def self.test_key
  request('/TestKey')
end