ld-eventsource 2.3.0 → 2.4.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: 56eca88c610f3aa1f572426034422f66f343ce30553c9d2e281462c4f3efe831
4
- data.tar.gz: a9719f1e29695c0468c8b745239feb71041d5735e32f071fd77873866518bad5
3
+ metadata.gz: 5199039ee9ec2f9b9843d5bbb79dbc12ca2a625cc0d79cb99b97bb41de729a6c
4
+ data.tar.gz: 37b524d87e083d857ab4202749f6f2aac649cb0b31c04401ecfe4dfa68d082ca
5
5
  SHA512:
6
- metadata.gz: 2daad080ae032ab394dd4700e6bc22d20d167aca0e0a7ba9f74b41544ff0c5232cb0d34b6803fdc09342d95608732bea6c9ee7cef87bc912fef9a8ab10ccfb26
7
- data.tar.gz: 520224cf097791d2942ed0662dc29d94bff02c979fdebde479186efb21dbfa052aa80eee62779be723db2c918a9dd34a19ab4eea343453b7e114524a3dc23eb5
6
+ metadata.gz: 6d1243db76fb427d8c5bcaae4976ce2942fe10c3ec45d4846f0b2b0d57bcb8ceb7a629da61f5f5a1f219afe29f8c1e5b8039dfad44af9b405225773499c9b4fd
7
+ data.tar.gz: 3a01a8c90f08f4740e27faf2d0789750fb15076a1e1242e45eebf09219ae500848003d09eeaa53fa16a14009383bbb424690b78419a01956d4cd7bbebfca0673
@@ -164,6 +164,7 @@ module SSE
164
164
 
165
165
  @on = { event: ->(_) {}, error: ->(_) {} }
166
166
  @last_id = last_event_id
167
+ @query_params_callback = nil
167
168
 
168
169
  yield self if block_given?
169
170
 
@@ -206,6 +207,36 @@ module SSE
206
207
  @on[:error] = action
207
208
  end
208
209
 
210
+ #
211
+ # Specifies a block or Proc to generate query parameters dynamically. This will be called before
212
+ # each connection attempt (both initial connection and reconnections), allowing you to update
213
+ # query parameters based on the current client state.
214
+ #
215
+ # The block should return a Hash with string keys and string values, which will be merged with
216
+ # any existing query parameters in the base URI. If the callback raises an exception, it will be
217
+ # logged and the connection will proceed with the base URI's query parameters (or no query
218
+ # parameters if none were present).
219
+ #
220
+ # This is useful for scenarios where query parameters need to reflect the current state of the
221
+ # client, such as sending a "basis" parameter that represents what data the client already has.
222
+ #
223
+ # @example Using dynamic query parameters
224
+ # client = SSE::Client.new(base_uri) do |c|
225
+ # c.query_params do
226
+ # {
227
+ # "basis" => (selector.state if selector.defined?),
228
+ # "filter" => filter_key
229
+ # }.compact
230
+ # end
231
+ # c.on_event { |event| handle_event(event) }
232
+ # end
233
+ #
234
+ # @yieldreturn [Hash<String, String>] a hash of query parameter names to values
235
+ #
236
+ def query_params(&action)
237
+ @query_params_callback = action
238
+ end
239
+
209
240
  #
210
241
  # Permanently shuts down the client and its connection. No further events will be dispatched. This
211
242
  # has no effect if called a second time.
@@ -289,8 +320,9 @@ module SSE
289
320
  end
290
321
  cxn = nil
291
322
  begin
292
- @logger.info { "Connecting to event stream at #{@uri}" }
293
- cxn = @http_client.request(@method, @uri, build_opts)
323
+ uri = build_uri_with_query_params
324
+ @logger.info { "Connecting to event stream at #{uri}" }
325
+ cxn = @http_client.request(@method, uri, build_opts)
294
326
  if cxn.status.code == 200
295
327
  content_type = cxn.content_type.mime_type
296
328
  if content_type && content_type.start_with?("text/event-stream")
@@ -397,5 +429,27 @@ module SSE
397
429
  {headers: build_headers, body: resolved_payload.to_s}
398
430
  end
399
431
  end
432
+
433
+ def build_uri_with_query_params
434
+ uri = @uri.dup
435
+
436
+ if @query_params_callback
437
+ begin
438
+ dynamic_params = @query_params_callback.call
439
+ if dynamic_params.is_a?(Hash) && !dynamic_params.empty?
440
+ existing_params = uri.query ? URI.decode_www_form(uri.query).to_h : {}
441
+ merged_params = existing_params.merge(dynamic_params)
442
+ uri.query = URI.encode_www_form(merged_params)
443
+ elsif !dynamic_params.is_a?(Hash)
444
+ @logger.warn { "query_params callback returned non-Hash value: #{dynamic_params.class}, ignoring" }
445
+ end
446
+ rescue StandardError => e
447
+ @logger.warn { "query_params callback raised an exception: #{e.inspect}, proceeding with base URI" }
448
+ @logger.debug { "Exception trace: #{e.backtrace}" }
449
+ end
450
+ end
451
+
452
+ uri
453
+ end
400
454
  end
401
455
  end
@@ -1,3 +1,3 @@
1
1
  module SSE
2
- VERSION = "2.3.0" # x-release-please-version
2
+ VERSION = "2.4.0" # x-release-please-version
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ld-eventsource
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LaunchDarkly
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-09-04 00:00:00.000000000 Z
11
+ date: 2026-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logger