recapi 0.1.20 → 0.1.21

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 +12 -4
  3. data/lib/recapi.rb +17 -4
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb0159dbbb16fbcdcbf3521d176be80a7f6b0ac32f597ae6be4e64cc72c5e0ff
4
- data.tar.gz: 4897a48ece87f2981a3ad2393f8564ca5d0090dea5a5eb4ab725d397fc562ce8
3
+ metadata.gz: d4447a3d6fed4e3427ffca2e1cecf110c84864d9538f857ec9d4037c42526ab7
4
+ data.tar.gz: e1f3af4bec5c24ef0d2239e4335a16428f15ce33ad0c817284e81a6d084d4898
5
5
  SHA512:
6
- metadata.gz: b1003bce4fb708f86872b87b1f12f9a19711c82b3d8ee24a72b436fd05f507b4d361eea01bead3fdbe9859aefbb28de2e795015cba7f5e46e7126f1fd69a9a53
7
- data.tar.gz: 3fe594aabd5ff99fa4eba82044a5df92fd1dbfb7e07785dcdf8ff4b1216055d610879c24231eb41a9d1b8d5d833e82526fe6e19f35e111f931ec3a3b177aba90
6
+ metadata.gz: d44b30bb0d223b5662fbdf50b3a2aef2f722b6ed4d325fcf07d739748ce5b3ca928c1000e83da3f9a7fac30219e4f1d3c58ef1d83f633ec5666c3eccd1b7066c
7
+ data.tar.gz: c0bb930ea7a3e06406b27fcda69a16ce0c117bf00494823bbe68717321658e81d4769b79b7b7bbf04cb1f838fc1d605fd2be1942a7564277a5783664def96489
data/README.md CHANGED
@@ -28,7 +28,7 @@ line with your address): save it as `lib/recapi.rb` and
28
28
  `require Rails.root.join("lib/recapi").to_s` instead.
29
29
 
30
30
  ```sh
31
- curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.20.rb
31
+ curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.21.rb
32
32
  ```
33
33
 
34
34
  ## Quick start
