recapi 0.1.12 → 0.1.13

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 +5 -2
  3. data/lib/recapi.rb +30 -6
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5494f67d90d8ce8686f9036fd05c9f1549a2af21a3af16734d82415bcf98fcde
4
- data.tar.gz: e41d02f51ea550051320c17a5d2c93917caa9b9e87ae8b9cf1e626f2ff4ff353
3
+ metadata.gz: 5769a64399a93b1e4984f9a1f77ead8d65081b81c8d2db8323bb0034e04563ec
4
+ data.tar.gz: f3209ebf643aa1573b067b4778573c23a971efa12c81a9284fededeb14640561
5
5
  SHA512:
6
- metadata.gz: bf77268b88e6b47414b83d527afab3a6766683d06d6793298b8a246a0d613b4a51fc8116ffc42ae78ff9e4af20ee28f0177cc7cb1fd60cad3e49b9ec488176c8
7
- data.tar.gz: a69a0f1965f86bfeb71b33696cf5dd945b74c3cdbacab24c2e80bd0009129edb54ac92dba2a197d2dad5f7752ff0159dc190afc504dd452398f919d8af1c2233
6
+ metadata.gz: 56250877312b500b42d4d19ad940a22fc111c84a3011cbdec3d0474875a858bf10c417b22be839bdb634dd23f6cdfa9b4ccc947bc5b7c507788803f5cb7aed97
7
+ data.tar.gz: 3bdc9a2cc2e4f5c4944649213bb266a47c875670f920e7a361995dd1d8f29306c4d1f01a68ad4640ae4d02c4e65c8402b81b8b15cf080a32361b819b6c3c8c0c
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.12.rb
33
+ curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.13.rb
34
34
  ```
35
35
 
36
36
  ## Quick start
@@ -78,7 +78,7 @@ and a log line.
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`. |
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. |
81
+ | `upsert_items(items, merge:)` | Creates or replaces items (hashes of `item_id`, `title`, `tags`, `metadata`…), 1,000 per request, retried like events. Returns how many were stored. With `merge: true` each item changes only the fields it sends and the others keep their stored values (a tags-only sync keeps the title and metadata); to clear a field, replace the item. A bad item doesn't keep the others out: once every request is sent, `Recapi::RejectedItems` (an `invalid_request` `Recapi::Error`) is raised with `upserted` and `rejected`, `[index in items, message]` pairs. |
82
82
  | `delete_user(user_id)` | Erases a user's events in recapi and their state in the store, retried like events. |
83
83
 
84
84
  `Recapi::Client.new(api_key, base_url, store:, timeout:, flush_interval:,
@@ -124,6 +124,9 @@ so a visitor who never returns stays on disk (about 8 KB each): run
124
124
 
125
125
  ## Changes
126
126
 
127
+ - 2026-11-14: version 0.1.13. `upsert_items(items, merge: true)` changes only the
128
+ fields sent. A bad item no longer fails its batch of 1,000: the others are stored,
129
+ and `Recapi::RejectedItems` lists the refused ones once every request is sent.
127
130
  - 2026-11-07: version 0.1.12. A saved history time ahead of the clock reads as now,
128
131
  and a damaged weight is sent as the API's limit (1e6). A time months ahead, or a
129
132
  weight past that limit, used to get a 400 on every list, and far enough past it
data/lib/recapi.rb CHANGED
@@ -25,7 +25,7 @@ rescue LoadError
25
25
  end
26
26
 
27
27
  module Recapi
28
- VERSION = "0.1.12"
28
+ VERSION = "0.1.13"
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.
@@ -313,6 +313,20 @@ module Recapi
313
313
  end
314
314
  end
315
315
 
316
+ # upsert_items' error when the API refused some items: the others were
317
+ # stored (status is the reply's, 200). rejected holds [index in the
318
+ # caller's list, message] pairs.
319
+ class RejectedItems < Error
320
+ attr_reader :upserted, :rejected
321
+
322
+ def initialize(upserted, rejected)
323
+ index, error = rejected.first
324
+ super("invalid_request", "#{rejected.size} items refused, the first items[#{index}]: #{error}", status: 200)
325
+ @upserted = upserted
326
+ @rejected = rejected
327
+ end
328
+ end
329
+
316
330
  # One recommended or popular item. metadata is the object stored with the
317
331
  # item (name, picture, link, price...), nil when it has none.
318
332
  #
@@ -688,15 +702,25 @@ module Recapi
688
702
 
689
703
  # Creates or replaces items (hashes of the API's item fields: item_id,
690
704
  # tags, metadata...), 1,000 per request. Retried like events. Returns how
691
- # many were stored; an invalid item raises, and nothing of its batch of
692
- # 1,000 is stored.
693
- def upsert_items(items)
705
+ # many were stored. merge: true changes only the fields each item sends and
706
+ # keeps the others' stored values. An invalid item doesn't keep the others
707
+ # out: once every request is sent, RejectedItems lists the refused ones.
708
+ def upsert_items(items, merge: false)
694
709
  upserted = 0
695
- items.each_slice(MAX_ITEMS_PER_REQUEST) do |batch|
710
+ rejected = []
711
+ items.each_slice(MAX_ITEMS_PER_REQUEST).with_index do |batch, n|
696
712
  body = { "items" => batch.map { |item| item.to_h.transform_keys(&:to_s) } }
713
+ body["merge"] = true if merge # only when asked: an older API refuses the field
697
714
  response = with_retry { request("POST", "/v1/items:batch", body: body) }
698
- upserted += response.is_a?(Hash) ? response.fetch("upserted", 0).to_i : 0
715
+ next unless response.is_a?(Hash)
716
+
717
+ upserted += response.fetch("upserted", 0).to_i
718
+ Array(response["rejected"]).each do |r|
719
+ rejected << [(n * MAX_ITEMS_PER_REQUEST) + r["index"].to_i, r["error"].to_s]
720
+ end
699
721
  end
722
+ raise RejectedItems.new(upserted, rejected) unless rejected.empty?
723
+
700
724
  upserted
701
725
  end
702
726
 
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.12
4
+ version: 0.1.13
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-07 00:00:00.000000000 Z
11
+ date: 2026-11-14 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.'