og_pilot_ruby 0.4.3 → 0.4.4

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: 9e2a983c2b0ced955a1d89e715316050384d6879ec31e328b391e1b7d6e788e9
4
- data.tar.gz: a2e254954c6f260f493f10e86fd19b8a7d2a64781f9a970ad730bd51fc411962
3
+ metadata.gz: 367bb4f8ea0c4a82c261f4403a42ae5bbf2afa25069bad87250c884ca343526b
4
+ data.tar.gz: 753f95ace14cc5d6b1895a286174810457837ba9e7b0fdb2dcb646bfa9f8ea78
5
5
  SHA512:
6
- metadata.gz: 8fe9e289d37a64f41c9d578aba09477ce5e0fa9ea29a422ded261b9ab01b45c02cb7aff88581ed446bd2e5aafeb94c9f49b5762e374bfe6b63d6063985ec462b
7
- data.tar.gz: 96d7f35d05dae228f048bae8e7ee55a046fc4480f2546b5153c3bf72763ccd7116efa41d59c6b9f71b9d651cd0d17aca71636dfaba6a62123d8a71a2c8257ef3
6
+ metadata.gz: ec298c3d0d781c259aece2d385f1f53a0e26633295b0bc8b1624ab85123a6cb5840a0b4f81a4e303940c425b618d270b8f504e19cb7654466046c11dbbda666b
7
+ data.tar.gz: 29008c57e899486428e21c2395e0f3cdfc2010d1bc533cd93102e0ba17a41cabc96c94295bf4b3a46247ebe8a6ff5e183a7981e6c8d540406cbea3e22e29eb9f
data/README.md CHANGED
@@ -29,6 +29,8 @@ 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.cache_store = Rails.cache
33
+ # config.cache_ttl = 86_400
32
34
  end
33
35
  ```
34
36
 
@@ -61,6 +63,12 @@ image_url = OgPilotRuby.create_image(
61
63
  If you omit `iat`, OG Pilot will cache the image indefinitely. Provide an `iat` to
62
64
  refresh the cache daily.
63
65
 
66
+ When `config.cache_store` is set, the client can also cache generated responses
67
+ locally:
68
+
69
+ - With `iat`: cache for `cache_ttl` seconds
70
+ - Without `iat`: cache for `7 * cache_ttl` seconds
71
+
64
72
  ### Fail-safe behavior
65
73
 
66
74
  `create_image` is non-blocking. If any error occurs (request, configuration,
@@ -558,6 +566,8 @@ The gem handles `iss` (domain) and `sub` (API key prefix) automatically.
558
566
  | `open_timeout` | `5` | Connection timeout in seconds |
559
567
  | `read_timeout` | `10` | Read timeout in seconds |
560
568
  | `strip_extensions` | `true` | When `true`, file extensions are stripped from resolved paths (see [Strip extensions](#strip-extensions)) |
569
+ | `cache_store` | `nil` | Optional cache backend with `read`/`write` (for example `Rails.cache`) |
570
+ | `cache_ttl` | `86400` | Cache TTL in seconds when `cache_store` is enabled |
561
571
 
562
572
  ### Ruby options
563
573
 
@@ -624,6 +634,26 @@ payload = {
624
634
  data = OgPilotRuby.create_image(**payload, json: true)
625
635
  ```
626
636
 
637
+ ### Local caching
638
+
639
+ You can optionally enable client-side caching to avoid repeated API calls for
640
+ identical payloads:
641
+
642
+ ```ruby
643
+ OgPilotRuby.configure do |config|
644
+ config.cache_store = Rails.cache
645
+ config.cache_ttl = 86_400 # 1 day
646
+ end
647
+ ```
648
+
649
+ Cache key inputs include the normalized payload, `iat`, output mode (`json`
650
+ vs URL), and configured domain.
651
+
652
+ TTL behavior:
653
+
654
+ - If `iat` is present: cached for `cache_ttl`
655
+ - If `iat` is omitted: cached for `7 * cache_ttl`
656
+
627
657
  ### Strip extensions
628
658
 
629
659
  When `strip_extensions` is enabled, the client removes file extensions from the
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "digest"
3
4
  require "json"
4
5
  require "logger"
5
6
  require "net/http"
@@ -25,14 +26,24 @@ module OgPilotRuby
25
26
  params.delete("path") if params.key?("path")
26
27
  params[:path] = manual_path.to_s.strip.empty? ? resolved_path(default:) : normalize_path(manual_path)
27
28
 
29
+ cache_key = cache_key_for(params, iat:, json:)
30
+ if config.cache_store && cache_key
31
+ cached = read_cached(cache_key)
32
+ return cached if cached
33
+ end
34
+
28
35
  uri = build_uri(params, iat:)
29
36
  response, final_uri = request(uri, json:, headers:)
30
37
 
31
- if json
38
+ result = if json
32
39
  JSON.parse(response.body)
33
40
  else
34
41
  response["Location"] || final_uri.to_s
35
42
  end
43
+
44
+ write_cached(cache_key, result, iat:) if config.cache_store && cache_key && result
45
+
46
+ result
36
47
  rescue StandardError => e
37
48
  log_create_image_failure(e, json:)
38
49
  json ? { "image_url" => nil } : nil
@@ -42,6 +53,25 @@ module OgPilotRuby
42
53
 
43
54
  attr_reader :config
44
55
 
56
+ def cache_key_for(params, iat:, json:)
57
+ key_data = params.transform_keys(&:to_sym).merge(iat: iat, json: json, domain: domain!)
58
+ normalized = key_data.sort_by { |k, _| k.to_s }.to_h
59
+ "og_pilot:#{Digest::SHA256.hexdigest(normalized.to_json)[0, 16]}"
60
+ end
61
+
62
+ def read_cached(cache_key)
63
+ config.cache_store.read(cache_key)
64
+ rescue StandardError
65
+ nil
66
+ end
67
+
68
+ def write_cached(cache_key, result, iat:)
69
+ ttl = iat ? config.cache_ttl : (7 * config.cache_ttl)
70
+ config.cache_store.write(cache_key, result, expires_in: ttl)
71
+ rescue StandardError
72
+ nil
73
+ end
74
+
45
75
  def log_create_image_failure(error, json:)
46
76
  mode = json ? "json" : "url"
47
77
  message = "OgPilotRuby create_image failed (mode=#{mode}): #{error.class}: #{error.message}"
@@ -5,7 +5,7 @@ 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
8
+ attr_accessor :api_key, :domain, :base_url, :open_timeout, :read_timeout, :strip_extensions, :cache_store, :cache_ttl
9
9
 
10
10
  def initialize
11
11
  @api_key = ENV.fetch("OG_PILOT_API_KEY", nil)
@@ -14,6 +14,8 @@ module OgPilotRuby
14
14
  @open_timeout = 5
15
15
  @read_timeout = 10
16
16
  @strip_extensions = true
17
+ @cache_store = nil
18
+ @cache_ttl = 86400
17
19
  end
18
20
  end
19
21
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OgPilotRuby
4
- VERSION = "0.4.3"
4
+ VERSION = "0.4.4"
5
5
  end
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.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sunergos IT LLC