recapi 0.1.9 → 0.1.11

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +15 -2
  3. data/lib/recapi.rb +20 -6
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28f52d6426fd83d0b14d106535a06fca9e6d29ac56b4a3b366ae31f8ca7e22df
4
- data.tar.gz: 93361e6a7d6dcbbc86be19b92ccc7fb4bc3b3695f99fa701ad08cfea17ccb57f
3
+ metadata.gz: ddfbe7da7e2ca36f680b013e06c795a516645d415329f1dc61892024e129d477
4
+ data.tar.gz: dacd7f80f58ecda60c653380d73e63a396315f906339d9940941922a1ed6333e
5
5
  SHA512:
6
- metadata.gz: 54a7bf72a2b956ed847fe57af69c3241af18c638a199a86aa3220b2f0726d495402a3faa4698e011f815f57881e5969090bf9047b28b60bc0b6d11767f88eacf
7
- data.tar.gz: a7825355e8be66451b687bc342a8c9961f8b7f8bc4c04fb7c57a38442ab59ed12daf67be7b04a39cba4b7a76843c5faec071e35dbcc15a5b3a1aba6f5154ae4b
6
+ metadata.gz: f5309158f3114f5d2d314045358364a9467a867aa4324e8650b6dc6269583de306c74e0f5a288f81cbfdf27adc75ca778a0f8d7d12a63710ac1c7c98158d4cfc
7
+ data.tar.gz: 82124ece99593007209d5a4e040c7d1f6768d47ae6ee93a0892f734d83c964b5d10e34d7198badb53453cab39be36ee1bb2bf18d43ac802f62806ae8e525c780
data/README.md CHANGED
@@ -30,7 +30,7 @@ line with your address): save it as `lib/recapi.rb` and
30
30
  `require Rails.root.join("lib/recapi").to_s` instead.
31
31
 
32
32
  ```sh
33
- curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.9.rb
33
+ curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.11.rb
34
34
  ```
35
35
 
36
36
  ## Quick start
@@ -74,7 +74,7 @@ and a log line.
74
74
  |---|---|
75
75
  | `track(item_id, level, user_id:, anon_id:, recommendation_id:)` | Records an interaction: `level` -10 to 10 or `:view` (3), `:cart` (5), `:purchase` (10), `:reject` (-10: never recommend the item to this user again until a positive event on it). The history changes at once; a background thread sends the event within 2 s (at once when a batch of 1,000 is full). An `item_id` the API would refuse (1-256 bytes of UTF-8, no control character; also `nil`, an Array or a Hash) drops the event with a log line: nothing raises, and the other events are sent as usual. A bad `level`, `user_id`, `anon_id` or `recommendation_id` still raises `ArgumentError`. |
76
76
  | `flush` | Sends the queued events now, with retries (end of a job). A network error, 429 or 5xx is retried 3 times (0.5 s, 1 s, 2 s with jitter, or the `Retry-After`, at most 8 s), then the events stay queued and the sender backs off up to a minute; any other error drops them (logged). |
77
- | `close` | Stops the sender and sends what is queued, one try per batch. It runs by itself at exit. |
77
+ | `close` | Stops the sender and sends what is queued, one try per batch. It runs by itself at exit, also for a classic Sinatra app (`ruby app.rb`), whose server runs from its own exit hook. Don't `track` while such an app loads, before its server starts: the client then closes before the first request, and every later `track` is sent on the page's thread. |
78
78
  | `recommend(user_id:, anon_id:, count:, placement:, tags_any:, tags_all:, exclude:, context_item_id:, discovery:)` | A `Recapi::Recommendations` (`recommendation_id`, `fallback?`, `items`). At most 1.5 s; on a network error, timeout, 5xx or 429 it returns popular items (`fallback?`, no id), or no item when those fail too: hide the slot; on a 401 or 403 no item at once. A `context_item_id` the API would refuse (also an Array or a Hash, as `params[:id]` gives for `?id[]=a`) isn't sent: you get the same popular list and a log line, with no recommendation request. Any other failure (a bad request, a reply that isn't the API's) raises. |
79
79
  | `identify(anon_id, user_id)` | At login: the visitor's history and shown items become the user's. The user's first restore from recapi then leaves that visitor's browsing out, so it doesn't count twice. |
80
80
  | `popular(count:, tags_any:, tags_all:)` | Popular items, for pages without a user. On a network error, timeout, 5xx, 429, 401 or 403: an empty list and a log line. Other failures as in `recommend`. |
@@ -122,6 +122,19 @@ and logs one line at start:
122
122
  so a visitor who never returns stays on disk (about 8 KB each): run
123
123
  `Rails.cache.cleanup` on a schedule, or leave `store:` out.
124
124
 