@@ -43,10 +43,16 @@ RECAPI = Recapi::Client.new(ENV.fetch("RECAPI_KEY"), "https://api.example.com",
43
43
  ```
44
44
 
45
45
  ```ruby
46
+ # Your products, 1,000 per request: a name and url show each in a list.
47
+ RECAPI.upsert_items([{ "item_id" => "sku-42", "tags" => ["shoes"], "metadata" => { "name" => "Trail shoe",
48
+ "url" => "https://shop.example/p/trail", "picture" => "https://shop.example/trail.jpg", "price" => 89, "currency" => "EUR" } }])
49
+
46
50
  # Queued, sent in the background. Levels: :view, :cart, :purchase, :reject or -10..10.
47
- RECAPI.track(product.id, :view, user_id: current_user&.id, anon_id: cookies[:visitor])
51
+ # In a controller. A visitor who is not logged in keeps one id in their session.
52
+ anon_id = session[:recapi_anon] ||= SecureRandom.hex(16)
53
+ RECAPI.track(product.id, :view, user_id: current_user&.id, anon_id: anon_id)
48
54
 
49
- recs = RECAPI.recommend(user_id: current_user.id, count: 4)
55
+ recs = RECAPI.recommend(user_id: current_user&.id, anon_id: anon_id, count: 4)
50
56
  recs.items.each do |item|
51
57
  item.item_id # a String, as every id recapi returns
52
58
  item.metadata # the item's stored object (a Hash), or nil
@@ -97,7 +103,7 @@ and a log line.
97
103
 
98
104
  | Call | What it does |
99
105
  |---|---|
100
- | `track(item_id, level, user_id:, anon_id:, recommendation_id:, value:, currency:)` | 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`. A purchase may carry `value:` (what the buyer paid for the line, an Integer or Float) and `currency:` (ISO 4217, such as `"EUR"`), kept for the sales figure of a lift test; a negative or non-finite value, one that is not a number, or a currency without a value or not of three letters, raises `ArgumentError`. |
106
+ | `track(item_id, level, user_id:, anon_id:, recommendation_id:, value:, currency:, context_item_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`. A purchase may carry `value:` (what the buyer paid for the line, an Integer or Float) and `currency:` (ISO 4217, such as `"EUR"`), kept for the sales figure of a lift test; a negative or non-finite value, one that is not a number, or a currency without a value or not of three letters, raises `ArgumentError`. `context_item_id:` names the product page a visitor with neither id (no cookie consent) came from: sent only on such an event, left out when it is the item itself or an id the API would refuse. |
101
107
  | `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). |
102
108
  | `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. |
103
109
  | `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. |
@@ -149,6 +155,8 @@ so a visitor who never returns stays on disk (about 8 KB each): run
149
155
 
150
156
  ## Changes
151
157
 
158
+ - 2026-12-04: version 0.1.21. `track` takes `context_item_id:`, the product page
159
+ a visitor without an id (no cookie consent) came from.
152
160
  - 2026-11-28: version 0.1.20. `Recapi::RECENTLY_VIEWED` and `Recapi::BOUGHT_TOGETHER`
153
161
  name the built-in placements, the home page's and the product page's second rows.
154
162
  - 2026-11-23: version 0.1.19. The comment on `Item#why` names the reason code
data/lib/recapi.rb CHANGED
@@ -25,7 +25,7 @@ rescue LoadError
25
25
  end
26
26
 
27
27
  module Recapi
28
- VERSION = "0.1.20"
28
+ VERSION = "0.1.21"
29
29
 
30
30
  # The built-in placements, which serve their own list with nothing to set
31
31
  # in the dashboard. Ask each beside the page's first row in one
@@ -561,8 +561,12 @@ module Recapi
561
561
  # would refuse raises nothing: a page often takes it from a visitor's URL, so
562
562
  # the event is dropped and logged instead. value is what was paid for a
563
563
  # purchase's line (nil for none, 0 for a free line), currency its ISO 4217
564
- # code; a currency needs a value.
565
- def track(item_id, level, user_id: nil, anon_id: nil, recommendation_id: nil, value: nil, currency: nil)
564
+ # code; a currency needs a value. context_item_id is the product page a
565
+ # visitor with neither id (no cookie consent) came from: sent only then, and
566
+ # left out when it is the item itself or an id the API would refuse, so the
567
+ # event still goes.
568
+ def track(item_id, level, user_id: nil, anon_id: nil, recommendation_id: nil, value: nil, currency: nil,
569
+ context_item_id: nil)
566
570
  level = level_value(level)
567
571
  user_id, anon_id, recommendation_id = check_ids(user_id: user_id, anon_id: anon_id,
568
572
  recommendation_id: recommendation_id)
@@ -595,7 +599,8 @@ module Recapi
595
599
  enqueue(compact(
596
600
  "item_id" => item_id, "level" => level, "user_id" => user_id, "anon_id" => anon_id,
597
601
  "recommendation_id" => attributed, "value" => value, "currency" => currency,
598
- "timestamp" => iso(t), "event_id" => SecureRandom.uuid
602
+ "timestamp" => iso(t), "event_id" => SecureRandom.uuid,
603
+ "context_item_id" => page_before(item_id, user_id, anon_id, context_item_id)
599
604
  ))
600
605
  nil
601
606
  end
@@ -1526,6 +1531,14 @@ module Recapi
1526
1531
  values.nil? || values.empty? ? nil : values.to_a
1527
1532
  end
1528
1533
 
1534
+ # A visitor with an id has a history that already says where they came
1535
+ # from; an id the API would refuse would cost the event itself (AP-1066).
1536
+ def page_before(item_id, user_id, anon_id, before)
1537
+ return nil unless user_id.nil? && anon_id.nil? && before.is_a?(String) && before != item_id
1538
+
1539
+ History.id?(before) ? before : nil
1540
+ end
1541
+
1529
1542
  def compact(fields)
1530
1543
  fields.compact
1531
1544
  end
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.20
4
+ version: 0.1.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - recapi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-12-02 00:00:00.000000000 Z
11
+ date: 2026-12-04 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.'