recapi 0.1.14 → 0.1.16
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.
- checksums.yaml +4 -4
- data/README.md +27 -14
- data/lib/recapi.rb +11 -6
- metadata +6 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4630dc6459bcf8c5f8563c8d2725197ef77adea276bc85524363938c20ab8629
|
|
4
|
+
data.tar.gz: 6a58b1472dd36ba98c5757b6c5a1a4c297e9417b9fc23e0bbcb125a71f633dd7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 37f40070a0df5413ad196d63396be3aa4a3fad991e23daae7e5dbdb095dc803e278e6065c2047afd20466f3480be0e0904f7898903f1b1e5568a08bf43dc8ecb
|
|
7
|
+
data.tar.gz: e40ee11a50b6aa926ac3ed147de9676b8a185914cdddb228345ebb0088f33255a652599972405a2ecf7449fb9792ae22bd5d62cff6fb1354b6533000702113cb
|
data/README.md
CHANGED
|
@@ -5,20 +5,18 @@ and background jobs. Ruby 3.1 or newer (tested on 3.1 and 4.0), standard
|
|
|
5
5
|
library only (`net/http`, `json`), one file, no dependency: it requires only
|
|
6
6
|
default gems, so it loads in a Bundler app whose Gemfile lists nothing else.
|
|
7
7
|
|
|
8
|
-
Like the
|
|
9
|
-
|
|
10
|
-
([docs/history-spec.md](../../docs/history-spec.md)) and sends it with every
|
|
8
|
+
Like the Python and Go libraries, it keeps each user's decayed **history**
|
|
9
|
+
and sends it with every
|
|
11
10
|
recommendation request, **sends events in the background** in batches of up
|
|
12
11
|
to 1,000 with **retries**, **falls back to popular items** when recapi is
|
|
13
12
|
slow or failing, and **attributes** events on recommended items to the
|
|
14
13
|
recommendation that showed them (the dashboard's clicks and conversions). It
|
|
15
|
-
passes
|
|
14
|
+
passes recapi's shared test vectors.
|
|
16
15
|
|
|
17
16
|
## Install
|
|
18
17
|
|
|
19
|
-
The library is the gem `recapi` on RubyGems
|
|
20
|
-
|
|
21
|
-
add `gem "recapi"` to your Gemfile, or
|
|
18
|
+
The library is the gem `recapi` on RubyGems: add `gem "recapi"` to your
|
|
19
|
+
Gemfile, or
|
|
22
20
|
|
|
23
21
|
```sh
|
|
24
22
|
bundle add recapi
|
|
@@ -30,7 +28,7 @@ line with your address): save it as `lib/recapi.rb` and
|
|
|
30
28
|
`require Rails.root.join("lib/recapi").to_s` instead.
|
|
31
29
|
|
|
32
30
|
```sh
|
|
33
|
-
curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.
|
|
31
|
+
curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.16.rb
|
|
34
32
|
```
|
|
35
33
|
|
|
36
34
|
## Quick start
|
|
@@ -53,7 +51,7 @@ recs.items.each do |item|
|
|
|
53
51
|
item.item_id # a String, as every id recapi returns
|
|
54
52
|
item.metadata # the item's stored object (a Hash), or nil
|
|
55
53
|
# why it is listed ("for_you", "similar", "trending"...), safe to show anyone: map the codes you
|
|
56
|
-
# want to your own label (docs/api
|
|
54
|
+
# want to your own label (https://recapi.io/docs/api#reason-codes), and show none for a code you don't map.
|
|
57
55
|
label = LABELS[item.why]
|
|
58
56
|
end
|
|
59
57
|
```
|
|
@@ -94,7 +92,7 @@ and a log line.
|
|
|
94
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. |
|
|
95
93
|
| `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. |
|
|
96
94
|
| `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`. |
|
|
97
|
-
| `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
|
|
95
|
+
| `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), and `metadata` merges key by key: keys sent are set, the others kept, a key set to `nil` is removed (`"metadata" => { "old_price" => nil }`) and `"metadata" => nil` removes them all; to clear another 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. |
|
|
98
96
|
| `delete_user(user_id)` | Erases a user's events in recapi and their state in the store, retried like events. |
|
|
99
97
|
|
|
100
98
|
`Recapi::Client.new(api_key, base_url, store:, timeout:, flush_interval:,
|
|
@@ -140,6 +138,10 @@ so a visitor who never returns stays on disk (about 8 KB each): run
|
|
|
140
138
|
|
|
141
139
|
## Changes
|
|
142
140
|
|
|
141
|
+
- 2026-11-19: version 0.1.16. `Recapi::Error#docs_url` is the errors page's
|
|
142
|
+
section for an API error, and its message and the library's log lines end with it.
|
|
143
|
+
- 2026-11-19: version 0.1.15. This page's links lead to recapi.io's public docs,
|
|
144
|
+
and the gem lists its homepage, docs and support address. No code change.
|
|
143
145
|
- 2026-11-15: version 0.1.14. `recommend_lists` asks a page's rows in one call,
|
|
144
146
|
billed as one recommendation.
|
|
145
147
|
- 2026-11-14: version 0.1.13. `upsert_items(items, merge: true)` changes only the
|
|
@@ -160,10 +162,21 @@ so a visitor who never returns stays on disk (about 8 KB each): run
|
|
|
160
162
|
stalled each page for 5 s. The hook now starts with the sender, at the first
|
|
161
163
|
`track`.
|
|
162
164
|
|
|
165
|
+
## Errors and troubleshooting
|
|
166
|
+
|
|
167
|
+
`recommend` and `popular` answer an outage, a limit or a refused key (401, 403)
|
|
168
|
+
with popular items or an empty list and a log line; a bad request raises a
|
|
169
|
+
`Recapi::Error` (`code`, `status`), and a bad argument an `ArgumentError`.
|
|
170
|
+
An API error's `docs_url` is its section of the errors page (nil from older servers).
|
|
171
|
+
|
|
172
|
+
- [Errors](https://recapi.io/docs/errors): each error code, its cause and fix.
|
|
173
|
+
- [Troubleshooting](https://recapi.io/docs/troubleshooting): no lists yet,
|
|
174
|
+
empty lists, only popular items, key errors.
|
|
175
|
+
|
|
163
176
|
## Development
|
|
164
177
|
|
|
165
|
-
|
|
166
|
-
test-sdk` does) against a fake API on a local socket. The autopilot
|
|
178
|
+
From the repository's root, `scripts/ruby.sh -w sdk/ruby/test/run.rb [filter]`
|
|
179
|
+
runs the tests (`make test-sdk` does) against a fake API on a local socket. The autopilot
|
|
167
180
|
containers have no Ruby: `scripts/ruby.sh` then fetches Debian's ruby3.1
|
|
168
181
|
once and runs it without RubyGems, so the tests have their own small runner
|
|
169
182
|
(`test/test_case.rb`), no minitest; `test/gem_test.rb` loads RubyGems itself
|
|
@@ -173,7 +186,7 @@ serves it (`server/internal/rubysdk`): any change to it needs a new
|
|
|
173
186
|
`Recapi::VERSION` and the line `go test ./internal/rubysdk` (in `server/`)
|
|
174
187
|
prints for `published.json`.
|
|
175
188
|
|
|
176
|
-
`RECAPI_KEY=rk_sec_…
|
|
177
|
-
|
|
189
|
+
`RECAPI_KEY=rk_sec_… scripts/ruby.sh sdk/ruby/sample.rb [item ids]` runs
|
|
190
|
+
`sample.rb` against staging (`RECAPI_URL` picks another API):
|
|
178
191
|
it tracks views for a new visitor, then prints their recommendations and the
|
|
179
192
|
popular items.
|
data/lib/recapi.rb
CHANGED
|
@@ -25,7 +25,7 @@ rescue LoadError
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
module Recapi
|
|
28
|
-
VERSION = "0.1.
|
|
28
|
+
VERSION = "0.1.16"
|
|
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
|
-
|
|
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
|
|
@@ -1412,7 +1416,8 @@ module Recapi
|
|
|
1412
1416
|
code = CODE_BY_STATUS[status] || (status >= 500 ? "internal" : "http_error") if code.nil? || code == ""
|
|
1413
1417
|
message = body["message"]
|
|
1414
1418
|
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"])
|
|
1419
|
+
Error.new(code.to_s, message.to_s, status: status, retry_after: retry_after(response["Retry-After"]),
|
|
1420
|
+
docs_url: body["docs_url"])
|
|
1416
1421
|
end
|
|
1417
1422
|
|
|
1418
1423
|
def retry_after(header)
|
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.
|
|
4
|
+
version: 0.1.16
|
|
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-
|
|
11
|
+
date: 2026-11-19 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.'
|
|
@@ -23,7 +23,10 @@ files:
|
|
|
23
23
|
homepage: https://recapi.io
|
|
24
24
|
licenses:
|
|
25
25
|
- MIT
|
|
26
|
-
metadata:
|
|
26
|
+
metadata:
|
|
27
|
+
homepage_uri: https://recapi.io/docs
|
|
28
|
+
documentation_uri: https://recapi.io/docs
|
|
29
|
+
bug_tracker_uri: https://recapi.io/contact
|
|
27
30
|
post_install_message:
|
|
28
31
|
rdoc_options: []
|
|
29
32
|
require_paths:
|