shopcircle-orbit 1.1.0 → 1.2.1

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: fc9150b18fb5fafd2bfffa30c16ab606e828e430e0a256f8bf7b26b99b6b9b24
4
- data.tar.gz: 5f80e5a8c9c68835749e856191903c165d8f4ce7dfaf8f6b2be037b935d348e1
3
+ metadata.gz: e3113ae248462ed96c7df563092cd89e421a4517c0ed6420c2abd5ab18ac9c34
4
+ data.tar.gz: f07313a22f1ab5c037f135b42576ee08db436c1e47c72fb4c35e96f33fed276b
5
5
  SHA512:
6
- metadata.gz: 29e8608f43338f1f5fceaf2b059c1cf9b70ce93368d609a5406dda26e418c3d207ee44043fd9b3d99c67e43b9b71bab4755714fa6dc58d0552a63507939da2b3
7
- data.tar.gz: d479a929d7d21e3759f09a0324014ba2aaf811ab9dceb5105e4841faca25322a767ee00c11f01bb249066c1d316f61520675819e1859ad9a2b6692485e65f213
6
+ metadata.gz: 6227c6ba47b0bd858fae94ce5a745b41e4b0277defe81cfd5c4b789401cc9e2316f1b9343051ec8fe0a5d88a7f588bcee91f80f7ced013260a4688c55fc6e002
7
+ data.tar.gz: 828d277ae8d56cf8742dd9648223dea7153917454175ddd07dae3465f3c24ac4f3e7a64222afe948db5d524a509ef012a11a9a83d9ec0697df05a190f58c4bb1
@@ -15,6 +15,8 @@ module ShopCircle
15
15
  @api_uri = URI.parse(config.api_url.to_s.chomp("/"))
16
16
  @http = nil
17
17
  @mutex = Mutex.new
18
+ @ingest_token = nil
19
+ @ingest_token_expires_at = 0
18
20
  end
19
21
 
20
22
  def send_single(event)
@@ -71,7 +73,16 @@ module ShopCircle
71
73
  request["shopcircle-client-id"] = @config.client_id
72
74
  request["shopcircle-sdk-name"] = SDK_NAME
73
75
  request["shopcircle-sdk-version"] = ShopCircle::Orbit::VERSION
74
- request["Authorization"] = "Bearer #{@config.client_secret}" if @config.client_secret
76
+
77
+ if @config.client_secret
78
+ request["shopcircle-client-secret"] = @config.client_secret
79
+ end
80
+
81
+ # Always attach an ingest token — required for public clients,
82
+ # accepted by secret-authenticated clients too.
83
+ token = fetch_ingest_token
84
+ request["shopcircle-ingest-token"] = token if token
85
+
75
86
  request.body = body
76
87
 
77
88
  response = @http.request(request)
@@ -120,6 +131,32 @@ module ShopCircle
120
131
  end
121
132
  end
122
133
 
134
+ # Fetch a short-lived ingest token for public clients.
135
+ # Tokens are cached and refreshed 60 seconds before expiry.
136
+ def fetch_ingest_token
137
+ now_ms = (Time.now.to_f * 1000).to_i
138
+ return @ingest_token if @ingest_token && now_ms < (@ingest_token_expires_at - 60_000)
139
+
140
+ ensure_connection
141
+ req = Net::HTTP::Post.new("/api/track/token")
142
+ req["Content-Type"] = "application/json"
143
+ req.body = JSON.generate({ clientId: @config.client_id })
144
+
145
+ resp = @http.request(req)
146
+ if resp.code.to_i == 200
147
+ data = JSON.parse(resp.body)
148
+ @ingest_token = data["token"]
149
+ @ingest_token_expires_at = data["expiresAt"].to_i
150
+ @ingest_token
151
+ else
152
+ log_error("Failed to fetch ingest token (#{resp.code})", resp.body)
153
+ nil
154
+ end
155
+ rescue StandardError => e
156
+ log_error("Ingest token fetch error", e.message)
157
+ nil
158
+ end
159
+
123
160
  # Exponential backoff with jitter.
124
161
  # Respects Retry-After header when present (429 responses).
125
162
  def retry_delay(attempt, response = nil)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ShopCircle
4
4
  module Orbit
5
- VERSION = "1.1.0"
5
+ VERSION = "1.2.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopcircle-orbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ShopCircle