recapi 0.1.8 → 0.1.10

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 +18 -8
  3. data/lib/recapi.rb +33 -10
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45a27aa9c5eae2fa2088d58247ebd703b60df834bc22e508737d89f17e90a8a2
4
- data.tar.gz: 7443f75901c5e2600ea181f3a61277bc9b2e5b2820aad2c999dba4b8353521b6
3
+ metadata.gz: e9d8c7e28e6929bfef64625ac5ba48ba5bec683d27b3d4c91646b67f189cadeb
4
+ data.tar.gz: b17b8ad7945fac43147ce43aca7cd27a1b62105a155fdc5ce15d8eccc7e6fca9
5
5
  SHA512:
6
- metadata.gz: 856de23aa6d4936301ac4d9a2dbd2de72c4ffea40e44ed41459f7b310ff36dc232103f9e8e6cd11d265790e6b95eda80cea2d6c40087d68df252d2861c5f2956
7
- data.tar.gz: dfd97019465b529c84fb4d315876acac76a414432fe3d53a901ec037f3b03c5490c2ce76ed2f3000651ff904f37115c9da9d046c59c99286924abcfea1ded8fc
6
+ metadata.gz: 01a65170536dc4f5f33596f509fc7102670ba72a566954b1443d7d4dc29f01137206c4a3b3c49822c4bf85bc6b3200751655a49480a2afa46fe0e10ba3fa45e5
7
+ data.tar.gz: d6d63d879a0eed194f410983abef9770b3296606184cf822a4824c56c2a0dd5a6350922595af45257c9ddf3bb3905be35de694ba9f230a9cc2d5d7a7b6676e7e
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.8.rb
33
+ curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.10.rb
34
34
  ```
35
35
 
36
36
  ## Quick start
@@ -62,8 +62,9 @@ Ids may be strings or integers (an ActiveRecord id): they travel as strings.
62
62
 
63
63
  ## Calls
64
64
 
65
- A refused argument (an invalid id or level) raises `ArgumentError`, and an
66
- API failure `Recapi::Error` (`code`, `status`, `retry_after`, `retryable?`,
65
+ A refused argument (an invalid id or level) raises `ArgumentError`, except an
66
+ `item_id` or `context_item_id` the API would refuse, as a page often takes those
67
+ from a visitor's URL (see the table). An API failure is a `Recapi::Error` (`code`, `status`, `retry_after`, `retryable?`,
67
68
  `outage?`). `recommend` and `popular` still raise on a bad request (400, 404,
68
69
  413, other 3xx/4xx) or a reply that isn't the API's; an outage, a limit or a
69
70
  refused key can start on a live site with no deploy, so they answer with a list
@@ -71,10 +72,10 @@ and a log line.
71
72
 
72
73
  | Call | What it does |
73
74
  |---|---|
74
- | `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). |
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`. |
75
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). |
76
- | `close` | Stops the sender and sends what is queued, one try per batch. It runs by itself at exit. |
77
- | `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. Any other failure (a bad request, a reply that isn't the API's) raises. |
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
+ | `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. |
78
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. |
79
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`. |
80
81
  | `upsert_items(items)` | Creates or replaces items (hashes of `item_id`, `title`, `tags`, `metadata`…), 1,000 per request, retried like events. Returns how many were stored. |
@@ -97,8 +98,8 @@ queue and sender at its first call. Proxies come from `HTTPS_PROXY`,
97
98
  `HTTP_PROXY` and `NO_PROXY`, as `Net::HTTP` reads them. Failures are logged
98
99
  to `logger:`, anything with `warn` and `error` (`Rails.logger`, a
99
100
  `Logger`); without one they go to `$stderr` as `recapi: …` lines, with lost
100
- events (a refused batch, rejected events) at most one line a minute for each
101
- error code and for rejected events, and `recommend`'s and `popular`'s failures
101
+ events (a refused batch, rejected events, a dropped `item_id`) at most one line a
102
+ minute for each error code, for rejected events and for dropped ids, and `recommend`'s and `popular`'s failures
102
103
  at most one line a minute for each error code, so a revoked key on a busy server
103
104
  doesn't flood the log, and a full queue one line until a send works. A failed write to
104
105
  `$stderr` (a full disk, a closed stream) is ignored. A logger you give gets
@@ -121,6 +122,15 @@ and logs one line at start:
121
122
  so a visitor who never returns stays on disk (about 8 KB each): run
122
123
  `Rails.cache.cleanup` on a schedule, or leave `store:` out.
123
124
 
125
+ ## Changes
126
+
127
+ - 2026-11-05: version 0.1.10. `track` stays queued and in the background in a
128
+ classic Sinatra app
129
+ (`ruby app.rb`). The exit hook used to close the client before the server
130
+ started, so every event was sent on the page's thread, and a hung API
131
+ stalled each page for 5 s. The hook now starts with the sender, at the first
132
+ `track`.
133
+
124
134
  ## Development
125
135
 
