Class: FulfilApi::Report

Inherits:
Object
  • Object
show all
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.

Examples:

Generate and download an invoice PDF

report = FulfilApi::Report.generate("account.invoice.html", 3991)
report.url       # => "https://..."
report.filename  # => "Invoice.pdf"
report.mimetype  # => "application/pdf"

tempfile = report.download
tempfile.path    # => "/tmp/fulfil_report20260324-12345.pdf"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename:, mimetype:, url:) ⇒ Report

Returns a new instance of Report.

Parameters:

  • filename (String)

    The filename of the generated report.

  • mimetype (String)

    The MIME type of the generated report.

  • url (String)

    The temporary URL to download the generated 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

#filenameObject (readonly)

Returns the value of attribute filename.



17
18
19
# File 'lib/fulfil_api/report.rb', line 17

def filename
  @filename
end

#mimetypeObject (readonly)

Returns the value of attribute mimetype.



17
18
19
# File 'lib/fulfil_api/report.rb', line 17

def mimetype
  @mimetype
end

#urlObject (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.

Parameters:

  • report_name (String)

    The report identifier (e.g., “account.invoice.html”).

  • id (Integer)

    The record ID to generate the report for.

  • data (Hash) (defaults to: {})

    Optional additional data for the report.

Returns:



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

#downloadTempfile

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.

Returns:

  • (Tempfile)

    A tempfile containing the downloaded report.



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