125
+ ## Changes
126
+
127
+ - 2026-11-07: version 0.1.11. A damaged stored state can't raise into your page
128
+ or stop a visitor's history from saving. A shown entry of the wrong types
129
+ costs that item's click credit, and a history time too big for a number
130
+ discards the state. Same in every library.
131
+ - 2026-11-05: version 0.1.10. `track` stays queued and in the background in a
132
+ classic Sinatra app
133
+ (`ruby app.rb`). The exit hook used to close the client before the server
134
+ started, so every event was sent on the page's thread, and a hung API
135
+ stalled each page for 5 s. The hook now starts with the sender, at the first
136
+ `track`.
137
+
125
138
  ## Development
126
139
 
127
140
  `../../scripts/ruby.sh -w test/run.rb [filter]` runs the tests (`make
data/lib/recapi.rb CHANGED
@@ -25,7 +25,7 @@ rescue LoadError
25
25
  end
26
26
 
27
27
  module Recapi
28
- VERSION = "0.1.9"
28
+ VERSION = "0.1.11"
29
29
 
30
30
  # Decayed user history, exactly as docs/history-spec.md (times in unix ms).
31
31
  # A port of sdk/python's history.py, snapshot merging included.
@@ -51,8 +51,10 @@ module Recapi
51
51
  Math.exp(-RATE * (dt_ms / 1000.0))
52
52
  end
53
53
 
54
+ # Compared, not converted: a stored 400-digit integer would become Infinity,
55
+ # which JSON can't save. NaN and Infinity fail the bound.
54
56
  def self.time?(value)
55
- (value.is_a?(Integer) || value.is_a?(Float)) && value.finite?
57
+ (value.is_a?(Integer) || value.is_a?(Float)) && value.abs <= Float::MAX
56
58
  end
57
59
 
58
60
  def self.weight?(value)
@@ -433,8 +435,8 @@ module Recapi
433
435
  TIMEOUT_ERRORS = [Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout].freeze
434
436
  # A connection the server closed while idle fails this way, before any reply.
435
437
  STALE_ERRORS = [EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED].freeze
436
- # Made here, not in initialize, whose block would hold the client through
437
- # self: the weak reference lets a dropped client be collected.
438
+ # Made here, not in enqueue, whose block would hold the client through self:
439
+ # the weak reference lets a dropped client be collected.
438
440
  CLOSE_AT_EXIT = lambda do |ref|
439
441
  at_exit do
440
442
  ref.__getobj__.close
@@ -480,7 +482,6 @@ module Recapi
480
482
  @sleeper = method(:wait)
481
483
  @random = -> { rand }
482
484
  init_threading
483
- CLOSE_AT_EXIT.call(WeakRef.new(self))
484
485
  end
485
486
 
486
487
  # Records an interaction: the user's history changes at once, the event
@@ -740,13 +741,20 @@ module Recapi
740
741
 
741
742
  shown = {}
742
743
  data["shown"].each do |e|
743
- shown[e[0]] = e[1..] if e.is_a?(Array) && e.size.between?(3, 4) && History.id?(e[0])
744
+ shown[e[0]] = e[1..] if shown_entry?(e)
744
745
  end
745
746
  anon_id = data["anon_id"]
746
747
  new(history, shown, data["restored"] == true, History.id?(anon_id) ? anon_id : nil)
747
748
  rescue JSON::ParserError, TypeError
748
749
  nil
749
750
  end
751
+
752
+ # Bad types here would raise much later, in track on the shop's page, or
753
+ # refuse a whole batch of events at the API.
754
+ def self.shown_entry?(entry)
755
+ entry.is_a?(Array) && entry.size.between?(3, 4) && History.id?(entry[0]) && entry[1].is_a?(String) &&
756
+ entry[2..].all? { |t| t.is_a?(Integer) && t.abs < 2**63 }
757
+ end
750
758
  end
751
759
 
752
760
  def self.attributable?(entry, t)
@@ -817,6 +825,12 @@ module Recapi
817
825
  @thread = Thread.new { run }
818
826
  @thread.name = "recapi-sender"
819
827
  @thread.report_on_exception = false
828
+ # Here, not in initialize: Sinatra's classic mode serves from its own
829
+ # at_exit, and exit hooks run last registered first, so a hook from
830
+ # the app's load would close the client before the first request.
831
+ # One registered while that server runs goes after the server ends.
832
+ # A forked worker starts its own sender, so it gets its own hook too.
833
+ CLOSE_AT_EXIT.call(WeakRef.new(self))
820
834
  end
821
835
  @queue << event
822
836
  drop_overflow if @queue.size > MAX_QUEUE
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - recapi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-05 00:00:00.000000000 Z
11
+ date: 2026-11-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Ruby client for the recapi recommendation API: per-user history, event
14
14
  batching, retries, popular fallback. Standard library only.'