og_pilot_ruby 0.4.1 → 0.4.2
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 +12 -3
- data/lib/og_pilot_ruby/client.rb +20 -0
- data/lib/og_pilot_ruby/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7dfbdfc6efc8c0fc36a6b92c6b99af56c0e0ef3b3831d7b91b8e56056b09dd16
|
|
4
|
+
data.tar.gz: 4fe12f91a949bd6117a915b48b88df8d25fda2d9665a5bba1d78533557cad202
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6cb305db104f09b0fb0cfc430f8fd1fdc276d02d048e4f4936bde4ba5273569568e351e5fdafe9287bb33fc9f9ba5d94279c38f161ebb4f1a36a87aa56a331c0
|
|
7
|
+
data.tar.gz: e144441605b592d95c672242cb90813c1056dc498fd045a10ee0882dfa157a30600585f11effa2fb976983189e14a53170888a3323c8f4cc3198f458c96a6ca8
|
data/README.md
CHANGED
|
@@ -61,6 +61,15 @@ image_url = OgPilotRuby.create_image(
|
|
|
61
61
|
If you omit `iat`, OG Pilot will cache the image indefinitely. Provide an `iat` to
|
|
62
62
|
refresh the cache daily.
|
|
63
63
|
|
|
64
|
+
### Fail-safe behavior
|
|
65
|
+
|
|
66
|
+
`create_image` is non-blocking. If any error occurs (request, configuration,
|
|
67
|
+
validation, parsing, etc.), the gem does not raise to your app and logs an
|
|
68
|
+
error-level message instead.
|
|
69
|
+
|
|
70
|
+
- URL mode (`json: false`, default): returns `nil`
|
|
71
|
+
- JSON mode (`json: true`): returns `{ "image_url" => nil }`
|
|
72
|
+
|
|
64
73
|
### Template helpers
|
|
65
74
|
|
|
66
75
|
`create_image` defaults to the `page` template when `template` is omitted.
|
|
@@ -119,7 +128,7 @@ The gem handles `iss` (domain) and `sub` (API key prefix) automatically.
|
|
|
119
128
|
|
|
120
129
|
| Option | Default | Description |
|
|
121
130
|
|-----------|---------|--------------------------------------------------------------------------|
|
|
122
|
-
| `json` | `false` | When `true`, sends `Accept: application/json` and parses the JSON response |
|
|
131
|
+
| `json` | `false` | When `true`, sends `Accept: application/json` and parses the JSON response. On failure, returns `{ "image_url" => nil }` |
|
|
123
132
|
| `headers` | — | Additional HTTP headers to include with the request |
|
|
124
133
|
| `default` | `false` | Forces `path` to `/` when `true`, unless a manual `path` is provided (see [Path handling](#path-handling)) |
|
|
125
134
|
|
|
@@ -173,7 +182,7 @@ Fetch JSON metadata instead:
|
|
|
173
182
|
```ruby
|
|
174
183
|
payload = {
|
|
175
184
|
template: "page",
|
|
176
|
-
title: "Hello OG Pilot"
|
|
185
|
+
title: "Hello OG Pilot"
|
|
177
186
|
}
|
|
178
187
|
|
|
179
188
|
data = OgPilotRuby.create_image(**payload, json: true)
|
|
@@ -190,7 +199,7 @@ Multiple extensions are also stripped (`/archive.tar.gz` becomes `/archive`).
|
|
|
190
199
|
Dotfiles like `/.hidden` are left unchanged. Query strings are preserved.
|
|
191
200
|
|
|
192
201
|
```ruby
|
|
193
|
-
OgPilotRuby.configure do |config
|
|
202
|
+
OgPilotRuby.configure do |config|
|
|
194
203
|
config.strip_extensions = true
|
|
195
204
|
end
|
|
196
205
|
|
data/lib/og_pilot_ruby/client.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
|
+
require "logger"
|
|
4
5
|
require "net/http"
|
|
5
6
|
require "uri"
|
|
6
7
|
|
|
@@ -32,12 +33,31 @@ module OgPilotRuby
|
|
|
32
33
|
else
|
|
33
34
|
response["Location"] || final_uri.to_s
|
|
34
35
|
end
|
|
36
|
+
rescue StandardError => e
|
|
37
|
+
log_create_image_failure(e, json:)
|
|
38
|
+
json ? { "image_url" => nil } : nil
|
|
35
39
|
end
|
|
36
40
|
|
|
37
41
|
private
|
|
38
42
|
|
|
39
43
|
attr_reader :config
|
|
40
44
|
|
|
45
|
+
def log_create_image_failure(error, json:)
|
|
46
|
+
mode = json ? "json" : "url"
|
|
47
|
+
message = "OgPilotRuby create_image failed (mode=#{mode}): #{error.class}: #{error.message}"
|
|
48
|
+
create_image_logger&.error(message)
|
|
49
|
+
rescue StandardError
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def create_image_logger
|
|
54
|
+
if defined?(::Rails) && ::Rails.respond_to?(:logger) && ::Rails.logger
|
|
55
|
+
::Rails.logger
|
|
56
|
+
else
|
|
57
|
+
@create_image_logger ||= Logger.new($stderr)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
41
61
|
def request(uri, json:, headers:, method: :post, redirects_left: MAX_REDIRECTS)
|
|
42
62
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
43
63
|
http.use_ssl = uri.scheme == "https"
|