recapi 0.1.16 → 0.1.17

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 +4 -2
  3. data/lib/recapi.rb +22 -4
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4630dc6459bcf8c5f8563c8d2725197ef77adea276bc85524363938c20ab8629
4
- data.tar.gz: 6a58b1472dd36ba98c5757b6c5a1a4c297e9417b9fc23e0bbcb125a71f633dd7
3
+ metadata.gz: 2a56e546f32406629e6623fa3f0617bde146a041b39c744732de8012e64f518e
4
+ data.tar.gz: ed62a0ed257ed7c3c591ec0a669e1cff3cf23b3ae1b4fc90da057396d68c92e1
5
5
  SHA512:
6
- metadata.gz: 37f40070a0df5413ad196d63396be3aa4a3fad991e23daae7e5dbdb095dc803e278e6065c2047afd20466f3480be0e0904f7898903f1b1e5568a08bf43dc8ecb
7
- data.tar.gz: e40ee11a50b6aa926ac3ed147de9676b8a185914cdddb228345ebb0088f33255a652599972405a2ecf7449fb9792ae22bd5d62cff6fb1354b6533000702113cb
6
+ metadata.gz: 313198beae9821f6d6aa9e074d51d3c421e9b90f7b40fb9f272c0971fa1af44318104d51b70fd93438013c2a387466927753de45cfeb2366e21a520c85271ced
7
+ data.tar.gz: 77c0f6a8c4f81ea312b6f860f9af9299c21bdc08592ea3daa755b995884dd68f6d7ad04b7a8affee783041f11f43d232527539030bafdc085a2cebf679fc589e
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.16.rb
31
+ curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.17.rb
32
32
  ```
33
33
 
34
34
  ## Quick start
@@ -86,7 +86,7 @@ and a log line.
86
86
 
87
87
  | Call | What it does |
88
88
  |---|---|
89
- | `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`. |
89
+ | `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`. |
90
90
  | `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). |
91
91
  | `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. |
92
92
  | `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. |
@@ -138,6 +138,8 @@ so a visitor who never returns stays on disk (about 8 KB each): run
138
138
 
139
139
  ## Changes
140
140
 
141
+ - 2026-11-22: version 0.1.17. `track` takes `value:` and `currency:`, what the
142
+ buyer paid for a purchase's line, for the sales figure of a lift test.
141
143
  - 2026-11-19: version 0.1.16. `Recapi::Error#docs_url` is the errors page's
142
144
  section for an API error, and its message and the library's log lines end with it.
143
145
  - 2026-11-19: version 0.1.15. This page's links lead to recapi.io's public docs,
data/lib/recapi.rb CHANGED
@@ -25,7 +25,7 @@ rescue LoadError
25
25
  end
26
26
 
27
27
  module Recapi
28
- VERSION = "0.1.16"
28
+ VERSION = "0.1.17"
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.
@@ -518,11 +518,14 @@ module Recapi
518
518
  # again until a positive event on it). recommendation_id overrides the
519
519
  # automatic attribution. Ids may be strings or integers. An item_id the API
520
520
  # would refuse raises nothing: a page often takes it from a visitor's URL, so
521
- # the event is dropped and logged instead.
522
- def track(item_id, level, user_id: nil, anon_id: nil, recommendation_id: nil)
521
+ # the event is dropped and logged instead. value is what was paid for a
522
+ # purchase's line (nil for none, 0 for a free line), currency its ISO 4217
523
+ # code; a currency needs a value.
524
+ def track(item_id, level, user_id: nil, anon_id: nil, recommendation_id: nil, value: nil, currency: nil)
523
525
  level = level_value(level)
524
526
  user_id, anon_id, recommendation_id = check_ids(user_id: user_id, anon_id: anon_id,
525
527
  recommendation_id: recommendation_id)
528
+ check_amount(value, currency)
526
529
  item_id = id_string(item_id)
527
530
  # A crafted link must not fail the page. Nothing is kept: in the history it
528
531
  # would make the API refuse every later list. The line never quotes the
@@ -550,7 +553,8 @@ module Recapi
550
553
  end
551
554
  enqueue(compact(
552
555
  "item_id" => item_id, "level" => level, "user_id" => user_id, "anon_id" => anon_id,
553
- "recommendation_id" => attributed, "timestamp" => iso(t), "event_id" => SecureRandom.uuid
556
+ "recommendation_id" => attributed, "value" => value, "currency" => currency,
557
+ "timestamp" => iso(t), "event_id" => SecureRandom.uuid
554
558
  ))
555
559
  nil
556
560
  end
@@ -1459,6 +1463,20 @@ module Recapi
1459
1463
  end
1460
1464
  end
1461
1465
 
1466
+ # Refuses what the API would refuse in the event: a value that is not a
1467
+ # number of at least 0 (true and "12" are not numbers), a currency that is
1468
+ # not three letters or has no value.
1469
+ def check_amount(value, currency)
1470
+ unless value.nil? || ((value.is_a?(Integer) || value.is_a?(Float)) && value >= 0 && value.finite?)
1471
+ raise ArgumentError, "value must be a number of at least 0"
1472
+ end
1473
+ return if currency.nil?
1474
+ raise ArgumentError, "currency needs a value" if value.nil?
1475
+ return if currency.is_a?(String) && currency.match?(/\A[A-Za-z]{3}\z/)
1476
+
1477
+ raise ArgumentError, "currency must be three letters (ISO 4217)"
1478
+ end
1479
+
1462
1480
  def escape(id)
1463
1481
  URI.encode_www_form_component(id).gsub("+", "%20")
1464
1482
  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.16
4
+ version: 0.1.17
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-19 00:00:00.000000000 Z
11
+ date: 2026-11-22 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.'