screenshotscout 0.7.0 → 0.9.0
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/README.md +37 -15
- data/examples/README.md +5 -1
- data/examples/html.rb +35 -0
- data/lib/screenshotscout/capture_values.rb +1 -0
- data/lib/screenshotscout/client.rb +25 -114
- data/lib/screenshotscout/internal/response_decoder.rb +141 -0
- data/lib/screenshotscout/internal/serializer.rb +20 -19
- data/lib/screenshotscout/responses.rb +4 -3
- data/lib/screenshotscout/version.rb +1 -1
- data/lib/screenshotscout.rb +1 -0
- data/sig/screenshotscout.rbs +6 -1
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0a6c2a20b607c8c38e5387794c76f65f44e8ce0fc133c91e35b2a8f818091845
|
|
4
|
+
data.tar.gz: fcf7bc56425c7210b2111ef0a862ba793c8eef1853e6954b2aec71e11f9c3f15
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2de0bcd80da4f6c94b6d5405ca08d2cc80e513a8c475cb4c0b184c39897b6cb4fbd833038f3629ecf2c2bb0fbaecad11059a9a7f1a4e0d029ab0ab475c91fbaa
|
|
7
|
+
data.tar.gz: 384a51e982b8a9e5400b0204e0fefde8a67f4bb70bbec0c0d27ff0481473dab08fe45828f56533349ee31ab526428e421db5d09787811b47cdecddd2eaae3752
|
data/README.md
CHANGED
|
@@ -51,43 +51,65 @@ puts response.screenshot_url
|
|
|
51
51
|
|
|
52
52
|
`capture` uses POST by default, or GET when the client has a secret key. It returns a
|
|
53
53
|
`BinaryCaptureResponse` when `response_type` is omitted or set to `CaptureResponseType::BINARY`.
|
|
54
|
+
With `CaptureFormat::MD` or `CaptureFormat::HTML`, the text is also in `markdown` or `html`.
|
|
54
55
|
|
|
55
|
-
##
|
|
56
|
+
## Request a JSON result
|
|
56
57
|
|
|
57
|
-
Set `
|
|
58
|
+
Set `response_type` to `CaptureResponseType::JSON` to receive capture metadata instead of binary
|
|
59
|
+
image, PDF, Markdown, or HTML output:
|
|
58
60
|
|
|
59
61
|
```ruby
|
|
60
62
|
require "screenshotscout"
|
|
61
63
|
|
|
62
64
|
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
ScreenshotScout::CaptureOptions.new(format: ScreenshotScout::CaptureFormat::MD)
|
|
65
|
+
options = ScreenshotScout::CaptureOptions.new(
|
|
66
|
+
response_type: ScreenshotScout::CaptureResponseType::JSON
|
|
66
67
|
)
|
|
68
|
+
response = client.capture("https://example.com", options)
|
|
67
69
|
|
|
68
|
-
|
|
70
|
+
puts response.result.screenshot_url
|
|
69
71
|
```
|
|
70
72
|
|
|
71
|
-
|
|
73
|
+
Unrecognized JSON result fields are retained in `response.result.additional_fields`.
|
|
72
74
|
|
|
73
|
-
##
|
|
75
|
+
## Capture from HTML
|
|
74
76
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
To render HTML you generated, such as an invoice or a report, call `capture_from_html` with the
|
|
78
|
+
document instead of a URL. It takes the same options as `capture`:
|
|
77
79
|
|
|
78
80
|
```ruby
|
|
79
81
|
require "screenshotscout"
|
|
80
82
|
|
|
81
83
|
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
82
|
-
|
|
83
|
-
|
|
84
|
+
|
|
85
|
+
html = <<~HTML
|
|
86
|
+
<!doctype html>
|
|
87
|
+
<html>
|
|
88
|
+
<head>
|
|
89
|
+
<title>Invoice #1042</title>
|
|
90
|
+
</head>
|
|
91
|
+
<body>
|
|
92
|
+
<h1>Invoice #1042</h1>
|
|
93
|
+
<p>Total due: $480.00</p>
|
|
94
|
+
</body>
|
|
95
|
+
</html>
|
|
96
|
+
HTML
|
|
97
|
+
|
|
98
|
+
response = client.capture_from_html(
|
|
99
|
+
html,
|
|
100
|
+
ScreenshotScout::CaptureOptions.new(format: ScreenshotScout::CaptureFormat::PDF)
|
|
84
101
|
)
|
|
85
|
-
response = client.capture("https://example.com", options)
|
|
86
102
|
|
|
87
|
-
|
|
103
|
+
File.binwrite("invoice.pdf", response.bytes)
|
|
88
104
|
```
|
|
89
105
|
|
|
90
|
-
|
|
106
|
+
Send a complete document that starts with `<!doctype html>`. A PDF takes its title from `<title>`.
|
|
107
|
+
The page has no URL, so use absolute URLs for images, stylesheets and fonts, or add a
|
|
108
|
+
`<base href>`. See [How to Render HTML](https://screenshotscout.com/docs/guides/render-html) for
|
|
109
|
+
more.
|
|
110
|
+
|
|
111
|
+
The HTML is sent in a POST request, which can't be signed. So `capture_from_html` needs a client
|
|
112
|
+
without a secret key, and an API key that doesn't require signed requests.
|
|
91
113
|
|
|
92
114
|
## Use GET
|
|
93
115
|
|
data/examples/README.md
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
- [`json.rb`](json.rb) requests a JSON response and prints the screenshot URL.
|
|
6
6
|
- [`capture_url.rb`](capture_url.rb) creates and prints a capture URL without sending an API
|
|
7
7
|
request.
|
|
8
|
+
- [`html.rb`](html.rb) renders an invoice to PDF, saves it as `invoice.pdf`, and prints the
|
|
9
|
+
returned screenshot URL.
|
|
8
10
|
|
|
9
11
|
## Run the examples
|
|
10
12
|
|
|
@@ -15,7 +17,8 @@ bundle install
|
|
|
15
17
|
```
|
|
16
18
|
|
|
17
19
|
Set your access key. If you also set the optional secret key, the client signs its requests and the
|
|
18
|
-
URL that `capture_url.rb` prints.
|
|
20
|
+
URL that `capture_url.rb` prints. `html.rb` sends the HTML in a POST request, which can't be
|
|
21
|
+
signed, so run it without `SCREENSHOTSCOUT_SECRET_KEY`.
|
|
19
22
|
|
|
20
23
|
PowerShell:
|
|
21
24
|
|
|
@@ -37,6 +40,7 @@ Run any example:
|
|
|
37
40
|
bundle exec ruby -Ilib examples/binary.rb
|
|
38
41
|
bundle exec ruby -Ilib examples/json.rb
|
|
39
42
|
bundle exec ruby -Ilib examples/capture_url.rb
|
|
43
|
+
bundle exec ruby -Ilib examples/html.rb
|
|
40
44
|
```
|
|
41
45
|
|
|
42
46
|
Generated capture URLs contain the access key and should be treated as sensitive.
|
data/examples/html.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "screenshotscout"
|
|
4
|
+
|
|
5
|
+
html = <<~HTML
|
|
6
|
+
<!doctype html>
|
|
7
|
+
<html lang="en">
|
|
8
|
+
<head>
|
|
9
|
+
<meta charset="utf-8" />
|
|
10
|
+
<title>Invoice #1042</title>
|
|
11
|
+
<style>
|
|
12
|
+
body { font-family: system-ui, sans-serif; margin: 48px; color: #111; }
|
|
13
|
+
table { width: 100%; border-collapse: collapse; margin-top: 24px; }
|
|
14
|
+
th, td { text-align: left; padding: 8px 0; border-bottom: 1px solid #ddd; }
|
|
15
|
+
.total td { font-weight: 600; }
|
|
16
|
+
</style>
|
|
17
|
+
</head>
|
|
18
|
+
<body>
|
|
19
|
+
<h1>Invoice #1042</h1>
|
|
20
|
+
<p>Billed to Example Corp.</p>
|
|
21
|
+
<table>
|
|
22
|
+
<tr><th>Item</th><th>Amount</th></tr>
|
|
23
|
+
<tr><td>Consulting, 4 hours</td><td>$480.00</td></tr>
|
|
24
|
+
<tr class="total"><td>Total</td><td>$480.00</td></tr>
|
|
25
|
+
</table>
|
|
26
|
+
</body>
|
|
27
|
+
</html>
|
|
28
|
+
HTML
|
|
29
|
+
|
|
30
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
31
|
+
options = ScreenshotScout::CaptureOptions.new(format: ScreenshotScout::CaptureFormat::PDF)
|
|
32
|
+
response = client.capture_from_html(html, options)
|
|
33
|
+
|
|
34
|
+
File.binwrite("invoice.pdf", response.bytes)
|
|
35
|
+
puts response.screenshot_url
|
|
@@ -35,14 +35,17 @@ module ScreenshotScout
|
|
|
35
35
|
validate_method(method) unless method.nil?
|
|
36
36
|
reject_signed_post(method)
|
|
37
37
|
method ||= @secret_key.nil? ? CaptureHttpMethod::POST : CaptureHttpMethod::GET
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
request = create_request(serialized, method, signature)
|
|
41
|
-
raw_response = execute_request(request)
|
|
42
|
-
|
|
43
|
-
raise create_api_error(raw_response) unless raw_response.status.between?(200, 299)
|
|
38
|
+
send_capture(serialized, method, options)
|
|
39
|
+
end
|
|
44
40
|
|
|
45
|
-
|
|
41
|
+
# Captures an HTML document instead of a URL, such as an invoice or a report you generated. Sends the HTML
|
|
42
|
+
# exactly as given, in one POST request, and waits for the final inline response. A binary string, such as the
|
|
43
|
+
# result of File.binread or an HTTP response body, is read as UTF-8. A client with a secret key raises
|
|
44
|
+
# ConfigurationError, because POST requests can't be signed.
|
|
45
|
+
def capture_from_html(html, options = nil)
|
|
46
|
+
serialized = Internal::Serializer.serialize(html, options, target: :html)
|
|
47
|
+
reject_signed_html
|
|
48
|
+
send_capture(serialized, CaptureHttpMethod::POST, options)
|
|
46
49
|
end
|
|
47
50
|
|
|
48
51
|
# Builds a sensitive GET capture URL without performing network I/O.
|
|
@@ -101,6 +104,21 @@ module ScreenshotScout
|
|
|
101
104
|
"method GET, or use a client without a secret key and an API key that doesn't require signed requests."
|
|
102
105
|
end
|
|
103
106
|
|
|
107
|
+
def reject_signed_html
|
|
108
|
+
return if @secret_key.nil?
|
|
109
|
+
|
|
110
|
+
source = @secret_key_from_environment ? " from #{Internal::Credentials::SECRET_KEY_VARIABLE}" : ""
|
|
111
|
+
raise ConfigurationError,
|
|
112
|
+
"`capture_from_html` sends the HTML in a POST body, which can't be signed. This client has a secret " \
|
|
113
|
+
"key#{source}: use a client without a secret key and an API key that doesn't require signed requests."
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def send_capture(serialized, method, options)
|
|
117
|
+
signature = create_signature(serialized.pairs)
|
|
118
|
+
request = create_request(serialized, method, signature)
|
|
119
|
+
Internal::ResponseDecoder.decode(execute_request(request), options)
|
|
120
|
+
end
|
|
121
|
+
|
|
104
122
|
def create_signature(pairs)
|
|
105
123
|
return nil if @secret_key.nil?
|
|
106
124
|
|
|
@@ -148,113 +166,6 @@ module ScreenshotScout
|
|
|
148
166
|
cause: e
|
|
149
167
|
end
|
|
150
168
|
|
|
151
|
-
def expected_response_kind(options)
|
|
152
|
-
response_type = options&.response_type
|
|
153
|
-
return :binary if response_type.nil? || response_type == CaptureResponseType::BINARY
|
|
154
|
-
return :json if response_type == CaptureResponseType::JSON
|
|
155
|
-
|
|
156
|
-
nil
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
def decode_success_response(raw_response, expected_kind)
|
|
160
|
-
actual_kind = json_media_type?(raw_response.content_type) ? :json : :binary
|
|
161
|
-
if !expected_kind.nil? && actual_kind != expected_kind
|
|
162
|
-
message = "Screenshot Scout returned a successful #{actual_kind} response " \
|
|
163
|
-
"when #{expected_kind} was requested."
|
|
164
|
-
cause = TypeError.new("Expected a #{expected_kind} response but received #{actual_kind}.")
|
|
165
|
-
raise ResponseDecodingError.new(message, raw_response), cause: cause
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
actual_kind == :json ? decode_json_response(raw_response) : decode_binary_response(raw_response)
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
def decode_binary_response(raw_response)
|
|
172
|
-
BinaryCaptureResponse.new(
|
|
173
|
-
raw_response: raw_response,
|
|
174
|
-
screenshot_url: raw_response.header("Screenshot-Scout-Screenshot-URL").first,
|
|
175
|
-
screenshot_url_expires_at: raw_response.header("Screenshot-Scout-Screenshot-URL-Expires-At").first,
|
|
176
|
-
cache_status: raw_response.header("Screenshot-Scout-Cache-Status").first,
|
|
177
|
-
markdown: markdown_media_type?(raw_response.content_type) ? utf8_text(raw_response.body) : nil
|
|
178
|
-
)
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
def decode_json_response(raw_response)
|
|
182
|
-
value = parse_json_success(raw_response)
|
|
183
|
-
unless value.is_a?(Hash)
|
|
184
|
-
cause = TypeError.new("Expected a JSON object.")
|
|
185
|
-
raise ResponseDecodingError.new(
|
|
186
|
-
"Screenshot Scout returned a successful JSON response that was not an object.",
|
|
187
|
-
raw_response
|
|
188
|
-
), cause: cause
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
screenshot_url = read_optional_string(value, "screenshot_url", raw_response)
|
|
192
|
-
expires_at = read_optional_string(value, "screenshot_url_expires_at", raw_response)
|
|
193
|
-
cache_status = read_optional_string(value, "cache_status", raw_response)
|
|
194
|
-
additional_fields = value.except("screenshot_url", "screenshot_url_expires_at", "cache_status")
|
|
195
|
-
result = CaptureResult.new(
|
|
196
|
-
screenshot_url: screenshot_url,
|
|
197
|
-
screenshot_url_expires_at: expires_at,
|
|
198
|
-
cache_status: cache_status,
|
|
199
|
-
additional_fields: additional_fields
|
|
200
|
-
)
|
|
201
|
-
JsonCaptureResponse.new(result: result, raw_response: raw_response)
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
def parse_json_success(raw_response)
|
|
205
|
-
Internal::JsonCodec.parse(raw_response.body)
|
|
206
|
-
rescue JSON::ParserError, EncodingError => e
|
|
207
|
-
raise ResponseDecodingError.new(
|
|
208
|
-
"Screenshot Scout returned a successful JSON response that could not be decoded.",
|
|
209
|
-
raw_response
|
|
210
|
-
), cause: e
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
def read_optional_string(object, key, raw_response)
|
|
214
|
-
return nil unless object.key?(key)
|
|
215
|
-
return object[key] if object[key].is_a?(String)
|
|
216
|
-
|
|
217
|
-
cause = TypeError.new("Expected \"#{key}\" to be a string.")
|
|
218
|
-
raise ResponseDecodingError.new(
|
|
219
|
-
"Screenshot Scout returned a non-string \"#{key}\" JSON field.",
|
|
220
|
-
raw_response
|
|
221
|
-
), cause: cause
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
def create_api_error(raw_response)
|
|
225
|
-
available, response_body = try_parse_json(raw_response.body)
|
|
226
|
-
object = response_body if response_body.is_a?(Hash)
|
|
227
|
-
error_code = object["error_code"] if object&.fetch("error_code", nil).is_a?(String)
|
|
228
|
-
error_message = object["error_message"] if object&.fetch("error_message", nil).is_a?(String)
|
|
229
|
-
errors = object["errors"] if object&.fetch("errors", nil).is_a?(Array)
|
|
230
|
-
|
|
231
|
-
APIError.new(
|
|
232
|
-
raw_response: raw_response,
|
|
233
|
-
response_body_available: available,
|
|
234
|
-
response_body: response_body,
|
|
235
|
-
error_code: error_code,
|
|
236
|
-
error_message: error_message,
|
|
237
|
-
errors: errors
|
|
238
|
-
)
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
def try_parse_json(body)
|
|
242
|
-
[true, Internal::JsonCodec.parse(body)]
|
|
243
|
-
rescue JSON::ParserError, EncodingError
|
|
244
|
-
[false, nil]
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
def json_media_type?(content_type)
|
|
248
|
-
return false if content_type.nil?
|
|
249
|
-
|
|
250
|
-
media_type = content_type.partition(";").first.strip.downcase
|
|
251
|
-
media_type == "application/json" || media_type.end_with?("+json")
|
|
252
|
-
end
|
|
253
|
-
|
|
254
|
-
def markdown_media_type?(content_type) = content_type.to_s.partition(";").first.strip.downcase == "text/markdown"
|
|
255
|
-
|
|
256
|
-
def utf8_text(bytes) = bytes.dup.force_encoding(Encoding::UTF_8).scrub
|
|
257
|
-
|
|
258
169
|
private_constant :ACCESS_KEY_PATTERN
|
|
259
170
|
end
|
|
260
171
|
end
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ScreenshotScout
|
|
6
|
+
module Internal
|
|
7
|
+
# Turns the buffered response to a capture request into the capture response its options ask for, or into an
|
|
8
|
+
# APIError for a non-2xx status.
|
|
9
|
+
module ResponseDecoder
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def decode(raw_response, options)
|
|
13
|
+
raise create_api_error(raw_response) unless raw_response.status.between?(200, 299)
|
|
14
|
+
|
|
15
|
+
decode_success_response(raw_response, expected_response_kind(options))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def expected_response_kind(options)
|
|
19
|
+
response_type = options&.response_type
|
|
20
|
+
return :binary if response_type.nil? || response_type == CaptureResponseType::BINARY
|
|
21
|
+
return :json if response_type == CaptureResponseType::JSON
|
|
22
|
+
|
|
23
|
+
nil
|
|
24
|
+
end
|
|
25
|
+
private_class_method :expected_response_kind
|
|
26
|
+
|
|
27
|
+
def decode_success_response(raw_response, expected_kind)
|
|
28
|
+
actual_kind = json_media_type?(raw_response.content_type) ? :json : :binary
|
|
29
|
+
if !expected_kind.nil? && actual_kind != expected_kind
|
|
30
|
+
message = "Screenshot Scout returned a successful #{actual_kind} response " \
|
|
31
|
+
"when #{expected_kind} was requested."
|
|
32
|
+
cause = TypeError.new("Expected a #{expected_kind} response but received #{actual_kind}.")
|
|
33
|
+
raise ResponseDecodingError.new(message, raw_response), cause: cause
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
actual_kind == :json ? decode_json_response(raw_response) : decode_binary_response(raw_response)
|
|
37
|
+
end
|
|
38
|
+
private_class_method :decode_success_response
|
|
39
|
+
|
|
40
|
+
def decode_binary_response(raw_response)
|
|
41
|
+
BinaryCaptureResponse.new(
|
|
42
|
+
raw_response: raw_response,
|
|
43
|
+
screenshot_url: raw_response.header("Screenshot-Scout-Screenshot-URL").first,
|
|
44
|
+
screenshot_url_expires_at: raw_response.header("Screenshot-Scout-Screenshot-URL-Expires-At").first,
|
|
45
|
+
cache_status: raw_response.header("Screenshot-Scout-Cache-Status").first,
|
|
46
|
+
markdown: markdown_media_type?(raw_response.content_type) ? utf8_text(raw_response.body) : nil,
|
|
47
|
+
html: html_media_type?(raw_response.content_type) ? utf8_text(raw_response.body) : nil
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
private_class_method :decode_binary_response
|
|
51
|
+
|
|
52
|
+
def decode_json_response(raw_response)
|
|
53
|
+
value = parse_json_success(raw_response)
|
|
54
|
+
unless value.is_a?(Hash)
|
|
55
|
+
cause = TypeError.new("Expected a JSON object.")
|
|
56
|
+
raise ResponseDecodingError.new(
|
|
57
|
+
"Screenshot Scout returned a successful JSON response that was not an object.",
|
|
58
|
+
raw_response
|
|
59
|
+
), cause: cause
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
screenshot_url = read_optional_string(value, "screenshot_url", raw_response)
|
|
63
|
+
expires_at = read_optional_string(value, "screenshot_url_expires_at", raw_response)
|
|
64
|
+
cache_status = read_optional_string(value, "cache_status", raw_response)
|
|
65
|
+
additional_fields = value.except("screenshot_url", "screenshot_url_expires_at", "cache_status")
|
|
66
|
+
result = CaptureResult.new(
|
|
67
|
+
screenshot_url: screenshot_url,
|
|
68
|
+
screenshot_url_expires_at: expires_at,
|
|
69
|
+
cache_status: cache_status,
|
|
70
|
+
additional_fields: additional_fields
|
|
71
|
+
)
|
|
72
|
+
JsonCaptureResponse.new(result: result, raw_response: raw_response)
|
|
73
|
+
end
|
|
74
|
+
private_class_method :decode_json_response
|
|
75
|
+
|
|
76
|
+
def parse_json_success(raw_response)
|
|
77
|
+
JsonCodec.parse(raw_response.body)
|
|
78
|
+
rescue JSON::ParserError, EncodingError => e
|
|
79
|
+
raise ResponseDecodingError.new(
|
|
80
|
+
"Screenshot Scout returned a successful JSON response that could not be decoded.",
|
|
81
|
+
raw_response
|
|
82
|
+
), cause: e
|
|
83
|
+
end
|
|
84
|
+
private_class_method :parse_json_success
|
|
85
|
+
|
|
86
|
+
def read_optional_string(object, key, raw_response)
|
|
87
|
+
return nil unless object.key?(key)
|
|
88
|
+
return object[key] if object[key].is_a?(String)
|
|
89
|
+
|
|
90
|
+
cause = TypeError.new("Expected \"#{key}\" to be a string.")
|
|
91
|
+
raise ResponseDecodingError.new(
|
|
92
|
+
"Screenshot Scout returned a non-string \"#{key}\" JSON field.",
|
|
93
|
+
raw_response
|
|
94
|
+
), cause: cause
|
|
95
|
+
end
|
|
96
|
+
private_class_method :read_optional_string
|
|
97
|
+
|
|
98
|
+
def create_api_error(raw_response)
|
|
99
|
+
available, response_body = try_parse_json(raw_response.body)
|
|
100
|
+
object = response_body if response_body.is_a?(Hash)
|
|
101
|
+
error_code = object["error_code"] if object&.fetch("error_code", nil).is_a?(String)
|
|
102
|
+
error_message = object["error_message"] if object&.fetch("error_message", nil).is_a?(String)
|
|
103
|
+
errors = object["errors"] if object&.fetch("errors", nil).is_a?(Array)
|
|
104
|
+
|
|
105
|
+
APIError.new(
|
|
106
|
+
raw_response: raw_response,
|
|
107
|
+
response_body_available: available,
|
|
108
|
+
response_body: response_body,
|
|
109
|
+
error_code: error_code,
|
|
110
|
+
error_message: error_message,
|
|
111
|
+
errors: errors
|
|
112
|
+
)
|
|
113
|
+
end
|
|
114
|
+
private_class_method :create_api_error
|
|
115
|
+
|
|
116
|
+
def try_parse_json(body)
|
|
117
|
+
[true, JsonCodec.parse(body)]
|
|
118
|
+
rescue JSON::ParserError, EncodingError
|
|
119
|
+
[false, nil]
|
|
120
|
+
end
|
|
121
|
+
private_class_method :try_parse_json
|
|
122
|
+
|
|
123
|
+
def json_media_type?(content_type)
|
|
124
|
+
return false if content_type.nil?
|
|
125
|
+
|
|
126
|
+
media_type = content_type.partition(";").first.strip.downcase
|
|
127
|
+
media_type == "application/json" || media_type.end_with?("+json")
|
|
128
|
+
end
|
|
129
|
+
private_class_method :json_media_type?
|
|
130
|
+
|
|
131
|
+
def markdown_media_type?(content_type) = content_type.to_s.partition(";").first.strip.downcase == "text/markdown"
|
|
132
|
+
private_class_method :markdown_media_type?
|
|
133
|
+
|
|
134
|
+
def html_media_type?(content_type) = content_type.to_s.partition(";").first.strip.downcase == "text/html"
|
|
135
|
+
private_class_method :html_media_type?
|
|
136
|
+
|
|
137
|
+
def utf8_text(bytes) = bytes.dup.force_encoding(Encoding::UTF_8).scrub
|
|
138
|
+
private_class_method :utf8_text
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
@@ -82,16 +82,20 @@ module ScreenshotScout
|
|
|
82
82
|
cookies headers hide_selectors click_selectors click_all_selectors inject_css inject_js
|
|
83
83
|
].freeze
|
|
84
84
|
|
|
85
|
+
# What a capture renders, written before the options, and how error messages name it.
|
|
86
|
+
TARGETS = { url: "The capture target URL", html: "The HTML to capture" }.freeze
|
|
87
|
+
|
|
85
88
|
module_function
|
|
86
89
|
|
|
87
|
-
def serialize(
|
|
88
|
-
|
|
90
|
+
def serialize(target_value, options, target: :url)
|
|
91
|
+
target_value = utf8_from_binary(target_value) if target == :html
|
|
92
|
+
target_text = string_value(target_value, target, subject: TARGETS.fetch(target))
|
|
89
93
|
unless options.nil? || options.is_a?(CaptureOptions)
|
|
90
94
|
raise SerializationError, "Capture options must be a ScreenshotScout::CaptureOptions instance when provided."
|
|
91
95
|
end
|
|
92
96
|
|
|
93
|
-
pairs = [WirePair.new(name:
|
|
94
|
-
body = {
|
|
97
|
+
pairs = [WirePair.new(name: target.name, value: target_text)]
|
|
98
|
+
body = { target.name => target_text }
|
|
95
99
|
WIRE_NAMES.each do |property, wire_name|
|
|
96
100
|
value = options&.public_send(property)
|
|
97
101
|
next if value.nil?
|
|
@@ -189,15 +193,17 @@ module ScreenshotScout
|
|
|
189
193
|
end
|
|
190
194
|
private_class_method :integer_value
|
|
191
195
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
196
|
+
# HTML often comes from File.binread or an HTTP response body, so a binary string is read as UTF-8 bytes,
|
|
197
|
+
# which string_value then checks. The copy leaves the caller's string as it was.
|
|
198
|
+
def utf8_from_binary(value)
|
|
199
|
+
return value unless value.is_a?(String) && value.encoding == Encoding::BINARY
|
|
200
|
+
|
|
201
|
+
value.dup.force_encoding(Encoding::UTF_8)
|
|
202
|
+
end
|
|
203
|
+
private_class_method :utf8_from_binary
|
|
204
|
+
|
|
205
|
+
def string_value(value, property, subject: "The capture option \"#{property}\"")
|
|
206
|
+
raise SerializationError.new("#{subject} must be a string.", option: property) unless value.is_a?(String)
|
|
201
207
|
|
|
202
208
|
string = value.encode(Encoding::UTF_8)
|
|
203
209
|
# encode doesn't check the bytes of a string that is already tagged as UTF-8.
|
|
@@ -205,12 +211,7 @@ module ScreenshotScout
|
|
|
205
211
|
|
|
206
212
|
string
|
|
207
213
|
rescue EncodingError => e
|
|
208
|
-
|
|
209
|
-
"The capture target URL must contain valid UTF-8."
|
|
210
|
-
else
|
|
211
|
-
"The capture option \"#{property}\" must contain valid UTF-8."
|
|
212
|
-
end
|
|
213
|
-
raise SerializationError.new(message, option: property), cause: e
|
|
214
|
+
raise SerializationError.new("#{subject} must contain valid UTF-8.", option: property), cause: e
|
|
214
215
|
end
|
|
215
216
|
private_class_method :string_value
|
|
216
217
|
|
|
@@ -43,17 +43,18 @@ module ScreenshotScout
|
|
|
43
43
|
end
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
-
# Successful binary capture, including response metadata and image, PDF, or
|
|
46
|
+
# Successful binary capture, including response metadata and image, PDF, Markdown, or HTML bytes.
|
|
47
47
|
class BinaryCaptureResponse < CaptureResponse
|
|
48
|
-
attr_reader :bytes, :screenshot_url, :screenshot_url_expires_at, :cache_status, :markdown
|
|
48
|
+
attr_reader :bytes, :screenshot_url, :screenshot_url_expires_at, :cache_status, :markdown, :html
|
|
49
49
|
|
|
50
|
-
def initialize(raw_response:, screenshot_url:, screenshot_url_expires_at:, cache_status:, markdown: nil)
|
|
50
|
+
def initialize(raw_response:, screenshot_url:, screenshot_url_expires_at:, cache_status:, markdown: nil, html: nil)
|
|
51
51
|
super(raw_response: raw_response)
|
|
52
52
|
@bytes = raw_response.body
|
|
53
53
|
@screenshot_url = screenshot_url
|
|
54
54
|
@screenshot_url_expires_at = screenshot_url_expires_at
|
|
55
55
|
@cache_status = cache_status
|
|
56
56
|
@markdown = markdown
|
|
57
|
+
@html = html
|
|
57
58
|
freeze
|
|
58
59
|
end
|
|
59
60
|
end
|
data/lib/screenshotscout.rb
CHANGED
|
@@ -9,5 +9,6 @@ require_relative "screenshotscout/transport"
|
|
|
9
9
|
require_relative "screenshotscout/internal/credentials"
|
|
10
10
|
require_relative "screenshotscout/internal/ecma_script_number_formatter"
|
|
11
11
|
require_relative "screenshotscout/internal/json_codec"
|
|
12
|
+
require_relative "screenshotscout/internal/response_decoder"
|
|
12
13
|
require_relative "screenshotscout/internal/serializer"
|
|
13
14
|
require_relative "screenshotscout/client"
|
data/sig/screenshotscout.rbs
CHANGED
|
@@ -12,6 +12,7 @@ module ScreenshotScout
|
|
|
12
12
|
|
|
13
13
|
module CaptureFormat
|
|
14
14
|
GIF: String
|
|
15
|
+
HTML: String
|
|
15
16
|
JPEG: String
|
|
16
17
|
JPG: String
|
|
17
18
|
MD: String
|
|
@@ -248,13 +249,15 @@ module ScreenshotScout
|
|
|
248
249
|
attr_reader screenshot_url_expires_at: String?
|
|
249
250
|
attr_reader cache_status: String?
|
|
250
251
|
attr_reader markdown: String?
|
|
252
|
+
attr_reader html: String?
|
|
251
253
|
|
|
252
254
|
def initialize: (
|
|
253
255
|
raw_response: RawResponse,
|
|
254
256
|
screenshot_url: String?,
|
|
255
257
|
screenshot_url_expires_at: String?,
|
|
256
258
|
cache_status: String?,
|
|
257
|
-
?markdown: String
|
|
259
|
+
?markdown: String?,
|
|
260
|
+
?html: String?
|
|
258
261
|
) -> void
|
|
259
262
|
end
|
|
260
263
|
|
|
@@ -315,6 +318,8 @@ module ScreenshotScout
|
|
|
315
318
|
?method: capture_http_method?
|
|
316
319
|
) -> CaptureResponse
|
|
317
320
|
|
|
321
|
+
def capture_from_html: (String html, ?CaptureOptions? options) -> CaptureResponse
|
|
322
|
+
|
|
318
323
|
def build_capture_url: (String url, ?CaptureOptions? options) -> String
|
|
319
324
|
|
|
320
325
|
def inspect: () -> String
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: screenshotscout
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Oleksii Velykyi
|
|
@@ -76,6 +76,7 @@ files:
|
|
|
76
76
|
- examples/README.md
|
|
77
77
|
- examples/binary.rb
|
|
78
78
|
- examples/capture_url.rb
|
|
79
|
+
- examples/html.rb
|
|
79
80
|
- examples/json.rb
|
|
80
81
|
- lib/screenshotscout.rb
|
|
81
82
|
- lib/screenshotscout/capture_options.rb
|
|
@@ -85,6 +86,7 @@ files:
|
|
|
85
86
|
- lib/screenshotscout/internal/credentials.rb
|
|
86
87
|
- lib/screenshotscout/internal/ecma_script_number_formatter.rb
|
|
87
88
|
- lib/screenshotscout/internal/json_codec.rb
|
|
89
|
+
- lib/screenshotscout/internal/response_decoder.rb
|
|
88
90
|
- lib/screenshotscout/internal/serializer.rb
|
|
89
91
|
- lib/screenshotscout/responses.rb
|
|
90
92
|
- lib/screenshotscout/transport.rb
|