shopcircle-orbit 1.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc9150b18fb5fafd2bfffa30c16ab606e828e430e0a256f8bf7b26b99b6b9b24
4
- data.tar.gz: 5f80e5a8c9c68835749e856191903c165d8f4ce7dfaf8f6b2be037b935d348e1
3
+ metadata.gz: 13eadce37b88d6b83df93f3df4d4e79bb02c4109ef9200b8b6343c0a6f1404a1
4
+ data.tar.gz: eb467f5a94400e3beaa199c9ff4bcf167a15d1834f2f37da639f92c4dc50665e
5
5
  SHA512:
6
- metadata.gz: 29e8608f43338f1f5fceaf2b059c1cf9b70ce93368d609a5406dda26e418c3d207ee44043fd9b3d99c67e43b9b71bab4755714fa6dc58d0552a63507939da2b3
7
- data.tar.gz: d479a929d7d21e3759f09a0324014ba2aaf811ab9dceb5105e4841faca25322a767ee00c11f01bb249066c1d316f61520675819e1859ad9a2b6692485e65f213
6
+ metadata.gz: 47d718f705390d6a1f204fa3fdf4a826b8bcaac1b4fdb346b733e624d2612bb69e4d21758044b5f66a2867d907a6a8d4628104dab3bd01422291a3b36212101b
7
+ data.tar.gz: 410eadea7c619138940cf1ae5692d1d31061613f29bc737f4542e0ab67dffac09b31da111f465d3ac8c69fd69b93cacec7f24d673924e97a5b6228a9c20ca82c
@@ -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,17 @@ 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
+ # For public clients (no secret), fetch and attach an ingest token
82
+ if !@config.client_secret
83
+ token = fetch_ingest_token
84
+ request["shopcircle-ingest-token"] = token if token
85
+ end
86
+
75
87
  request.body = body
76
88
 
77
89
  response = @http.request(request)
@@ -120,6 +132,32 @@ module ShopCircle
120
132
  end
121
133
  end
122
134
 
135
+ # Fetch a short-lived ingest token for public clients.
136
+ # Tokens are cached and refreshed 60 seconds before expiry.
137
+ def fetch_ingest_token
138
+ now_ms = (Time.now.to_f * 1000).to_i
139
+ return @ingest_token if @ingest_token && now_ms < (@ingest_token_expires_at - 60_000)
140
+
141
+ ensure_connection
142
+ req = Net::HTTP::Post.new("/api/track/token")
143
+ req["Content-Type"] = "application/json"
144
+ req.body = JSON.generate({ clientId: @config.client_id })
145
+
146
+ resp = @http.request(req)
147
+ if resp.code.to_i == 200
148
+ data = JSON.parse(resp.body)
149
+ @ingest_token = data["token"]
150
+ @ingest_token_expires_at = data["expiresAt"].to_i
151
+ @ingest_token
152
+ else
153
+ log_error("Failed to fetch ingest token (#{resp.code})", resp.body)
154
+ nil
155
+ end
156
+ rescue StandardError => e
157
+ log_error("Ingest token fetch error", e.message)
158
+ nil
159
+ end
160
+
123
161
  # Exponential backoff with jitter.
124
162
  # Respects Retry-After header when present (429 responses).
125
163
  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.0"
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ShopCircle