Class: FulfilApi::Report
- Inherits:
-
Object
- Object
- FulfilApi::Report
- Defined in:
- lib/fulfil_api/report.rb
Overview
The Report class provides an interface for generating reports (e.g., PDF invoices) via Fulfil’s report API. The API returns a temporary URL where the generated document can be downloaded.
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#mimetype ⇒ Object
readonly
Returns the value of attribute mimetype.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.generate(report_name, id, data: {}) ⇒ FulfilApi::Report
Generates a report for the given record ID.
Instance Method Summary collapse
-
#download ⇒ Tempfile
Downloads the report from the temporary URL and returns a Tempfile.
-
#initialize(filename:, mimetype:, url:) ⇒ Report
constructor
A new instance of Report.
Constructor Details
#initialize(filename:, mimetype:, url:) ⇒ Report
Returns a new instance of Report.
38 39 40 41 42 |
# File 'lib/fulfil_api/report.rb', line 38 def initialize(filename:, mimetype:, url:) @filename = filename @mimetype = mimetype @url = url end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
17 18 19 |
# File 'lib/fulfil_api/report.rb', line 17 def filename @filename end |
#mimetype ⇒ Object (readonly)
Returns the value of attribute mimetype.
17 18 19 |
# File 'lib/fulfil_api/report.rb', line 17 def mimetype @mimetype end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
17 18 19 |
# File 'lib/fulfil_api/report.rb', line 17 def url @url end |
Class Method Details
.generate(report_name, id, data: {}) ⇒ FulfilApi::Report
Generates a report for the given record ID.
25 26 27 28 29 30 31 32 33 |
# File 'lib/fulfil_api/report.rb', line 25 def self.generate(report_name, id, data: {}) response = FulfilApi.client.put("report/#{report_name}", body: { objects: [id], data: data }) new( filename: response["filename"], mimetype: response["mimetype"], url: response["url"] ) end |
Instance Method Details
#download ⇒ Tempfile
Downloads the report from the temporary URL and returns a Tempfile.
The tempfile preserves the file extension from the report’s filename (e.g., “.pdf”) so it can be used directly with file upload APIs.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fulfil_api/report.rb', line 50 def download response = Faraday.get(url) extension = File.extname(filename) tempfile = Tempfile.new(["fulfil_report", extension]) tempfile.binmode tempfile.write(response.body) tempfile.rewind tempfile end |