module Fattura24::Api
Constants
- API_HOST
This library uses fattura24.com v0.3 apis. Check their docs here.
Public Class Methods
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.
# 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
Gets numerator list. Returns a Response object.
# File lib/fattura24/api/client.rb, line 60 def self.get_numerator request('/GetNumerator') end
Gets 'piano dei conti' Returns a Response object.
# File lib/fattura24/api/client.rb, line 53 def self.get_pdc request('/GetPdc') end
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.
# File lib/fattura24/api/client.rb, line 85 def self.get_product(options = {}) validate_params(options, %i[code category]) request('/GetProduct', options) end
Gets a list of document templates. Returns a Response object.
# File lib/fattura24/api/client.rb, line 46 def self.get_template request('/GetTemplate') end
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.
# 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
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.
# File lib/fattura24/api/client.rb, line 111 def self.save_document(data = {}) request('/SaveDocument', { xml: hash_to_xml(data) }) end
Tests validity of your api key. Returns a Response object.
# File lib/fattura24/api/client.rb, line 37 def self.test_key request('/TestKey') end
Private Class Methods
# File lib/fattura24/api/client.rb, line 128 def self.hash_to_xml(hash) { document: Fattura24::Utils.crush(hash) || '' } .to_xml( root: 'Fattura24', indent: 0, skip_types: true, camelize: true ) end
# File lib/fattura24/api/client.rb, line 138 def self.inject_api_key(hash) hash.merge(apiKey: Fattura24.configuration.api_key) end
# File lib/fattura24/api/client.rb, line 148 def self.raise_params_error(invalid_params) msg = "This params are not permitted: #{invalid_params.join(', ')}" raise Fattura24::InvalidParams, msg end
# File lib/fattura24/api/client.rb, line 142 def self.validate_params(params, permitted_params) invalid_params = params.keys.map(&:to_sym) - permitted_params raise_params_error(invalid_params) if invalid_params.any? end