126
136
  `../../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.8"
28
+ VERSION = "0.1.10"
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.
@@ -433,8 +433,8 @@ module Recapi
433
433
  TIMEOUT_ERRORS = [Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout].freeze
434
434
  # A connection the server closed while idle fails this way, before any reply.
435
435
  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.
436
+ # Made here, not in enqueue, whose block would hold the client through self:
437
+ # the weak reference lets a dropped client be collected.
438
438
  CLOSE_AT_EXIT = lambda do |ref|
439
439
  at_exit do
440
440
  ref.__getobj__.close
@@ -480,21 +480,28 @@ module Recapi
480
480
  @sleeper = method(:wait)
481
481
  @random = -> { rand }
482
482
  init_threading
483
- CLOSE_AT_EXIT.call(WeakRef.new(self))
484
483
  end
485
484
 
486
485
  # Records an interaction: the user's history changes at once, the event
487
486
  # is queued. level is an integer -10 to 10 or :view (3), :cart (5),
488
487
  # :purchase (10), :reject (-10: never recommend the item to this user
489
488
  # again until a positive event on it). recommendation_id overrides the
490
- # automatic attribution. Ids may be strings or integers.
489
+ # automatic attribution. Ids may be strings or integers. An item_id the API
490
+ # would refuse raises nothing: a page often takes it from a visitor's URL, so
491
+ # the event is dropped and logged instead.
491
492
  def track(item_id, level, user_id: nil, anon_id: nil, recommendation_id: nil)
492
493
  level = level_value(level)
493
- item_id = id_string(item_id)
494
- raise ArgumentError, "item_id #{ID_RULE}" unless History.id?(item_id)
495
-
496
494
  user_id, anon_id, recommendation_id = check_ids(user_id: user_id, anon_id: anon_id,
497
495
  recommendation_id: recommendation_id)
496
+ item_id = id_string(item_id)
497
+ # A crafted link must not fail the page. Nothing is kept: in the history it
498
+ # would make the API refuse every later list. The line never quotes the
499
+ # visitor's id.
500
+ unless History.id?(item_id)
501
+ log_lost(:item_id, "recapi: event dropped: item_id #{ID_RULE}")
502
+ return nil
503
+ end
504
+
498
505
  t = now_ms
499
506
  attributed = recommendation_id
500
507
  key = key_for(user_id, anon_id)
@@ -554,8 +561,11 @@ module Recapi
554
561
  # no id) and logs the error. If those fail too (recapi is down, or the plan's
555
562
  # limit covers them), the list is empty: hide the slot. On a 401 or 403 (a
556
563
  # revoked key, a suspended shop) it is empty at once, as popular would be
557
- # refused too. Any other failure raises: a bad request or a reply that isn't
558
- # the API's can't start on a live site without a deploy.
564
+ # refused too. A context_item_id the API would refuse (a page often takes it
565
+ # from a visitor's URL: Rails' params[:id] can be an Array or a Hash) is never
566
+ # sent: the answer is the same popular list. Any other failure raises: a bad
567
+ # request or a reply that isn't the API's can't start on a live site without
568
+ # a deploy.
559
569
  def recommend(user_id: nil, anon_id: nil, count: 0, placement: nil, tags_any: nil, tags_all: nil,
560
570
  exclude: nil, context_item_id: nil, discovery: nil)
561
571
  deadline = monotonic + recommend_timeout
@@ -565,6 +575,13 @@ module Recapi
565
575
  key = key_for(user_id, anon_id)
566
576
  t = now_ms
567
577
  state = key.nil? ? UserState.new : load(key)
578
+ unless context_item_id.nil? || context_item_id == "" || History.id?(context_item_id)
579
+ _, _, rejected = state.history.wire(t)
580
+ # The message never quotes the id: a visitor may have written it. nil:
581
+ # not skipped in the list either, as no item can have this id.
582
+ cause = Error.new("invalid_request", "context_item_id #{ID_RULE}")
583
+ return fallback(cause, count, with_rejected(exclude, rejected), nil, tags_any, tags_all)
584
+ end
568
585
  if user_id && !state.restored && !state.history.empty? && !@api_key.start_with?(PUBLIC_KEY_PREFIX)
569
586
  state = restore(key, user_id, state, deadline, t)
570
587
  end
@@ -799,6 +816,12 @@ module Recapi
799
816
  @thread = Thread.new { run }
800
817
  @thread.name = "recapi-sender"
801
818
  @thread.report_on_exception = false
819
+ # Here, not in initialize: Sinatra's classic mode serves from its own
820
+ # at_exit, and exit hooks run last registered first, so a hook from
821
+ # the app's load would close the client before the first request.
822
+ # One registered while that server runs goes after the server ends.
823
+ # A forked worker starts its own sender, so it gets its own hook too.
824
+ CLOSE_AT_EXIT.call(WeakRef.new(self))
802
825
  end
803
826
  @queue << event
804
827
  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.8
4
+ version: 0.1.10
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-04 00:00:00.000000000 Z
11
+ date: 2026-11-05 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.'