og_pilot_ruby 0.4.6 → 0.4.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfefae49c597192416e12b3ce37cbeb6c9a96978cd0117544cab4ed3d334d754
4
- data.tar.gz: 1ad8da7a1af0b52c0a039fce941d5e35c0718317586fbe694b537886c6e7e14d
3
+ metadata.gz: 7337416e1651ceadf68ca9eba08b8a4a4945d62f30154391006b2dd11cd7e892
4
+ data.tar.gz: ebd7c609da7e75151f6f06741b82a19d244ca3953e7f563f496f7f268c3ec5cb
5
5
  SHA512:
6
- metadata.gz: 3c43d41211ef1bbe5685565ce341adea1c9e56b8007e322c37a7561ee5b068ba944176826a7455546ec6580b53b3a8b3f640064ad30dada66a25df1035230f1a
7
- data.tar.gz: 8967f05bb0129386f0b8397f401918ccecc54127a6f939066615a960fa98433e3863579bfc59b926373631b0d0513624c8fff68a3715c8fff0192d464a8b5f90
6
+ metadata.gz: 06fcd6974ee8842cff3cb77747cb0c8a011207aecc5d0fced3f92ea049fd6109343d0bb9395fbe8798267a681b79a1d72dded42935c75e05c8e99b1d443c747f
7
+ data.tar.gz: 198f785e8486ebad307f5ca1f4c70d4743362e526bb9ff5978d65ad30a8878677f498f2c120317491319cf67183ffbffee0092432cfee65b359731e316e947dc
data/README.md CHANGED
@@ -29,6 +29,9 @@ OgPilotRuby.configure do |config|
29
29
  config.api_key = ENV.fetch("OG_PILOT_API_KEY")
30
30
  config.domain = ENV.fetch("OG_PILOT_DOMAIN")
31
31
  # config.strip_extensions = true
32
+ # config.image_type = "webp"
33
+ # config.quality = 82
34
+ # config.max_bytes = 220_000
32
35
  # config.cache_store = Rails.cache
33
36
  # config.cache_ttl = 86_400
34
37
  end
