Ruby SDK Quickstart
Following the steps described on this page, you'll create a simple Ruby application that interacts with the Fatture in Cloud API.
If you want to download the complete working example you can find it here.
0️⃣ Prerequisites
In this guide, we assume that these prerequisites are met:
- Ruby installed
- A private app using the OAuth 2.0 Authorization Code Flow
- A Fatture in Cloud account.
1️⃣ Step One: Install the Fatture in Cloud SDK
In this quickstart, we'll use the Fatture in Cloud SDK, published to RubyGems.
To install it:
gem install fattureincloud_ruby_sdk
2️⃣ Step Two: Set up the OAuth access token retrieval
Create the file oauth.rb and copy in the following code:
require 'rubygems'
require 'webrick'
require 'json'
require 'fattureincloud_ruby_sdk'
class Oauth < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
oauth = FattureInCloud_Ruby_Sdk::OAuth2AuthorizationCodeManager.new('CLIENT_ID', 'CLIENT_SECRET', 'http://localhost:8000/oauth')
if !request.request_uri.query.nil?
url_obj = URI.decode_www_form(request.request_uri.query).to_h
if !url_obj['code'].nil?
token = oauth.fetch_token(url_obj['code'])
File.open('./token.json', 'w') do |file|
file.write({"access_token" => token.access_token}.to_json) # saving the oAuth access token in the token.json file
end
body = 'Token saved succesfully in ./token.json'
else
redirect(response, oauth)
end
else redirect(response, oauth)
end
response.status = 200
response['Content-Type'] = 'text/html'
response.body = body
end
def redirect(response, oauth)
url = oauth.get_authorization_url([FattureInCloud_Ruby_Sdk::Scope::ENTITY_SUPPLIERS_READ], 'EXAMPLE_STATE')
response.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect, url)
end
end
if $PROGRAM_NAME == __FILE__
server = WEBrick::HTTPServer.new(Port: 8000)
server.mount '/oauth', Oauth
trap 'INT' do server.shutdown end
server.start
end
To make this code work properly the only thing you need to set your client id, client secret and eventually the redirect uri at line 8.
In this QuickStart the access token and refresh token are stored in a file, this is only for educational purposes, the OAuth access token and refresh token are sensitive data and should be saved securely on your database. Also, never share your Client Secret with third-party actors, or publish it to your frontend!
3️⃣ Step Three: Set up the sample
In this example, we'll show how to retrieve your Company ID using the Ruby SDK. If you plan to manage only one company, you can insert it directly into your code as a variable. Check the Company-scoped Methods page for more info.
Create the files index.rb and quickstart.rb in your working directory and copy in the following code:
- index.rb
- quickstart.rb
require 'rubygems'
require 'webrick'
require './oauth' # importing the class created in the next tab
require './quickstart' # importing the class created in the previous steps
if $0 == __FILE__ then
server = WEBrick::HTTPServer.new(:Port => 8000)
server.mount "/quickstart", QuickStart # route that refers to the QuickStary class in the imported quickstart.rb file
server.mount "/oauth", Oauth # route that refers to the Oauth class in the imported oauth.rb file
trap "INT" do server.shutdown end
server.start
end
require 'fattureincloud_ruby_sdk'
class QuickStart < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
# setup authorization
FattureInCloud_Ruby_Sdk.configure do |config|
# Configure OAuth2 access token for authorization: OAuth2AuthenticationCodeFlow
config.access_token = retrieve_token_from_file()
end
# Retrieve the first company id
user_api_instance = FattureInCloud_Ruby_Sdk::UserApi.new
user_companies_response = user_api_instance.list_user_companies
first_company_id = user_companies_response.data.companies[0].id
# Retrieve the list of the Suppliers
suppliers_api_instance = FattureInCloud_Ruby_Sdk::SuppliersApi.new
company_suppliers = suppliers_api_instance.list_suppliers(first_company_id)
response.body = company_suppliers.to_s
end
def retrieve_token_from_file()
obj = JSON.parse(File.read("./token.json"))
return obj["access_token"].to_s
end
end
Make sure your FattureInCloud app redirect URL points to the just defined /oauth endpoint (e.g. http://localhost:8000/oauth).
4️⃣ Step Four: Run the sample
From the command line, run the following command:
ruby index.rb
Now visit http://localhost:8000/oauth (or whatever your URL is), you will be redirected to the Fatture in Cloud login page where you will be asked to grant some permissions, according to what scopes you specified previously. Finally, you will see the success message and the access token will be stored in the token.json file.
You can now visit http://localhost:8000/quickstart (or whatever your URL is) to test the application.
❓ What now?
In this example, we used a limited set of the available API methods to show how to use our SDK.
If you want to access the full documentation of the available methods and models, you can check the following resources:
- Ruby SDK GitHub Repository: the Readme file contains the full list of the available methods and models
- API Reference: it contains the list of methods and models
- OpenAPI Specification: Our OpenAPI Specification contains the full description of the available methods and models
- RubyDoc page: The package documentation
- RubyGems page: The main package page on RubyGems