Ruby
Ruby scripts and applications can merge templates and manage the template storage through the gem txtextcontrol-reportingcloud. It provides the class TXTextControl::ReportingCloud::ReportingCloud, whose methods send the requests of the Web API with Ruby's standard library and return the parsed results. A script that merges the demo template shows the gem at work, and a table maps each of its methods to its endpoint.
The gem has had no release since March 2019. It authenticates with the user name and password of the account only, returns documents as Base64 strings and has no methods for the thumbnails of a document, sharing or tracked changes. For these, call the Web API directly, as Web API in Getting Started shows with curl.
Ruby in Getting Started builds a first script with the gem step by step.
Gem and Requirements
| Gem | Version | Released | Ruby | Runtime dependencies |
|---|---|---|---|---|
| txtextcontrol-reportingcloud | 1.2.1 | March 4, 2019 | No minimum version declared | None; the gem uses net/http, json, uri, cgi, ostruct and date of the standard library |
Add the gem to the Gemfile of your application and run bundle, or install it with RubyGems:
gem install txtextcontrol-reportingcloud
Installing an SDK has the details and a first call that checks the installation.
Authentication
The gem sends every request with Basic authentication. It has no option for an API key, so it needs the user name and password of your ReportingCloud account. The constructor is ReportingCloud.new(username, password, base_url = nil); without base_url, the requests go to https://api.reporting.cloud. The instance has the attributes username, password, base_uri, api_version, which is "v1", and read_timeout, the timeout of a request in seconds, which is 10. Authentication explains Basic authentication and why API keys are recommended for the Web API.
Merge a Template
The following script merges sample_invoice.tx, the demo template in the template storage of every trial account, with one invoice record and saves invoice.pdf. It reads the credentials from the environment variables REPORTINGCLOUD_USERNAME and REPORTINGCLOUD_PASSWORD:
require "base64"
require "txtextcontrol/reportingcloud"
require "txtextcontrol/reportingcloud/merge_body"
require "txtextcontrol/reportingcloud/merge_settings"
# The Ruby SDK authenticates with the user name and password of the account
reporting_cloud = TXTextControl::ReportingCloud::ReportingCloud.new(
ENV["REPORTINGCLOUD_USERNAME"], ENV["REPORTINGCLOUD_PASSWORD"])
# One record with the fields and the item block of sample_invoice.tx
merge_data = [
{
"invoice_no" => "778723",
"billto_name" => "Joey Montana",
"billto_companyname" => "Montana, LLC",
"item" => [
{ "qty" => "1", "item_no" => "1", "item_description" => "Item description 1", "item_total" => "2130.40" },
{ "qty" => "1", "item_no" => "2", "item_description" => "Item description 2", "item_total" => "5543" }
],
"total" => "8210.538"
}
]
merge_settings = TXTextControl::ReportingCloud::MergeSettings.new
merge_settings.document_title = "Invoice 778723"
merge_body = TXTextControl::ReportingCloud::MergeBody.new(merge_data, merge_settings)
begin
# Merge the stored template; the documents come back as Base64 strings
documents = reporting_cloud.merge_document(merge_body, "sample_invoice.tx", :pdf)
File.binwrite("invoice.pdf", Base64.strict_decode64(documents[0]))
puts "invoice.pdf has been written."
rescue RuntimeError => error
# The message is the response body of the failed request
puts error.message
end
The code works as follows:
require "txtextcontrol/reportingcloud"loads the main class.MergeBodyandMergeSettingsare in files of their own and are required separately.MergeBody.new(merge_data, merge_settings = nil, template = nil)takes the records as a non-empty array of hashes, whose keys are the merge field names; a nested array such asitemfills a merge block.template, a Base64-encoded template, replaces the template name. Merge Data describes the structure.merge_document(merge_body, template_name = nil, return_format = :pdf, append = false, test = false)sends the request to POST /v1/document/merge. Passing a template name together with template data, or neither, raises anArgumentError.- The method returns an array of Base64 strings, one for each created document, which
Base64.strict_decode64turns into the binary file.
A new MergeSettings has remove_empty_fields, remove_empty_blocks, remove_empty_images and remove_trailing_whitespace set to true and merge_html set to false, and the gem sends all five. remove_empty_images is therefore true, while the Web API's default is false. There is no attribute for the culture. The other attributes are those of DocumentSettings: author, creator_application, document_subject, document_title, user_password, creation_date and last_modification_date. Merge Settings describes every setting.
ReportingCloud Methods
Return formats and image formats are symbols, such as :pdf, :docx or :png. Documents and templates go in and come back as Base64 strings.
| Method | Web API endpoint | Returns |
|---|---|---|
merge_document | POST /v1/document/merge | An array of Base64 strings |
append_documents | POST /v1/document/append | A Base64 string, one document from an AppendBody |
convert_document | POST /v1/document/convert | A Base64 string |
find_and_replace | POST /v1/document/findandreplace | A Base64 string |
upload_template | POST /v1/templates/upload | Nothing |
list_templates | GET /v1/templates/list | An array of Template objects |
get_template_count | GET /v1/templates/count | An integer |
template_exists? | GET /v1/templates/exists | true or false |
get_template_info | GET /v1/templates/info | A TemplateInfo object |
get_template_page_count | GET /v1/templates/pagecount | An integer |
get_template_thumbnails | GET /v1/templates/thumbnails | An array of Base64 images, one per page |
download_template | GET /v1/templates/download | A Base64 string |
delete_template | DELETE /v1/templates/delete | Nothing |
get_account_settings | GET /v1/account/settings | An AccountSettings object |
get_api_keys, create_api_key, delete_api_key | GET /v1/account/apikeys, PUT and DELETE /v1/account/apikey | An array of APIKey objects, the new key or nothing |
list_fonts | GET /v1/fonts/list | An array of font names |
check_text | GET /v1/proofing/check | An array of IncorrectWord objects |
get_available_dictionaries | GET /v1/proofing/availabledictionaries | An array of dictionary names |
get_suggestions | GET /v1/proofing/suggestions | An array of suggestions |
append_documents, convert_document and find_and_replace take a test argument as well. The API key methods manage the keys of the account; the gem itself still signs in with the user name and password.
Errors
When the Web API answers with an error, a method raises a RuntimeError whose message is the response body; the HTTP status code isn't part of it. Arguments the gem rejects before sending, such as merge data that isn't an array of hashes or an empty text for check_text, raise an ArgumentError. Errors and Status Codes lists the status codes and their causes.
API Documentation and Source
- API documentation. The YARD documentation describes every class, attribute and method of the gem. The README in the repository shows how to build it with
yard doc. - Source code. The repository TextControl/txtextcontrol-reportingcloud-ruby holds the source under a BSD license, with RSpec tests. Its code was last changed on March 4, 2019, for version 1.2.1; a change to the README followed in December 2020.