fulfil_api 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/fulfil_api/report.rb +61 -0
- data/lib/fulfil_api/test_helper.rb +36 -0
- data/lib/fulfil_api/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3cb2c76cac5e785167cc4edf3e1d2f92ab3ec3e330edd5e9a34ef50cf8d09705
|
|
4
|
+
data.tar.gz: ad349377c00dd1906553266232d0ce2bc4fd8f2aff7c96683c81dc1a9a3834de
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 16aea8d65692250edd1b51ce9089dec1b0d39ae850e9860d847f9e673a39b10b32e0b4a1c81ae29c8bcf7ece11f5fd10b0672896ebb0a89d815fb3ef3169aa6a
|
|
7
|
+
data.tar.gz: a825f694c375ed30e4fc76a9aef39a8adfc0298f0ad9fe99b749ba09e6dd54928d71de7a08e7c436a85f3855b8d88e637cc7e99d78f80842429e0c0d440d9cb4
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FulfilApi
|
|
4
|
+
# The {FulfilApi::Report} class provides an interface for generating reports
|
|
5
|
+
# (e.g., PDF invoices) via Fulfil's report API. The API returns a temporary
|
|
6
|
+
# URL where the generated document can be downloaded.
|
|
7
|
+
#
|
|
8
|
+
# @example Generate and download an invoice PDF
|
|
9
|
+
# report = FulfilApi::Report.generate("account.invoice.html", 3991)
|
|
10
|
+
# report.url # => "https://..."
|
|
11
|
+
# report.filename # => "Invoice.pdf"
|
|
12
|
+
# report.mimetype # => "application/pdf"
|
|
13
|
+
#
|
|
14
|
+
# tempfile = report.download
|
|
15
|
+
# tempfile.path # => "/tmp/fulfil_report20260324-12345.pdf"
|
|
16
|
+
class Report
|
|
17
|
+
attr_reader :filename, :mimetype, :url
|
|
18
|
+
|
|
19
|
+
# Generates a report for the given record ID.
|
|
20
|
+
#
|
|
21
|
+
# @param report_name [String] The report identifier (e.g., "account.invoice.html").
|
|
22
|
+
# @param id [Integer] The record ID to generate the report for.
|
|
23
|
+
# @param data [Hash] Optional additional data for the report.
|
|
24
|
+
# @return [FulfilApi::Report] A report object with filename, mimetype, and url.
|
|
25
|
+
def self.generate(report_name, id, data: {})
|
|
26
|
+
response = FulfilApi.client.put("report/#{report_name}", body: { objects: [id], data: data })
|
|
27
|
+
|
|
28
|
+
new(
|
|
29
|
+
filename: response["filename"],
|
|
30
|
+
mimetype: response["mimetype"],
|
|
31
|
+
url: response["url"]
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @param filename [String] The filename of the generated report.
|
|
36
|
+
# @param mimetype [String] The MIME type of the generated report.
|
|
37
|
+
# @param url [String] The temporary URL to download the generated report.
|
|
38
|
+
def initialize(filename:, mimetype:, url:)
|
|
39
|
+
@filename = filename
|
|
40
|
+
@mimetype = mimetype
|
|
41
|
+
@url = url
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Downloads the report from the temporary URL and returns a {Tempfile}.
|
|
45
|
+
#
|
|
46
|
+
# The tempfile preserves the file extension from the report's filename
|
|
47
|
+
# (e.g., ".pdf") so it can be used directly with file upload APIs.
|
|
48
|
+
#
|
|
49
|
+
# @return [Tempfile] A tempfile containing the downloaded report.
|
|
50
|
+
def download
|
|
51
|
+
response = Faraday.get(url)
|
|
52
|
+
|
|
53
|
+
extension = File.extname(filename)
|
|
54
|
+
tempfile = Tempfile.new(["fulfil_report", extension])
|
|
55
|
+
tempfile.binmode
|
|
56
|
+
tempfile.write(response.body)
|
|
57
|
+
tempfile.rewind
|
|
58
|
+
tempfile
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -65,6 +65,26 @@ module FulfilApi
|
|
|
65
65
|
.and_return(status: status, body: response.to_json, headers: { "Content-Type": "application/json" })
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
+
# Stubs an HTTP PUT request to the Fulfil report API based on the provided parameters.
|
|
69
|
+
#
|
|
70
|
+
# @param [Hash] response The response body to return as a JSON object (default is {}).
|
|
71
|
+
# @param [Integer] status The HTTP status code to return (default is 200).
|
|
72
|
+
# @param [Hash] options Additional options for the request URL.
|
|
73
|
+
# @option options [String] :report_name The report identifier
|
|
74
|
+
# (e.g., 'account.invoice.html').
|
|
75
|
+
#
|
|
76
|
+
# @return [WebMock::RequestStub] The WebMock request stub object.
|
|
77
|
+
#
|
|
78
|
+
# @example Stub a report request for a specific report
|
|
79
|
+
# stub_fulfil_report_request(report_name: "account.invoice.html", response: { url: "https://..." })
|
|
80
|
+
#
|
|
81
|
+
# @example Stub all report requests
|
|
82
|
+
# stub_fulfil_report_request(response: { url: "https://..." })
|
|
83
|
+
def stub_fulfil_report_request(response: {}, status: 200, **options)
|
|
84
|
+
stubbed_report_request_for(**options)
|
|
85
|
+
.and_return(status: status, body: response.to_json, headers: { "Content-Type": "application/json" })
|
|
86
|
+
end
|
|
87
|
+
|
|
68
88
|
private
|
|
69
89
|
|
|
70
90
|
# Builds the WebMock request stub for the Fulfil 3PL API based on the provided method and options.
|
|
@@ -84,6 +104,22 @@ module FulfilApi
|
|
|
84
104
|
end
|
|
85
105
|
end
|
|
86
106
|
|
|
107
|
+
# Builds the WebMock request stub for the Fulfil report API based on the provided options.
|
|
108
|
+
#
|
|
109
|
+
# @param [Hash] options Additional options for the request URL.
|
|
110
|
+
# @option options [String] :report_name The report identifier
|
|
111
|
+
# (e.g., 'account.invoice.html').
|
|
112
|
+
#
|
|
113
|
+
# @return [WebMock::RequestStub] The WebMock request stub object.
|
|
114
|
+
def stubbed_report_request_for(**options)
|
|
115
|
+
case options.transform_keys(&:to_sym)
|
|
116
|
+
in { report_name: }
|
|
117
|
+
stub_request(:put, %r{fulfil.io/api/v\d+/report/#{report_name}(.*)}i)
|
|
118
|
+
else
|
|
119
|
+
stub_request(:put, %r{fulfil.io/api/v\d+/report}i)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
87
123
|
# Builds the WebMock request stub for the Fulfil API based on the provided method and options.
|
|
88
124
|
#
|
|
89
125
|
# @param [String, Symbol] method The HTTP method to be stubbed (e.g., :get, :post).
|
data/lib/fulfil_api/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fulfil_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Stefan Vermaas
|
|
@@ -92,6 +92,7 @@ files:
|
|
|
92
92
|
- lib/fulfil_api/relation/loadable.rb
|
|
93
93
|
- lib/fulfil_api/relation/naming.rb
|
|
94
94
|
- lib/fulfil_api/relation/query_methods.rb
|
|
95
|
+
- lib/fulfil_api/report.rb
|
|
95
96
|
- lib/fulfil_api/resource.rb
|
|
96
97
|
- lib/fulfil_api/resource/attribute_assignable.rb
|
|
97
98
|
- lib/fulfil_api/resource/attribute_type.rb
|