@@ -63,6 +66,19 @@ image_url = OgPilotRuby.create_image(
63
66
  If you omit `iat`, OG Pilot will cache the image indefinitely. Provide an `iat` to
64
67
  refresh the cache daily.
65
68
 
69
+ For the same delivery settings on every request, configure them once:
70
+
71
+ ```ruby
72
+ OgPilotRuby.configure do |config|
73
+ config.image_type = "webp"
74
+ config.quality = 82
75
+ config.max_bytes = 220_000
76
+ end
77
+ ```
78
+
79
+ Per-call `image_type`, `quality`, and `max_bytes` values still override those
80
+ defaults when you need a one-off delivery profile.
81
+
66
82
  When `config.cache_store` is set, the client can also cache generated responses
67
83
  locally:
68
84
 
@@ -553,6 +569,9 @@ The gem handles `iss` (domain) and `sub` (API key prefix) automatically.
553
569
  | `image_url` | No | — | Hero image URL |
554
570
  | `bg_color` | No | — | Background color (hex format) |
555
571
  | `text_color` | No | — | Text color (hex format) |
572
+ | `image_type` | No | — | Delivered image format: `jpeg`, `png`, `webp`, or `gif` |
573
+ | `quality` | No | — | Delivered image quality from `1` to `100` |
574
+ | `max_bytes` | No | — | Maximum delivered image size in bytes |
556
575
  | `iat` | No | — | Issued-at timestamp for daily cache busting |
557
576
  | `path` | No | auto-set | Request path for image rendering context. When provided, it overrides auto-resolution (see [Path handling](#path-handling)) |
558
577
 
@@ -567,6 +586,9 @@ The gem handles `iss` (domain) and `sub` (API key prefix) automatically.
567
586
  | `read_timeout` | `10` | Read timeout in seconds |
568
587
  | `strip_extensions` | `true` | When `true`, file extensions are stripped from resolved paths (see [Strip extensions](#strip-extensions)) |
569
588
  | `strip_query_parameters` | `false` | When `true`, query strings are removed from resolved paths before signing (see [Strip query parameters](#strip-query-parameters)) |
589
+ | `image_type` | `nil` | Default delivered image format: `jpeg`, `png`, `webp`, or `gif` |
590
+ | `quality` | `nil` | Default delivered image quality from `1` to `100` |
591
+ | `max_bytes` | `nil` | Default maximum delivered image size in bytes |
570
592
  | `cache_store` | `nil` | Optional cache backend with `read`/`write` (for example `Rails.cache`) |
571
593
  | `cache_ttl` | `86400` | Cache TTL in seconds when `cache_store` is enabled |
572
594
 
@@ -10,4 +10,7 @@ OgPilotRuby.configure do |config|
10
10
  # config.read_timeout = 10
11
11
  # config.strip_extensions = true
12
12
  # config.strip_query_parameters = true
13
+ # config.image_type = "webp"
14
+ # config.quality = 82
15
+ # config.max_bytes = 220_000
13
16
  end
@@ -20,7 +20,7 @@ module OgPilotRuby
20
20
 
21
21
  def create_image(params = {}, json: false, iat: nil, headers: {}, default: false)
22
22
  params ||= {}
23
- params = params.dup
23
+ params = apply_configured_image_defaults(params.dup)
24
24
  # Always include a path; manual overrides win, otherwise resolve from the current request.
25
25
  manual_path = params.key?(:path) ? params[:path] : params["path"]
26
26
  params.delete("path") if params.key?("path")
@@ -38,7 +38,8 @@ module OgPilotRuby
38
38
  result = if json
39
39
  JSON.parse(response.body)
40
40
  else
41
- response["Location"] || final_uri.to_s
41
+ url = response["Location"] || final_uri.to_s
42
+ status_placeholder?(url) ? nil : url
42
43
  end
43
44
 
44
45
  write_cached(cache_key, result, iat:) if config.cache_store && cache_key && result
@@ -102,6 +103,11 @@ module OgPilotRuby
102
103
  if response.is_a?(Net::HTTPRedirection)
103
104
  location = response["Location"]
104
105
  if location && !location.empty?
106
+ # Status placeholders (processing/failed) mean the image isn't ready yet.
107
+ # Return the redirect response as-is so the caller can detect this and
108
+ # avoid caching a temporary placeholder URL.
109
+ return [response, URI.join(uri.to_s, location)] if status_placeholder?(location)
110
+
105
111
  raise OgPilotRuby::RequestError, "OG Pilot request failed with too many redirects" if redirects_left <= 0
106
112
 
107
113
  redirect_uri = URI.join(uri.to_s, location)
@@ -131,6 +137,10 @@ module OgPilotRuby
131
137
  raise OgPilotRuby::RequestError, "OG Pilot request failed with unauthorized: #{e.message}"
132
138
  end
133
139
 
140
+ def status_placeholder?(url)
141
+ url.to_s.match?(%r{/status/(?:processing|failed)\.(?:jpg|png)\z})
142
+ end
143
+
134
144
  def build_http_request(method, uri)
135
145
  case method
136
146
  when :post
@@ -168,6 +178,21 @@ module OgPilotRuby
168
178
  symbolized
169
179
  end
170
180
 
181
+ def apply_configured_image_defaults(params)
182
+ {
183
+ image_type: config.image_type,
184
+ quality: config.quality,
185
+ max_bytes: config.max_bytes
186
+ }.each do |key, value|
187
+ next if value.nil?
188
+ next if params.key?(key) || params.key?(key.to_s)
189
+
190
+ params[key] = value
191
+ end
192
+
193
+ params
194
+ end
195
+
171
196
  def validate_payload!(payload)
172
197
  raise OgPilotRuby::ConfigurationError, "OG Pilot domain is missing" if payload[:iss].nil? || payload[:iss].empty?
173
198
  raise OgPilotRuby::ConfigurationError, "OG Pilot API key prefix is missing" if payload[:sub].nil? || payload[:sub].empty?
@@ -5,7 +5,9 @@ module OgPilotRuby
5
5
  DEFAULT_BASE_URL = "https://ogpilot.com"
6
6
  private_constant :DEFAULT_BASE_URL
7
7
 
8
- attr_accessor :api_key, :domain, :base_url, :open_timeout, :read_timeout, :strip_extensions, :strip_query_parameters, :cache_store, :cache_ttl
8
+ attr_accessor :api_key, :domain, :base_url, :open_timeout, :read_timeout,
9
+ :strip_extensions, :strip_query_parameters, :image_type,
10
+ :quality, :max_bytes, :cache_store, :cache_ttl
9
11
 
10
12
  def initialize
11
13
  @api_key = ENV.fetch("OG_PILOT_API_KEY", nil)
@@ -15,6 +17,9 @@ module OgPilotRuby
15
17
  @read_timeout = 10
16
18
  @strip_extensions = true
17
19
  @strip_query_parameters = false
20
+ @image_type = nil
21
+ @quality = nil
22
+ @max_bytes = nil
18
23
  @cache_store = nil
19
24
  @cache_ttl = 86400
20
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OgPilotRuby
4
- VERSION = "0.4.6"
4
+ VERSION = "0.4.8"
5
5
  end
data/lib/og_pilot_ruby.rb CHANGED
@@ -59,6 +59,9 @@ module OgPilotRuby
59
59
  # image_url - String hero image URL.
60
60
  # bg_color - String background color (hex format).
61
61
  # text_color - String text color (hex format).
62
+ # image_type - String delivered image format (jpeg/png/webp/gif).
63
+ # quality - Integer delivered image quality (1-100).
64
+ # max_bytes - Integer maximum delivered image size in bytes.
62
65
  # path - String request path for analytics context.
63
66
  #
64
67
  # == Request options
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: og_pilot_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sunergos IT LLC