recapi 0.1.15 → 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 +7 -2
  3. data/lib/recapi.rb +32 -9
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 955bf1ce3f0c03191af7c2292d51f26a8c27ecd5eabd92ae8fb0b95e21d36c07
4
- data.tar.gz: f5ce6c0d310e8a307cf1a8db3563d0184a4c6e00cc41c05575c17ba690f78580
3
+ metadata.gz: 2a56e546f32406629e6623fa3f0617bde146a041b39c744732de8012e64f518e
4
+ data.tar.gz: ed62a0ed257ed7c3c591ec0a669e1cff3cf23b3ae1b4fc90da057396d68c92e1
5
5
  SHA512:
6
- metadata.gz: c94cb8d86868e975e5ef63594b957b439feaf4880cbc1c2acfa3ecccb4d61fbfca70aaafe804c6c2419f315e9ebbfe78ab7ad61a9c6bcbcfc5af9fc4f48e87f1
7
- data.tar.gz: dc57e8e7a0cc117b54bdc3543a616b80b035b4573ca98cd6a9d02dd0e752355045cba66849b8cfbea84da6efd94d60189b6f98bcc45bffbcd53aef5c68b238f3
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.15.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,10 @@ 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.
143
+ - 2026-11-19: version 0.1.16. `Recapi::Error#docs_url` is the errors page's
144
+ section for an API error, and its message and the library's log lines end with it.
141
145
  - 2026-11-19: version 0.1.15. This page's links lead to recapi.io's public docs,
142
146
  and the gem lists its homepage, docs and support address. No code change.
143
147
  - 2026-11-15: version 0.1.14. `recommend_lists` asks a page's rows in one call,
@@ -165,6 +169,7 @@ so a visitor who never returns stays on disk (about 8 KB each): run
165
169
  `recommend` and `popular` answer an outage, a limit or a refused key (401, 403)
166
170
  with popular items or an empty list and a log line; a bad request raises a
167
171
  `Recapi::Error` (`code`, `status`), and a bad argument an `ArgumentError`.
172
+ An API error's `docs_url` is its section of the errors page (nil from older servers).
168
173
 
169
174
  - [Errors](https://recapi.io/docs/errors): each error code, its cause and fix.
170
175
  - [Troubleshooting](https://recapi.io/docs/troubleshooting): no lists yet,
data/lib/recapi.rb CHANGED
@@ -25,7 +25,7 @@ rescue LoadError
25
25
  end
26
26
 
27
27
  module Recapi
28
- VERSION = "0.1.15"
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.
@@ -288,12 +288,16 @@ module Recapi
288
288
 
289
289
  # A failed call. status is 0 when no HTTP response was received. code is the
290
290
  # API's error code (invalid_request, rate_limited, quota_exceeded...) or a
291
- # client one: network, timeout, invalid_response.
291
+ # client one: network, timeout, invalid_response. docs_url is the errors
292
+ # page's section for the code, sent by the API (nil from an older server
293
+ # and on client codes); message then ends with ", see <docs_url>", so a
294
+ # log line that prints the message names the fix.
292
295
  class Error < StandardError
293
- attr_reader :code, :status, :retry_after
296
+ attr_reader :code, :status, :retry_after, :docs_url
294
297
 
295
- def initialize(code, message, status: 0, retry_after: nil)
296
- super(message)
298
+ def initialize(code, message, status: 0, retry_after: nil, docs_url: nil)
299
+ @docs_url = docs_url.is_a?(String) && !docs_url.empty? ? docs_url : nil
300
+ super(@docs_url ? "#{message}, see #{@docs_url}" : message)
297
301
  @code = code
298
302
  @status = status
299
303
  @retry_after = retry_after
@@ -514,11 +518,14 @@ module Recapi
514
518
  # again until a positive event on it). recommendation_id overrides the
515
519
  # automatic attribution. Ids may be strings or integers. An item_id the API
516
520
  # would refuse raises nothing: a page often takes it from a visitor's URL, so
517
- # the event is dropped and logged instead.
518
- 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)
519
525
  level = level_value(level)
520
526
  user_id, anon_id, recommendation_id = check_ids(user_id: user_id, anon_id: anon_id,
521
527
  recommendation_id: recommendation_id)
528
+ check_amount(value, currency)
522
529
  item_id = id_string(item_id)
523
530
  # A crafted link must not fail the page. Nothing is kept: in the history it
524
531
  # would make the API refuse every later list. The line never quotes the
@@ -546,7 +553,8 @@ module Recapi
546
553
  end
547
554
  enqueue(compact(
548
555
  "item_id" => item_id, "level" => level, "user_id" => user_id, "anon_id" => anon_id,
549
- "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
550
558
  ))
551
559
  nil
552
560
  end
@@ -1412,7 +1420,8 @@ module Recapi
1412
1420
  code = CODE_BY_STATUS[status] || (status >= 500 ? "internal" : "http_error") if code.nil? || code == ""
1413
1421
  message = body["message"]
1414
1422
  message = "HTTP #{status}" if message.nil? || message == ""
1415
- Error.new(code.to_s, message.to_s, status: status, retry_after: retry_after(response["Retry-After"]))
1423
+ Error.new(code.to_s, message.to_s, status: status, retry_after: retry_after(response["Retry-After"]),
1424
+ docs_url: body["docs_url"])
1416
1425
  end
1417
1426
 
1418
1427
  def retry_after(header)
@@ -1454,6 +1463,20 @@ module Recapi
1454
1463
  end
1455
1464
  end
1456
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
+
1457
1480
  def escape(id)
1458
1481
  URI.encode_www_form_component(id).gsub("+", "%20")
1459
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.15
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.'