screenshotscout 0.4.1 → 0.6.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 +55 -36
- data/examples/README.md +4 -4
- data/examples/binary.rb +1 -2
- data/examples/capture_url.rb +2 -3
- data/examples/json.rb +1 -2
- data/lib/screenshotscout/client.rb +36 -19
- data/lib/screenshotscout/internal/credentials.rb +76 -0
- data/lib/screenshotscout/transport.rb +7 -0
- data/lib/screenshotscout/version.rb +1 -1
- data/lib/screenshotscout.rb +1 -0
- data/sig/screenshotscout.rbs +6 -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: 453a6daf83880b4a4944a622edeef17dc85fc6fb9ead7f88d7cdd5f70bcd12be
|
|
4
|
+
data.tar.gz: ba96a2e79656528cae4fb5de5ab316a95b9866e87683fcae92e1235e1554b101
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a668350fff10e050c92041e25b4dd6cc7c00744d913ed182c15d8f836697a68a13a46a3e39d53ce486f7fd1e72a6c1e1e99790d8b1be22f4841a60e49dbca6fb
|
|
7
|
+
data.tar.gz: 49cc93d3c90d719a4b9f5fcb15db3e09562c9b14eee94c6a3af0c2eebe7ef88c6a978c970e34fde34256defe6605c351f6c82faa3cdc4388e1b21c7bfbbf5881
|
data/README.md
CHANGED
|
@@ -25,12 +25,11 @@ gem install screenshotscout
|
|
|
25
25
|
## Get your API credentials
|
|
26
26
|
|
|
27
27
|
[Sign up for Screenshot Scout](https://screenshotscout.com/auth/signup) or sign in, then open the
|
|
28
|
-
[API Keys page](https://screenshotscout.com/app/api-keys)
|
|
29
|
-
|
|
30
|
-
[signed requests](#signed-requests).
|
|
28
|
+
[API Keys page](https://screenshotscout.com/app/api-keys) and copy the access key. You only need the
|
|
29
|
+
secret key for [signed requests](#signed-requests).
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
Set the access key as the `SCREENSHOTSCOUT_ACCESS_KEY` environment variable, or
|
|
32
|
+
[pass it in code](#pass-the-keys-yourself).
|
|
34
33
|
|
|
35
34
|
## Capture a screenshot
|
|
36
35
|
|
|
@@ -40,7 +39,7 @@ the full page and saves the returned image:
|
|
|
40
39
|
```ruby
|
|
41
40
|
require "screenshotscout"
|
|
42
41
|
|
|
43
|
-
client = ScreenshotScout::Client.new
|
|
42
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
44
43
|
response = client.capture(
|
|
45
44
|
"https://example.com",
|
|
46
45
|
ScreenshotScout::CaptureOptions.new(full_page: true)
|
|
@@ -50,8 +49,8 @@ File.binwrite("screenshot.png", response.bytes)
|
|
|
50
49
|
puts response.screenshot_url
|
|
51
50
|
```
|
|
52
51
|
|
|
53
|
-
`capture` uses POST by default
|
|
54
|
-
omitted or set to `CaptureResponseType::BINARY`.
|
|
52
|
+
`capture` uses POST by default, or GET when the client has a secret key. It returns a
|
|
53
|
+
`BinaryCaptureResponse` when `response_type` is omitted or set to `CaptureResponseType::BINARY`.
|
|
55
54
|
|
|
56
55
|
## Request a JSON result
|
|
57
56
|
|
|
@@ -61,7 +60,7 @@ image or PDF output:
|
|
|
61
60
|
```ruby
|
|
62
61
|
require "screenshotscout"
|
|
63
62
|
|
|
64
|
-
client = ScreenshotScout::Client.new
|
|
63
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
65
64
|
options = ScreenshotScout::CaptureOptions.new(
|
|
66
65
|
response_type: ScreenshotScout::CaptureResponseType::JSON
|
|
67
66
|
)
|
|
@@ -74,15 +73,13 @@ Unrecognized JSON result fields are retained in `response.result.additional_fiel
|
|
|
74
73
|
|
|
75
74
|
## Use GET
|
|
76
75
|
|
|
77
|
-
POST is
|
|
76
|
+
POST is the default, or GET when the client has a secret key. Pass `CaptureHttpMethod::GET` when
|
|
77
|
+
you need a GET request:
|
|
78
78
|
|
|
79
79
|
```ruby
|
|
80
80
|
require "screenshotscout"
|
|
81
81
|
|
|
82
|
-
client = ScreenshotScout::Client.new
|
|
83
|
-
access_key: ENV.fetch("SCREENSHOTSCOUT_ACCESS_KEY"),
|
|
84
|
-
secret_key: ENV.fetch("SCREENSHOTSCOUT_SECRET_KEY", nil)
|
|
85
|
-
)
|
|
82
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
86
83
|
options = ScreenshotScout::CaptureOptions.new(format: ScreenshotScout::CaptureFormat::WEBP)
|
|
87
84
|
response = client.capture(
|
|
88
85
|
"https://example.com",
|
|
@@ -101,41 +98,65 @@ load the screenshot directly. It creates the URL without sending an API request:
|
|
|
101
98
|
```ruby
|
|
102
99
|
require "screenshotscout"
|
|
103
100
|
|
|
104
|
-
client = ScreenshotScout::Client.new
|
|
105
|
-
access_key: ENV.fetch("SCREENSHOTSCOUT_ACCESS_KEY"),
|
|
106
|
-
secret_key: ENV.fetch("SCREENSHOTSCOUT_SECRET_KEY", nil)
|
|
107
|
-
)
|
|
101
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
108
102
|
options = ScreenshotScout::CaptureOptions.new(full_page: true, block_ads: true)
|
|
109
103
|
capture_url = client.build_capture_url("https://example.com", options)
|
|
110
104
|
puts capture_url
|
|
111
105
|
```
|
|
112
106
|
|
|
113
|
-
The generated URL contains the access key.
|
|
107
|
+
The generated URL contains the access key. When the client has a secret key, the URL is signed
|
|
108
|
+
automatically; otherwise it is unsigned. Treat it as sensitive. Before exposing generated URLs
|
|
114
109
|
to browsers or users, configure a secret key and enable **Require signed requests** for the API key
|
|
115
110
|
on the [API Keys page](https://screenshotscout.com/app/api-keys).
|
|
116
111
|
|
|
117
112
|
## Signed requests
|
|
118
113
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
114
|
+
Set the API key's secret key as the `SCREENSHOTSCOUT_SECRET_KEY` environment variable. The client
|
|
115
|
+
then signs its capture requests and the capture URLs it builds, with no change to your code:
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
require "screenshotscout"
|
|
119
|
+
|
|
120
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY and SCREENSHOTSCOUT_SECRET_KEY
|
|
121
|
+
|
|
122
|
+
response = client.capture("https://example.com")
|
|
123
|
+
File.binwrite("signed-screenshot.png", response.bytes)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The secret key is used locally and is never transmitted. See the
|
|
127
|
+
[signed requests guide](https://screenshotscout.com/docs/signed-requests) for details.
|
|
128
|
+
|
|
129
|
+
## Pass the keys yourself
|
|
130
|
+
|
|
131
|
+
To use keys from somewhere other than the environment, such as a secrets manager or per-customer
|
|
132
|
+
settings, pass them to the client:
|
|
122
133
|
|
|
123
134
|
```ruby
|
|
124
135
|
require "screenshotscout"
|
|
125
136
|
|
|
126
137
|
client = ScreenshotScout::Client.new(
|
|
127
|
-
access_key:
|
|
128
|
-
secret_key:
|
|
138
|
+
access_key: config.access_key,
|
|
139
|
+
secret_key: config.secret_key # optional
|
|
129
140
|
)
|
|
141
|
+
```
|
|
130
142
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
143
|
+
If you pass either key, the client reads neither environment variable. If you pass no key, for
|
|
144
|
+
example because the variable holding it is `nil`, the client reads both environment variables
|
|
145
|
+
instead.
|
|
146
|
+
|
|
147
|
+
## Send requests to another server
|
|
148
|
+
|
|
149
|
+
To send requests to another server, such as your own proxy or a mock server, pass its base URL, or
|
|
150
|
+
set it as the `SCREENSHOTSCOUT_BASE_URL` environment variable:
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
require "screenshotscout"
|
|
154
|
+
|
|
155
|
+
client = ScreenshotScout::Client.new(base_url: "https://proxy.example.com") # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
136
156
|
```
|
|
137
157
|
|
|
138
|
-
|
|
158
|
+
The client sends captures to the base URL plus `/v1/capture`, and `build_capture_url` builds URLs on
|
|
159
|
+
it. A base URL you pass wins over the variable.
|
|
139
160
|
|
|
140
161
|
## Capture options
|
|
141
162
|
|
|
@@ -162,7 +183,7 @@ Use the provided constants for documented values, or pass a string:
|
|
|
162
183
|
```ruby
|
|
163
184
|
require "screenshotscout"
|
|
164
185
|
|
|
165
|
-
client = ScreenshotScout::Client.new
|
|
186
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
166
187
|
options = ScreenshotScout::CaptureOptions.new(
|
|
167
188
|
format: ScreenshotScout::CaptureFormat::WEBP,
|
|
168
189
|
wait_until: ScreenshotScout::CaptureWaitUntil::LOAD
|
|
@@ -234,10 +255,8 @@ class ApplicationTransport
|
|
|
234
255
|
end
|
|
235
256
|
end
|
|
236
257
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
transport: ApplicationTransport.new(open_timeout: 10, read_timeout: 300)
|
|
240
|
-
)
|
|
258
|
+
transport = ApplicationTransport.new(open_timeout: 10, read_timeout: 300)
|
|
259
|
+
client = ScreenshotScout::Client.new(transport: transport) # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
241
260
|
|
|
242
261
|
response = client.capture("https://example.com")
|
|
243
262
|
File.binwrite("screenshot.png", response.bytes)
|
|
@@ -251,7 +270,7 @@ headers, content type, and body. `APIError` provides the same response details.
|
|
|
251
270
|
```ruby
|
|
252
271
|
require "screenshotscout"
|
|
253
272
|
|
|
254
|
-
client = ScreenshotScout::Client.new
|
|
273
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
255
274
|
|
|
256
275
|
begin
|
|
257
276
|
client.capture("https://example.com")
|
data/examples/README.md
CHANGED
|
@@ -14,21 +14,21 @@ From the repository root, install the dependencies:
|
|
|
14
14
|
bundle install
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
Set your access key.
|
|
18
|
-
|
|
17
|
+
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.
|
|
19
19
|
|
|
20
20
|
PowerShell:
|
|
21
21
|
|
|
22
22
|
```powershell
|
|
23
23
|
$env:SCREENSHOTSCOUT_ACCESS_KEY = "YOUR_ACCESS_KEY"
|
|
24
|
-
$env:SCREENSHOTSCOUT_SECRET_KEY = "YOUR_SECRET_KEY" # Optional
|
|
24
|
+
$env:SCREENSHOTSCOUT_SECRET_KEY = "YOUR_SECRET_KEY" # Optional
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
macOS or Linux:
|
|
28
28
|
|
|
29
29
|
```shell
|
|
30
30
|
export SCREENSHOTSCOUT_ACCESS_KEY="YOUR_ACCESS_KEY"
|
|
31
|
-
export SCREENSHOTSCOUT_SECRET_KEY="YOUR_SECRET_KEY" # Optional
|
|
31
|
+
export SCREENSHOTSCOUT_SECRET_KEY="YOUR_SECRET_KEY" # Optional
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
Run any example:
|
data/examples/binary.rb
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "screenshotscout"
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
client = ScreenshotScout::Client.new(access_key: access_key)
|
|
5
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
7
6
|
options = ScreenshotScout::CaptureOptions.new(full_page: true)
|
|
8
7
|
response = client.capture("https://example.com", options)
|
|
9
8
|
|
data/examples/capture_url.rb
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require "screenshotscout"
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
client = ScreenshotScout::Client.new(access_key: access_key, secret_key: secret_key)
|
|
5
|
+
# Reads SCREENSHOTSCOUT_ACCESS_KEY, and SCREENSHOTSCOUT_SECRET_KEY if it's set
|
|
6
|
+
client = ScreenshotScout::Client.new
|
|
8
7
|
options = ScreenshotScout::CaptureOptions.new(full_page: true, block_ads: true)
|
|
9
8
|
|
|
10
9
|
puts client.build_capture_url("https://example.com", options)
|
data/examples/json.rb
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "screenshotscout"
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
client = ScreenshotScout::Client.new(access_key: access_key)
|
|
5
|
+
client = ScreenshotScout::Client.new # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
7
6
|
options = ScreenshotScout::CaptureOptions.new(
|
|
8
7
|
response_type: ScreenshotScout::CaptureResponseType::JSON
|
|
9
8
|
)
|
|
@@ -5,27 +5,36 @@ require "openssl"
|
|
|
5
5
|
module ScreenshotScout
|
|
6
6
|
# Reusable blocking client for the inline Screenshot Scout capture operation.
|
|
7
7
|
class Client
|
|
8
|
-
CAPTURE_ENDPOINT = "https://api.screenshotscout.com/v1/capture"
|
|
9
8
|
ACCESS_KEY_PATTERN = %r{\A[A-Za-z0-9\-._~+/]+=*\z}
|
|
10
|
-
MISSING_ACCESS_KEY = Object.new.freeze
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
# Creates a client. When no key is passed, the client reads SCREENSHOTSCOUT_ACCESS_KEY and, if it's set,
|
|
11
|
+
# SCREENSHOTSCOUT_SECRET_KEY. Passing either key means neither variable is read. Pass base_url to send
|
|
12
|
+
# requests to another server, such as your own proxy or a mock server: captures go to that URL plus
|
|
13
|
+
# /v1/capture, and build_capture_url builds URLs on it. When no base URL is passed, the client reads
|
|
14
|
+
# SCREENSHOTSCOUT_BASE_URL, and without it uses the Screenshot Scout API.
|
|
15
|
+
def initialize(access_key: nil, secret_key: nil, base_url: nil, transport: nil)
|
|
16
|
+
credentials = Internal::Credentials.resolve(access_key, secret_key, environment: ENV)
|
|
17
|
+
validate_access_key(credentials.access_key)
|
|
18
|
+
validate_secret_key(credentials.secret_key)
|
|
15
19
|
validate_transport(transport)
|
|
20
|
+
endpoint = Internal::Credentials.resolve_base_url(base_url, environment: ENV)
|
|
16
21
|
|
|
17
|
-
@access_key = access_key.dup.freeze
|
|
18
|
-
@secret_key = secret_key&.dup&.freeze
|
|
22
|
+
@access_key = credentials.access_key.dup.freeze
|
|
23
|
+
@secret_key = credentials.secret_key&.dup&.freeze
|
|
24
|
+
@secret_key_from_environment = credentials.from_environment && !@secret_key.nil?
|
|
25
|
+
@capture_endpoint = endpoint.capture_endpoint
|
|
26
|
+
@base_url_from_environment = endpoint.from_environment
|
|
19
27
|
@transport = transport || NetHttpTransport.new
|
|
20
28
|
end
|
|
21
29
|
|
|
22
|
-
# Sends one GET or POST request and waits for the final inline response.
|
|
23
|
-
#
|
|
24
|
-
|
|
30
|
+
# Sends one GET or POST request and waits for the final inline response. The method defaults to POST,
|
|
31
|
+
# or GET when the client has a secret key, because POST requests can't be signed. A client with a secret
|
|
32
|
+
# key signs GET requests and raises ConfigurationError for an explicit POST.
|
|
33
|
+
def capture(url, options = nil, method: nil)
|
|
25
34
|
serialized = Internal::Serializer.serialize(url, options)
|
|
26
|
-
method
|
|
27
|
-
validate_method(method)
|
|
35
|
+
validate_method(method) unless method.nil?
|
|
28
36
|
reject_signed_post(method)
|
|
37
|
+
method ||= @secret_key.nil? ? CaptureHttpMethod::POST : CaptureHttpMethod::GET
|
|
29
38
|
expected_kind = expected_response_kind(options)
|
|
30
39
|
signature = create_signature(serialized.pairs)
|
|
31
40
|
request = create_request(serialized, method, signature)
|
|
@@ -42,7 +51,12 @@ module ScreenshotScout
|
|
|
42
51
|
signature = create_signature(serialized.pairs)
|
|
43
52
|
pairs = [Internal::WirePair.new(name: "access_key", value: @access_key), *serialized.pairs]
|
|
44
53
|
pairs << Internal::WirePair.new(name: "signature", value: signature) unless signature.nil?
|
|
45
|
-
"#{
|
|
54
|
+
"#{@capture_endpoint}?#{Internal::Serializer.encode_query(pairs)}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Leaves out the keys, so that printing or logging a client doesn't reveal them.
|
|
58
|
+
def inspect
|
|
59
|
+
"#<#{self.class.name}>"
|
|
46
60
|
end
|
|
47
61
|
|
|
48
62
|
private
|
|
@@ -81,9 +95,10 @@ module ScreenshotScout
|
|
|
81
95
|
def reject_signed_post(method)
|
|
82
96
|
return if @secret_key.nil? || method != CaptureHttpMethod::POST
|
|
83
97
|
|
|
98
|
+
source = @secret_key_from_environment ? " from #{Internal::Credentials::SECRET_KEY_VARIABLE}" : ""
|
|
84
99
|
raise ConfigurationError,
|
|
85
|
-
"POST requests can't be signed. This client has a secret key: send this capture with
|
|
86
|
-
"or use a client without a secret key and an API key that doesn't require signed requests."
|
|
100
|
+
"POST requests can't be signed. This client has a secret key#{source}: send this capture with " \
|
|
101
|
+
"method GET, or use a client without a secret key and an API key that doesn't require signed requests."
|
|
87
102
|
end
|
|
88
103
|
|
|
89
104
|
def create_signature(pairs)
|
|
@@ -102,13 +117,13 @@ module ScreenshotScout
|
|
|
102
117
|
if method == CaptureHttpMethod::GET
|
|
103
118
|
pairs = serialized.pairs.dup
|
|
104
119
|
pairs << Internal::WirePair.new(name: "signature", value: signature) unless signature.nil?
|
|
105
|
-
url = "#{
|
|
120
|
+
url = "#{@capture_endpoint}?#{Internal::Serializer.encode_query(pairs)}"
|
|
106
121
|
TransportRequest.new(method: method, url: url, headers: headers)
|
|
107
122
|
else
|
|
108
123
|
headers["Content-Type"] = "application/json"
|
|
109
124
|
TransportRequest.new(
|
|
110
125
|
method: method,
|
|
111
|
-
url:
|
|
126
|
+
url: @capture_endpoint,
|
|
112
127
|
headers: headers,
|
|
113
128
|
body: Internal::JsonCodec.encode_wire_object(serialized.body)
|
|
114
129
|
)
|
|
@@ -127,7 +142,9 @@ module ScreenshotScout
|
|
|
127
142
|
rescue Error
|
|
128
143
|
raise
|
|
129
144
|
rescue StandardError => e
|
|
130
|
-
|
|
145
|
+
source = @base_url_from_environment ? " The base URL comes from #{Internal::Credentials::BASE_URL_VARIABLE}." : ""
|
|
146
|
+
raise TransportError,
|
|
147
|
+
"Screenshot Scout request failed before a complete HTTP response body was received.#{source}",
|
|
131
148
|
cause: e
|
|
132
149
|
end
|
|
133
150
|
|
|
@@ -233,6 +250,6 @@ module ScreenshotScout
|
|
|
233
250
|
media_type == "application/json" || media_type.end_with?("+json")
|
|
234
251
|
end
|
|
235
252
|
|
|
236
|
-
private_constant :
|
|
253
|
+
private_constant :ACCESS_KEY_PATTERN
|
|
237
254
|
end
|
|
238
255
|
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ScreenshotScout
|
|
4
|
+
module Internal
|
|
5
|
+
# Picks the keys and the base URL a client uses: the values passed to it, or else the environment variables.
|
|
6
|
+
module Credentials
|
|
7
|
+
ACCESS_KEY_VARIABLE = "SCREENSHOTSCOUT_ACCESS_KEY"
|
|
8
|
+
SECRET_KEY_VARIABLE = "SCREENSHOTSCOUT_SECRET_KEY"
|
|
9
|
+
BASE_URL_VARIABLE = "SCREENSHOTSCOUT_BASE_URL"
|
|
10
|
+
DEFAULT_BASE_URL = "https://api.screenshotscout.com"
|
|
11
|
+
CAPTURE_PATH = "/v1/capture"
|
|
12
|
+
|
|
13
|
+
# The keys to validate, and whether they were read from the environment.
|
|
14
|
+
Resolved = Data.define(:access_key, :secret_key, :from_environment)
|
|
15
|
+
|
|
16
|
+
# The URL captures are sent to and capture URLs are built on, and whether the base URL was read from the
|
|
17
|
+
# environment.
|
|
18
|
+
ResolvedBaseUrl = Data.define(:capture_endpoint, :from_environment)
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# Keys passed to the client always win: if either one is passed, neither variable is read. Only when
|
|
23
|
+
# both are nil are both read, as environment[name], which ENV, a Hash and a lambda all support.
|
|
24
|
+
def resolve(access_key, secret_key, environment:)
|
|
25
|
+
unless access_key.nil? && secret_key.nil?
|
|
26
|
+
return Resolved.new(access_key: access_key, secret_key: secret_key, from_environment: false)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
environment_access_key = read_variable(environment, ACCESS_KEY_VARIABLE)
|
|
30
|
+
environment_secret_key = read_variable(environment, SECRET_KEY_VARIABLE)
|
|
31
|
+
if environment_access_key.nil?
|
|
32
|
+
raise ConfigurationError,
|
|
33
|
+
"A non-blank Screenshot Scout access key is required. Pass one, or set #{ACCESS_KEY_VARIABLE}."
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Resolved.new(access_key: environment_access_key, secret_key: environment_secret_key, from_environment: true)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A base URL passed to the client wins. Without one, the variable is read, whether or not keys were passed,
|
|
40
|
+
# and without that the production API is used. The base URL is used as given: only trailing slashes are
|
|
41
|
+
# removed before the capture path is appended.
|
|
42
|
+
def resolve_base_url(base_url, environment:)
|
|
43
|
+
unless base_url.nil? || base_url.is_a?(String)
|
|
44
|
+
raise ConfigurationError, "The Screenshot Scout base URL must be a string when provided."
|
|
45
|
+
end
|
|
46
|
+
unless base_url.nil? || base_url.empty?
|
|
47
|
+
return ResolvedBaseUrl.new(capture_endpoint: append_capture_path(base_url), from_environment: false)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
environment_base_url = read_variable(environment, BASE_URL_VARIABLE)
|
|
51
|
+
if environment_base_url.nil?
|
|
52
|
+
ResolvedBaseUrl.new(capture_endpoint: append_capture_path(DEFAULT_BASE_URL), from_environment: false)
|
|
53
|
+
else
|
|
54
|
+
ResolvedBaseUrl.new(capture_endpoint: append_capture_path(environment_base_url), from_environment: true)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Uses no regular expression, which would raise ArgumentError for text that isn't valid UTF-8.
|
|
59
|
+
def append_capture_path(base_url)
|
|
60
|
+
base_url = base_url.delete_suffix("/") while base_url.end_with?("/")
|
|
61
|
+
base_url + CAPTURE_PATH
|
|
62
|
+
end
|
|
63
|
+
private_class_method :append_capture_path
|
|
64
|
+
|
|
65
|
+
# Returns nil for a variable that is unset or empty.
|
|
66
|
+
def read_variable(environment, name)
|
|
67
|
+
value = environment[name]
|
|
68
|
+
return nil if value.nil? || value.empty?
|
|
69
|
+
raise ConfigurationError, "#{name} is set but blank." if value.strip.empty?
|
|
70
|
+
|
|
71
|
+
value
|
|
72
|
+
end
|
|
73
|
+
private_class_method :read_variable
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -17,6 +17,13 @@ module ScreenshotScout
|
|
|
17
17
|
@body = body&.dup&.freeze
|
|
18
18
|
freeze
|
|
19
19
|
end
|
|
20
|
+
|
|
21
|
+
# Shows the Authorization header, which carries the access key, as [FILTERED].
|
|
22
|
+
def inspect
|
|
23
|
+
headers = @headers.to_h { |name, value| [name, name.casecmp?("Authorization") ? "[FILTERED]" : value] }
|
|
24
|
+
"#<#{self.class.name} method=#{@method.inspect} url=#{@url.inspect} headers=#{headers.inspect} " \
|
|
25
|
+
"body=#{@body.inspect}>"
|
|
26
|
+
end
|
|
20
27
|
end
|
|
21
28
|
|
|
22
29
|
# Small default adapter. It owns each Net::HTTP session that it opens.
|
data/lib/screenshotscout.rb
CHANGED
|
@@ -6,6 +6,7 @@ require_relative "screenshotscout/errors"
|
|
|
6
6
|
require_relative "screenshotscout/capture_options"
|
|
7
7
|
require_relative "screenshotscout/responses"
|
|
8
8
|
require_relative "screenshotscout/transport"
|
|
9
|
+
require_relative "screenshotscout/internal/credentials"
|
|
9
10
|
require_relative "screenshotscout/internal/ecma_script_number_formatter"
|
|
10
11
|
require_relative "screenshotscout/internal/json_codec"
|
|
11
12
|
require_relative "screenshotscout/internal/serializer"
|
data/sig/screenshotscout.rbs
CHANGED
|
@@ -286,6 +286,8 @@ module ScreenshotScout
|
|
|
286
286
|
headers: Hash[String, String],
|
|
287
287
|
?body: String?
|
|
288
288
|
) -> void
|
|
289
|
+
|
|
290
|
+
def inspect: () -> String
|
|
289
291
|
end
|
|
290
292
|
|
|
291
293
|
interface _Transport
|
|
@@ -298,8 +300,9 @@ module ScreenshotScout
|
|
|
298
300
|
|
|
299
301
|
class Client
|
|
300
302
|
def initialize: (
|
|
301
|
-
access_key: String
|
|
303
|
+
?access_key: String?,
|
|
302
304
|
?secret_key: String?,
|
|
305
|
+
?base_url: String?,
|
|
303
306
|
?transport: _Transport?
|
|
304
307
|
) -> void
|
|
305
308
|
|
|
@@ -310,6 +313,8 @@ module ScreenshotScout
|
|
|
310
313
|
) -> CaptureResponse
|
|
311
314
|
|
|
312
315
|
def build_capture_url: (String url, ?CaptureOptions? options) -> String
|
|
316
|
+
|
|
317
|
+
def inspect: () -> String
|
|
313
318
|
end
|
|
314
319
|
|
|
315
320
|
class Error < StandardError
|
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.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Oleksii Velykyi
|
|
@@ -82,6 +82,7 @@ files:
|
|
|
82
82
|
- lib/screenshotscout/capture_values.rb
|
|
83
83
|
- lib/screenshotscout/client.rb
|
|
84
84
|
- lib/screenshotscout/errors.rb
|
|
85
|
+
- lib/screenshotscout/internal/credentials.rb
|
|
85
86
|
- lib/screenshotscout/internal/ecma_script_number_formatter.rb
|
|
86
87
|
- lib/screenshotscout/internal/json_codec.rb
|
|
87
88
|
- lib/screenshotscout/internal/serializer.rb
|