calmlens 0.25.0 → 0.27.3
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 +14 -12
- data/lib/calmlens/assets.rb +101 -143
- data/lib/calmlens/cdn.rb +24 -7
- data/lib/calmlens/error_codes.rb +2 -8
- data/lib/calmlens/errors.rb +41 -8
- data/lib/calmlens/events.rb +25 -42
- data/lib/calmlens/folders.rb +33 -38
- data/lib/calmlens/hash_lists.rb +38 -53
- data/lib/calmlens/internal.rb +11 -1
- data/lib/calmlens/lenses.rb +33 -40
- data/lib/calmlens/model.rb +234 -0
- data/lib/calmlens/models.rb +741 -0
- data/lib/calmlens/option_keys.rb +0 -1
- data/lib/calmlens/policies.rb +44 -52
- data/lib/calmlens/project_configuration.rb +33 -35
- data/lib/calmlens/resource.rb +27 -50
- data/lib/calmlens/response.rb +29 -0
- data/lib/calmlens/server_client.rb +103 -15
- data/lib/calmlens/session.rb +105 -21
- data/lib/calmlens/testing/asset_filters.rb +1 -1
- data/lib/calmlens/testing/fake_api.rb +31 -72
- data/lib/calmlens/testing/mock_decision.rb +3 -3
- data/lib/calmlens/testing.rb +19 -21
- data/lib/calmlens/text.rb +6 -7
- data/lib/calmlens/upload_mime_types.rb +1 -0
- data/lib/calmlens/upload_tokens.rb +9 -3
- data/lib/calmlens/version.rb +1 -1
- data/lib/calmlens/webhooks.rb +16 -37
- data/lib/calmlens.rb +2 -0
- data/sig/calmlens.rbs +220 -148
- data/sig/error_codes.rbs +1 -4
- data/sig/models.rbs +567 -0
- data/sig/types.rbs +48 -934
- 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: dd55b9d818f3fba64bfb421f5aac982a555b694d0ecb7351d71f453fb5c7f360
|
|
4
|
+
data.tar.gz: 7fd0ec890fcc7ec6e58402b5d776a2b4537d95594b77e9c8eac751684930d7c4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4e7aca5d883c740103352261128b773249891b678002d64c8aa52889b43d329e3a80bc437a9743b9239457552db4e4591ce0a6cfac7afa722273aae79c3b5bc0
|
|
7
|
+
data.tar.gz: 9fb6664747c8e35995f624c49cfcd3cace38e62ac5506aeedc68dbd788ce7cb2252a835ba8ffa59b47f035f6aae853a90e555c6759fca5c2b7f34d2f0083f61e
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# CalmLens Ruby SDK
|
|
2
2
|
|
|
3
|
-
Ruby 3.
|
|
3
|
+
Ruby 3.3+ server-side client for [CalmLens](https://calmlens.com) content moderation, uploads, events, webhooks and CDN delivery.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -14,7 +14,7 @@ Or add it to your Gemfile:
|
|
|
14
14
|
gem "calmlens"
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
The gem has no runtime dependencies: it uses only the standard library (Net::HTTP, JSON, OpenSSL). It ships YARD documentation and RBS signatures in `sig/`, so Steep and other RBS tools can check your calls and the
|
|
17
|
+
The gem has no runtime dependencies: it uses only the standard library (Net::HTTP, JSON, OpenSSL). It ships YARD documentation and RBS signatures in `sig/`, so Steep and other RBS tools can check your calls and the fields you read.
|
|
18
18
|
|
|
19
19
|
Versions follow semantic versioning: see [Versioning and support](https://calmlens.com/docs/sdk#versioning-and-support).
|
|
20
20
|
|
|
@@ -25,7 +25,8 @@ Create an API key and copy your project ID in the CalmLens portal, and keep both
|
|
|
25
25
|
```ruby
|
|
26
26
|
require 'calmlens'
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
# Reads CALMLENS_API_KEY and CALMLENS_PROJECT_ID from the environment.
|
|
29
|
+
client = CalmLens::ServerClient.new
|
|
29
30
|
|
|
30
31
|
# Classify text and wait for the decision.
|
|
31
32
|
asset = client.text.classify(text: 'Great photo!', external_id: 'comment-42')
|
|
@@ -34,22 +35,22 @@ asset = client.text.classify(text: 'Great photo!', external_id: 'comment-42')
|
|
|
34
35
|
queued = File.open('photo.jpg', 'rb') do |file|
|
|
35
36
|
client.assets.create(name: 'photo.jpg', content: {data: file, type: 'image/jpeg'})
|
|
36
37
|
end
|
|
37
|
-
decided = client.assets.wait_for_decision(
|
|
38
|
+
decided = client.assets.wait_for_decision(queued.id, timeout: 30)
|
|
38
39
|
|
|
39
|
-
if %w[allowed limited].include?(decided
|
|
40
|
-
puts "Show #{decided
|
|
40
|
+
if %w[allowed limited].include?(decided.classification.moderation.decision)
|
|
41
|
+
puts "Show #{decided.id}"
|
|
41
42
|
end
|
|
42
43
|
|
|
43
44
|
client.close
|
|
44
45
|
```
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
Methods take the ID they act on first and their options as snake_case keywords. Results are read-only objects with snake_case readers; `[]`, `dig`, `to_h` and pattern matching also work, and `last_response.request_id` identifies the request.
|
|
47
48
|
|
|
48
|
-
`asset
|
|
49
|
+
`asset.classification.moderation.decision` decides what to do with the content:
|
|
49
50
|
|
|
50
51
|
| Status | What to do |
|
|
51
52
|
| ---------------- | ------------------------------------------------------------------------------------ |
|
|
52
|
-
| `
|
|
53
|
+
| `nil` | Not decided yet. Wait, poll or listen for a webhook. |
|
|
53
54
|
| `allowed` | Show it to everyone. |
|
|
54
55
|
| `limited` | Show it, but reduce its reach, such as keeping it out of recommendations. |
|
|
55
56
|
| `age-restricted` | Show it only behind your own age check, with `viewer_claims: {age_confirmed: true}`. |
|
|
@@ -65,13 +66,14 @@ New statuses and fields may appear in later API versions. The SDK returns them u
|
|
|
65
66
|
- [Stream uploads from any IO](https://calmlens.com/docs/sdk/ruby/reference#upload-content). Content larger than `part_size_bytes:` (8 MiB) that can be read again, such as a String or a File, goes in [parts](https://calmlens.com/docs/guides/multipart-uploads) automatically, each retried on its own and resumable with the same idempotency key.
|
|
66
67
|
- [Moderate replies with their conversation](https://calmlens.com/docs/sdk/ruby/reference#conversation-context).
|
|
67
68
|
- [Sign upload tokens for browser uploads](https://calmlens.com/docs/sdk/ruby/reference#upload-tokens) through [upload presets](https://calmlens.com/docs/guides/upload-presets).
|
|
68
|
-
- [Record reviewer decisions](https://calmlens.com/docs/sdk/ruby/reference#reviewer-decisions)
|
|
69
|
+
- [Record reviewer decisions](https://calmlens.com/docs/sdk/ruby/reference#reviewer-decisions).
|
|
69
70
|
- [Serve content from the CDN](https://calmlens.com/docs/sdk/ruby#cdn-methods), with [lenses, transforms and signed URLs](https://calmlens.com/docs/sdk/ruby/reference#cdn-urls) and [age-restricted delivery](https://calmlens.com/docs/guides/delivering-assets#age-restricted-assets).
|
|
70
|
-
- [Organise assets in folders](https://calmlens.com/docs/sdk/ruby/reference#folders), [search them](https://calmlens.com/docs/guides/search) and [page through lists](https://calmlens.com/docs/sdk/ruby#pagination)
|
|
71
|
+
- [Organise assets in folders](https://calmlens.com/docs/sdk/ruby/reference#folders), [search them](https://calmlens.com/docs/guides/search) and [page through lists](https://calmlens.com/docs/sdk/ruby#pagination) with `auto_paging_each`.
|
|
71
72
|
- [Match images against hash lists](https://calmlens.com/docs/sdk/ruby/reference#hash-lists).
|
|
72
|
-
- Keep [upload presets](https://calmlens.com/docs/guides/upload-presets), project settings and metadata fields in your code: `upload_presets` (`list`, `
|
|
73
|
+
- Keep [upload presets](https://calmlens.com/docs/guides/upload-presets), project settings and metadata fields in your code: `upload_presets` (`list`, `ensure`, `delete`), `project_settings` (`get`, `update`) and `metadata_fields` (`list`, `ensure`, `delete`). `ensure` and `update` create or replace by key or ID, and the `revision` you read refuses a change made since.
|
|
73
74
|
- [Read the events feed](https://calmlens.com/docs/sdk/ruby#pagination) and [verify webhooks in Rails or Rack](https://calmlens.com/docs/sdk/ruby#webhooks).
|
|
74
75
|
- [Handle errors](https://calmlens.com/docs/sdk/ruby/reference#errors) with automatic [retries, timeouts and idempotency keys](https://calmlens.com/docs/sdk/ruby/reference#retries-and-timeouts).
|
|
76
|
+
- [Log requests and record metrics](https://calmlens.com/docs/sdk/ruby/reference#logging-and-metrics), and [call endpoints this version has no method for](https://calmlens.com/docs/sdk/ruby/reference#custom-requests).
|
|
75
77
|
- [Type-check your code with RBS signatures](https://calmlens.com/docs/sdk/ruby/reference#types).
|
|
76
78
|
|
|
77
79
|
Every method is listed in the [Ruby SDK documentation](https://calmlens.com/docs/sdk/ruby#methods).
|
data/lib/calmlens/assets.rb
CHANGED
|
@@ -5,12 +5,12 @@ require 'securerandom'
|
|
|
5
5
|
module CalmLens
|
|
6
6
|
# Uploads, moderation and management of assets. Reach it through {ServerClient#assets}.
|
|
7
7
|
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
# Results are
|
|
12
|
-
#
|
|
13
|
-
#
|
|
8
|
+
# Methods take an asset's ID as their first argument and options as snake_case keywords; keys
|
|
9
|
+
# inside `metadata` are sent unchanged. Any method also accepts
|
|
10
|
+
# `request_options: { idempotency_key:, timeout:, open_timeout:, max_retries: }`.
|
|
11
|
+
# Results are {Asset} models. Enum values added to the API later appear as they are, so compare
|
|
12
|
+
# `classification.moderation.decision` and similar values with known strings and treat anything
|
|
13
|
+
# else as not ready.
|
|
14
14
|
class Assets < Resource
|
|
15
15
|
# The prefix of this resource's operations in Internal::OPTION_KEYS.
|
|
16
16
|
OPERATIONS = 'assets'
|
|
@@ -32,7 +32,7 @@ module CalmLens
|
|
|
32
32
|
# The longest `idempotency_key` of an upload sent in parts: each part's key adds `:part-<n>`.
|
|
33
33
|
MAX_SPLIT_IDEMPOTENCY_KEY_LENGTH = 240
|
|
34
34
|
# The classification statuses of a settled asset: `completed` (with its decision in
|
|
35
|
-
# `classification.moderation.
|
|
35
|
+
# `classification.moderation.decision`) or `failed`. {#wait_for_decision} returns at the first.
|
|
36
36
|
SETTLED_STATUSES = %w[completed failed].freeze
|
|
37
37
|
DECISION_POLL_FIRST_DELAY = 0.5
|
|
38
38
|
# Each wait between decision checks is this many times the one before, up to DECISION_POLL_MAX_DELAY.
|
|
@@ -49,92 +49,64 @@ module CalmLens
|
|
|
49
49
|
# @option options [String] :query
|
|
50
50
|
# @option options [String] :mode `hybrid`, `semantic` or `keyword`
|
|
51
51
|
# @option options [Integer] :page_size how many results, 1 to 20 (default 10)
|
|
52
|
-
# @return [
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
# @return [AssetSearchResult] `query`, `items` (each with `asset`, `score` and `matched_fields`) and
|
|
53
|
+
# `semantic_status`
|
|
54
|
+
def search(**options)
|
|
55
|
+
call(options, __method__) do |api, request|
|
|
56
|
+
build(AssetSearchResult, request('GET', project('assets/search'), query: api, request_options: request))
|
|
57
|
+
end
|
|
55
58
|
end
|
|
56
59
|
|
|
57
60
|
# Lists one page of assets, newest first unless `sort` says otherwise.
|
|
58
61
|
# @option options [String] :cursor from the previous page
|
|
59
62
|
# @option options [Integer] :page_size 1 to 100, default 50
|
|
60
|
-
# @option options [String, nil] :filter an RSQL filter such as `classification.moderation.
|
|
63
|
+
# @option options [String, nil] :filter an RSQL filter such as `classification.moderation.decision==allowed`;
|
|
61
64
|
# `folderId==fldr_…` lists one folder's assets and `folderId==null` the project root's
|
|
62
65
|
# @option options [String] :sort `createdAt` and `id`, comma-separated, descending when prefixed
|
|
63
66
|
# with `-`, such as `-createdAt,id`; `-createdAt` by default
|
|
64
|
-
# @return [
|
|
65
|
-
def list(options
|
|
67
|
+
# @return [AssetPage] {Page#auto_paging_each} fetches the rest.
|
|
68
|
+
def list(**options)
|
|
66
69
|
call(options, __method__) do |api, request|
|
|
67
|
-
request('GET', project('assets'), query: api, request_options: request)
|
|
70
|
+
page(AssetPage, request('GET', project('assets'), query: api, request_options: request), options) { list(**_1) }
|
|
68
71
|
end
|
|
69
72
|
end
|
|
70
73
|
|
|
71
|
-
# Iterates every asset matching the options, fetching pages as needed.
|
|
72
|
-
# @param options [Hash] the options of {#list}; `cursor` sets the starting position.
|
|
73
|
-
# @yieldparam asset [Hash]
|
|
74
|
-
# @return [Enumerator, nil] an Enumerator when no block is given.
|
|
75
|
-
def each(options = {}, &block)
|
|
76
|
-
return enum_for(:each, options) unless block
|
|
77
|
-
|
|
78
|
-
each_item(options, __method__, method(:list), &block)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
74
|
# Lists one page of an asset's children, such as frames extracted from a video.
|
|
82
|
-
# @
|
|
75
|
+
# @param asset_id [String]
|
|
83
76
|
# @option options [String] :cursor
|
|
84
77
|
# @option options [Integer] :page_size
|
|
85
78
|
# @option options [String] :sort as for {#list}
|
|
86
|
-
# @return [
|
|
87
|
-
def list_children(options)
|
|
88
|
-
call(options, __method__) do |api, request|
|
|
89
|
-
|
|
90
|
-
|
|
79
|
+
# @return [AssetPage] {Page#auto_paging_each} fetches the rest.
|
|
80
|
+
def list_children(asset_id, **options)
|
|
81
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
82
|
+
response = request('GET', "#{asset_path(api)}/children", query: without(api, 'assetId'), request_options: request)
|
|
83
|
+
page(AssetPage, response, options) { list_children(asset_id, **_1) }
|
|
91
84
|
end
|
|
92
85
|
end
|
|
93
86
|
|
|
94
87
|
# Lists one page of an audio or video asset's findings timeline, in time order.
|
|
95
88
|
# `classification.moderation.timeline.segmentCount` says how many segments there are. Pass each
|
|
96
|
-
# page's `
|
|
97
|
-
# @
|
|
89
|
+
# page's `next_cursor` back unchanged; cursors are opaque.
|
|
90
|
+
# @param asset_id [String]
|
|
98
91
|
# @option options [String] :cursor
|
|
99
92
|
# @option options [Integer] :page_size
|
|
100
|
-
# @return [
|
|
101
|
-
def list_timeline(options)
|
|
102
|
-
call(options, __method__) do |api, request|
|
|
103
|
-
|
|
104
|
-
|
|
93
|
+
# @return [AssetTimelinePage] {Page#auto_paging_each} fetches the rest.
|
|
94
|
+
def list_timeline(asset_id, **options)
|
|
95
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
96
|
+
response = request('GET', project("assets/#{Internal.segment(api['assetId'])}/timeline"), query: without(api, 'assetId'),
|
|
97
|
+
request_options: request)
|
|
98
|
+
page(AssetTimelinePage, response, options) { list_timeline(asset_id, **_1) }
|
|
105
99
|
end
|
|
106
100
|
end
|
|
107
101
|
|
|
108
|
-
#
|
|
109
|
-
# @
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def each_timeline_segment(options, &block)
|
|
113
|
-
return enum_for(:each_timeline_segment, options) unless block
|
|
114
|
-
|
|
115
|
-
each_item(options, __method__, method(:list_timeline), &block)
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
# Iterates every child of an asset, fetching pages as needed.
|
|
119
|
-
# @param options [Hash] the options of {#list_children}
|
|
120
|
-
# @yieldparam asset [Hash]
|
|
121
|
-
# @return [Enumerator, nil] an Enumerator when no block is given.
|
|
122
|
-
def each_child(options, &block)
|
|
123
|
-
return enum_for(:each_child, options) unless block
|
|
124
|
-
|
|
125
|
-
each_item(options, __method__, method(:list_children), &block)
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
# @option options [String] :asset_id
|
|
129
|
-
# @return [Hash] the asset
|
|
130
|
-
def retrieve(options)
|
|
131
|
-
call(options, __method__) do |api, request|
|
|
132
|
-
request('GET', "/assets/#{Internal.segment(api['assetId'])}", request_options: request)
|
|
133
|
-
end
|
|
102
|
+
# @param asset_id [String]
|
|
103
|
+
# @return [Asset]
|
|
104
|
+
def retrieve(asset_id, **options)
|
|
105
|
+
call(with_ids(options, asset_id: asset_id), __method__) { |api, request| get_asset(api['assetId'], request) }
|
|
134
106
|
end
|
|
135
107
|
|
|
136
108
|
# Waits until an asset is settled, its `classification.status` `completed` (with its decision in
|
|
137
|
-
# `classification.moderation.
|
|
109
|
+
# `classification.moderation.decision`) or `failed`, and returns the asset. A `queued` or `processing`
|
|
138
110
|
# asset is still waiting, even when a reviewer has already decided it. It retrieves the asset,
|
|
139
111
|
# waiting 0.5 seconds before the second check and 1.5 times longer each time after that, up to 2
|
|
140
112
|
# seconds.
|
|
@@ -142,13 +114,13 @@ module CalmLens
|
|
|
142
114
|
#
|
|
143
115
|
# `timeout:` is the whole wait in seconds; `request_options: {timeout:}` still limits each request.
|
|
144
116
|
# To stop waiting early, run the call in a thread you can stop: it only sleeps and sends requests.
|
|
145
|
-
# @
|
|
117
|
+
# @param asset_id [String]
|
|
146
118
|
# @option options [Numeric] :timeout seconds to wait for a decision (default 60)
|
|
147
|
-
# @return [
|
|
119
|
+
# @return [Asset] the settled asset
|
|
148
120
|
# @raise [TimeoutError] `sdk/timeout` when the asset isn't settled in time; `details` holds
|
|
149
|
-
# `'assetId'` and the last `'classificationStatus'` and `'
|
|
150
|
-
def wait_for_decision(options)
|
|
151
|
-
call(options, __method__) do |api, request|
|
|
121
|
+
# `'assetId'` and the last `'classificationStatus'` and `'moderationDecision'`.
|
|
122
|
+
def wait_for_decision(asset_id, **options)
|
|
123
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
152
124
|
timeout = api.fetch('timeout', DEFAULT_DECISION_TIMEOUT)
|
|
153
125
|
unless timeout.is_a?(Numeric) && timeout.real? && timeout.to_f.finite? && !timeout.negative?
|
|
154
126
|
raise Internal.invalid('timeout must be a non-negative number of seconds')
|
|
@@ -159,77 +131,57 @@ module CalmLens
|
|
|
159
131
|
end
|
|
160
132
|
|
|
161
133
|
# Updates an asset's name, description, tags, visibility, metadata and similar fields.
|
|
162
|
-
# @
|
|
163
|
-
# @return [
|
|
164
|
-
def update(options)
|
|
165
|
-
call(options, __method__) do |api, request|
|
|
166
|
-
request('PATCH',
|
|
167
|
-
request_options: request)
|
|
134
|
+
# @param asset_id [String]
|
|
135
|
+
# @return [Asset] the updated asset
|
|
136
|
+
def update(asset_id, **options)
|
|
137
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
138
|
+
build(Asset, request('PATCH', asset_path(api), body: without(api, 'assetId'), request_options: request))
|
|
168
139
|
end
|
|
169
140
|
end
|
|
170
141
|
|
|
171
142
|
# Processes an asset whose `classification.status` is `failed` again.
|
|
172
|
-
# @
|
|
173
|
-
# @return [
|
|
174
|
-
def retry(options)
|
|
175
|
-
call(options, __method__) do |api, request|
|
|
176
|
-
request('POST', "
|
|
143
|
+
# @param asset_id [String]
|
|
144
|
+
# @return [Asset]
|
|
145
|
+
def retry(asset_id, **options)
|
|
146
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
147
|
+
build(Asset, request('POST', "#{asset_path(api)}/retry", request_options: request))
|
|
177
148
|
end
|
|
178
149
|
end
|
|
179
150
|
|
|
180
|
-
# Records a reviewer's decision, replacing the policy's: `classification.moderation.
|
|
151
|
+
# Records a reviewer's decision, replacing the policy's: `classification.moderation.decision`
|
|
181
152
|
# becomes the decision, the decision and note join `classification.moderation.manualReviews` and
|
|
182
153
|
# the matching `asset.<status>` event is sent with the API key as its `actor`. An asset still pending
|
|
183
154
|
# moderation can only be blocked or purged. Purging needs `confirm_purge: true` and the
|
|
184
155
|
# `assets:delete` permission, and permanently deletes the stored content. Not retried: it carries
|
|
185
156
|
# no `Idempotency-Key`.
|
|
186
|
-
# @
|
|
157
|
+
# @param asset_id [String]
|
|
187
158
|
# @option options [String] :decision `allowed`, `limited`, `age-restricted`, `blocked` or `purged`
|
|
188
159
|
# @option options [String] :note why, 1 to 1499 characters
|
|
189
160
|
# @option options [Boolean] :confirm_purge must be true to purge
|
|
190
|
-
# @return [
|
|
191
|
-
def decide(options)
|
|
192
|
-
call(options, __method__) do |api, request|
|
|
193
|
-
request('POST', "
|
|
194
|
-
request_options: request)
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
# Completes the policy evidence behind a decided asset's moderation decision: the report's
|
|
199
|
-
# `classification.moderation.explanationComplete` becomes `true` and every action stays as decided.
|
|
200
|
-
# With `rerun: true` the asset is moderated again with the project's current policy and an
|
|
201
|
-
# `asset.rerun-requested` event is recorded; use it when the policy changed since the decision
|
|
202
|
-
# (`asset/policy-changed`). The asset returns unchanged: it keeps its decision until the re-run's
|
|
203
|
-
# is stored, reported by `asset.classified` and `asset.<status>` events, and a failed re-run keeps
|
|
204
|
-
# it. A reviewed asset raises `asset/reviewed`, and one whose content is gone
|
|
205
|
-
# `asset/content-unavailable`.
|
|
206
|
-
# @option options [String] :asset_id
|
|
207
|
-
# @option options [Boolean] :rerun
|
|
208
|
-
# @return [Hash] the asset
|
|
209
|
-
def explain(options)
|
|
210
|
-
call(options, __method__) do |api, request|
|
|
211
|
-
request('POST', "/assets/#{Internal.segment(api['assetId'])}/explain",
|
|
212
|
-
body: api['rerun'] ? { 'rerun' => true } : {}, request_options: request)
|
|
161
|
+
# @return [Asset]
|
|
162
|
+
def decide(asset_id, **options)
|
|
163
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
164
|
+
build(Asset, request('POST', "#{asset_path(api)}/decision", body: without(api, 'assetId'), request_options: request))
|
|
213
165
|
end
|
|
214
166
|
end
|
|
215
167
|
|
|
216
168
|
# Removes the stored content and returns the asset with `content.status` `removed` (reason
|
|
217
169
|
# `requested`); the record and classification results are kept.
|
|
218
|
-
# @
|
|
219
|
-
# @return [
|
|
220
|
-
def remove_content(options)
|
|
221
|
-
call(options, __method__) do |api, request|
|
|
222
|
-
request('DELETE', "
|
|
170
|
+
# @param asset_id [String]
|
|
171
|
+
# @return [Asset]
|
|
172
|
+
def remove_content(asset_id, **options)
|
|
173
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
174
|
+
build(Asset, request('DELETE', "#{asset_path(api)}/content", request_options: request))
|
|
223
175
|
end
|
|
224
176
|
end
|
|
225
177
|
|
|
226
178
|
# Deletes the asset record and its stored content. An asset extracted from another (one with a
|
|
227
179
|
# `parentId`) raises `asset/child-not-deletable`: delete its parent instead.
|
|
228
|
-
# @
|
|
180
|
+
# @param asset_id [String]
|
|
229
181
|
# @return [nil]
|
|
230
|
-
def delete(options)
|
|
231
|
-
call(options, __method__) do |api, request|
|
|
232
|
-
request('DELETE',
|
|
182
|
+
def delete(asset_id, **options)
|
|
183
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
184
|
+
request('DELETE', asset_path(api), request_options: request)
|
|
233
185
|
nil
|
|
234
186
|
end
|
|
235
187
|
end
|
|
@@ -243,8 +195,8 @@ module CalmLens
|
|
|
243
195
|
# retried: it carries no `Idempotency-Key`.
|
|
244
196
|
# @option options [Array<String>] :asset_ids 1 to 500 asset IDs
|
|
245
197
|
# @option options [String] :operation one of {BATCH_OPERATIONS}
|
|
246
|
-
# @return [
|
|
247
|
-
def batch(options)
|
|
198
|
+
# @return [AssetBatch] `batch_id`, `status` (`queued`) and `asset_count`
|
|
199
|
+
def batch(**options)
|
|
248
200
|
call(options, __method__) do |api, request|
|
|
249
201
|
ids = api['assetIds']
|
|
250
202
|
unless ids.is_a?(Array) && ids.size.between?(1, MAX_BATCH_ASSETS)
|
|
@@ -253,19 +205,19 @@ module CalmLens
|
|
|
253
205
|
raise Internal.invalid('Every asset ID must be a non-empty string') unless ids.all? { |id| id.is_a?(String) && !id.empty? }
|
|
254
206
|
raise Internal.invalid("operation must be one of #{BATCH_OPERATIONS.join(', ')}") unless BATCH_OPERATIONS.include?(api['operation'])
|
|
255
207
|
|
|
256
|
-
request('POST', project('assets/batch'), body: { 'assetIds' => ids, 'operation' => api['operation'] },
|
|
257
|
-
|
|
208
|
+
build(AssetBatch, request('POST', project('assets/batch'), body: { 'assetIds' => ids, 'operation' => api['operation'] },
|
|
209
|
+
request_options: request))
|
|
258
210
|
end
|
|
259
211
|
end
|
|
260
212
|
|
|
261
213
|
# Moves an asset into a folder, or to the project root with `folder_id: nil`.
|
|
262
|
-
# @
|
|
214
|
+
# @param asset_id [String]
|
|
263
215
|
# @option options [String, nil] :folder_id
|
|
264
|
-
# @return [
|
|
265
|
-
def move(options)
|
|
266
|
-
call(options, __method__) do |api, request|
|
|
267
|
-
request('POST', "/
|
|
268
|
-
|
|
216
|
+
# @return [Asset]
|
|
217
|
+
def move(asset_id, **options)
|
|
218
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
219
|
+
build(Asset, request('POST', "#{asset_path(api)}/move", body: { 'folderId' => Internal.required(api, 'folderId') },
|
|
220
|
+
request_options: request))
|
|
269
221
|
end
|
|
270
222
|
end
|
|
271
223
|
|
|
@@ -304,18 +256,18 @@ module CalmLens
|
|
|
304
256
|
# @option options [Hash] :context `{items: [{text:, author_id:}]}`, earlier posts in the
|
|
305
257
|
# conversation, oldest first. Only the posts CalmLens uses are sent: the most recent
|
|
306
258
|
# {ModerationContext::USED_ITEMS} that fit in {ModerationContext::USED_CHARACTERS} characters.
|
|
307
|
-
# @return [
|
|
308
|
-
def create(options)
|
|
259
|
+
# @return [Asset]
|
|
260
|
+
def create(**options)
|
|
309
261
|
call(options, __method__) { |api, request| submit(api, request, false) }
|
|
310
262
|
end
|
|
311
263
|
|
|
312
264
|
# Uploads content and waits for the moderation decision where the content type supports it.
|
|
313
265
|
# Documents, websites, audio, video and files over 20 MiB sent in parts always finish in the
|
|
314
266
|
# background and return `pending`. Takes the options of {#create}.
|
|
315
|
-
# @return [
|
|
267
|
+
# @return [Asset] show it to everyone only when `classification.moderation.decision` is
|
|
316
268
|
# `allowed` or `limited` (reduce the reach of `limited` assets). Serve `age-restricted` assets
|
|
317
269
|
# only behind your own age check, through signed URLs.
|
|
318
|
-
def classify(options)
|
|
270
|
+
def classify(**options)
|
|
319
271
|
call(options, __method__) do |api, request|
|
|
320
272
|
content = api['content']
|
|
321
273
|
if content.is_a?(Hash) && content.key?('plainText') && (!content['plainText'].is_a?(String) || content['plainText'].strip.empty?)
|
|
@@ -329,13 +281,13 @@ module CalmLens
|
|
|
329
281
|
# Uploads a later part of a multipart upload started with {#create} or {#classify} and `part:`.
|
|
330
282
|
# Whether the final part waits for the decision was chosen when the upload started; pass `wait:`
|
|
331
283
|
# only to override it.
|
|
332
|
-
# @
|
|
284
|
+
# @param asset_id [String]
|
|
333
285
|
# @option options [Hash] :part `{data:, part_number:, is_final:}`; part numbers start at 2.
|
|
334
286
|
# @option options [Boolean] :wait `true` to wait for the decision, `false` to return with the
|
|
335
287
|
# asset `queued`
|
|
336
|
-
# @return [
|
|
337
|
-
def upload_part(options)
|
|
338
|
-
call(options, __method__) do |api, request|
|
|
288
|
+
# @return [Asset] `classification.status` stays `uploading` until the final part.
|
|
289
|
+
def upload_part(asset_id, **options)
|
|
290
|
+
call(with_ids(options, asset_id: asset_id), __method__) do |api, request|
|
|
339
291
|
Internal.segment(api['assetId'])
|
|
340
292
|
part = api['part']
|
|
341
293
|
raise Internal.invalid('part is required') unless part.is_a?(Hash)
|
|
@@ -356,6 +308,12 @@ module CalmLens
|
|
|
356
308
|
# Waits between decision checks; a mock client's waits take no real time.
|
|
357
309
|
def sleep(seconds) = @session.pause(seconds)
|
|
358
310
|
|
|
311
|
+
def asset_path(api) = "/assets/#{Internal.segment(api['assetId'])}"
|
|
312
|
+
|
|
313
|
+
def get_asset(asset_id, request_options)
|
|
314
|
+
build(Asset, request('GET', "/assets/#{Internal.segment(asset_id)}", request_options: request_options))
|
|
315
|
+
end
|
|
316
|
+
|
|
359
317
|
def poll_for_decision(asset_id, request_options, timeout)
|
|
360
318
|
deadline = @session.monotonic + timeout
|
|
361
319
|
delay = DECISION_POLL_FIRST_DELAY
|
|
@@ -366,13 +324,13 @@ module CalmLens
|
|
|
366
324
|
|
|
367
325
|
# The outer deadline covers transport retries and the entire response read.
|
|
368
326
|
asset = ::Timeout.timeout(timeout.zero? ? nil : remaining) do
|
|
369
|
-
|
|
327
|
+
get_asset(asset_id, request_options)
|
|
370
328
|
end
|
|
371
329
|
remaining = deadline - @session.monotonic
|
|
372
330
|
break if timeout.positive? && !remaining.positive?
|
|
373
331
|
|
|
374
|
-
processing = asset.
|
|
375
|
-
moderation = asset.
|
|
332
|
+
processing = asset.classification&.status
|
|
333
|
+
moderation = asset.classification&.moderation&.decision
|
|
376
334
|
return asset if SETTLED_STATUSES.include?(processing)
|
|
377
335
|
break unless remaining.positive?
|
|
378
336
|
|
|
@@ -383,7 +341,7 @@ module CalmLens
|
|
|
383
341
|
rescue ::Timeout::Error
|
|
384
342
|
raise TimeoutError.new(SdkErrorCodes::TIMEOUT, "Asset #{asset_id} was not settled after #{timeout}s",
|
|
385
343
|
details: { 'assetId' => asset_id, 'classificationStatus' => processing,
|
|
386
|
-
'
|
|
344
|
+
'moderationDecision' => moderation })
|
|
387
345
|
end
|
|
388
346
|
|
|
389
347
|
def submit(options, request_options, wait)
|
|
@@ -477,7 +435,7 @@ module CalmLens
|
|
|
477
435
|
# Sends parts 2 to `count`. A part the API already has (its response was lost) is skipped, and a
|
|
478
436
|
# final part that finds the upload completed reads the asset back.
|
|
479
437
|
def send_remaining_parts(asset, count, piece, key, request_options)
|
|
480
|
-
id = asset
|
|
438
|
+
id = asset.id
|
|
481
439
|
number = 2
|
|
482
440
|
while number <= count
|
|
483
441
|
final = number == count
|
|
@@ -490,9 +448,7 @@ module CalmLens
|
|
|
490
448
|
number = expected
|
|
491
449
|
next
|
|
492
450
|
end
|
|
493
|
-
if final && e.code == ErrorCodes::UPLOAD_NOT_OPEN
|
|
494
|
-
return retrieve('assetId' => id, 'request_options' => request_options.except(:idempotency_key))
|
|
495
|
-
end
|
|
451
|
+
return get_asset(id, request_options.except(:idempotency_key)) if final && e.code == ErrorCodes::UPLOAD_NOT_OPEN
|
|
496
452
|
|
|
497
453
|
raise
|
|
498
454
|
end
|
|
@@ -536,14 +492,16 @@ module CalmLens
|
|
|
536
492
|
{ 'partNumber' => 1, 'isFinal' => final, 'totalSizeInBytes' => total }
|
|
537
493
|
end
|
|
538
494
|
|
|
539
|
-
# Like the API's asset responses: an ID
|
|
495
|
+
# Like the API's asset responses: an ID, string statuses for the upload and classification, and a
|
|
496
|
+
# moderation decision, null until moderation decides.
|
|
540
497
|
def expect_asset(data)
|
|
541
498
|
valid = data.is_a?(Hash) && data['id'].is_a?(String) && !data['id'].empty? &&
|
|
542
499
|
data['upload'].is_a?(Hash) && data['upload']['status'].is_a?(String) &&
|
|
543
500
|
data['classification'].is_a?(Hash) && data['classification']['status'].is_a?(String) &&
|
|
544
501
|
data.dig('classification', 'moderation').is_a?(Hash) &&
|
|
545
|
-
data.dig('classification', 'moderation'
|
|
546
|
-
|
|
502
|
+
data.dig('classification', 'moderation').key?('decision') &&
|
|
503
|
+
[String, NilClass].include?(data.dig('classification', 'moderation', 'decision').class)
|
|
504
|
+
return build(Asset, data) if valid
|
|
547
505
|
|
|
548
506
|
raise InvalidResponseError.new(SdkErrorCodes::INVALID_RESPONSE, 'Expected an asset response', status: 200)
|
|
549
507
|
end
|
data/lib/calmlens/cdn.rb
CHANGED
|
@@ -71,7 +71,7 @@ module CalmLens
|
|
|
71
71
|
# already decided the viewer may see age-restricted content. Your platform confirms the viewer's
|
|
72
72
|
# age; viewer claims are assertions from your app, and CalmLens doesn't verify them.
|
|
73
73
|
# @return [String]
|
|
74
|
-
def get_asset_url(options)
|
|
74
|
+
def get_asset_url(**options)
|
|
75
75
|
call(options, __method__) { |api, _request| asset_url(api) }
|
|
76
76
|
end
|
|
77
77
|
|
|
@@ -89,8 +89,8 @@ module CalmLens
|
|
|
89
89
|
# @option options [String] :lens
|
|
90
90
|
# @option options [String, Hash] :transform
|
|
91
91
|
# @option options [Hash] :viewer_claims
|
|
92
|
-
# @return [
|
|
93
|
-
def sign_asset_url(options)
|
|
92
|
+
# @return [SignedAssetUrl] `url` and `expires_at`, in milliseconds since the Unix epoch
|
|
93
|
+
def sign_asset_url(**options)
|
|
94
94
|
call(options, __method__) do |api, request|
|
|
95
95
|
check_signing(api)
|
|
96
96
|
project = Internal.segment(api.fetch('projectId', @session.project_id))
|
|
@@ -102,7 +102,7 @@ module CalmLens
|
|
|
102
102
|
raise InvalidResponseError.new(SdkErrorCodes::INVALID_RESPONSE, 'The API returned no signed asset URL')
|
|
103
103
|
end
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
build(SignedAssetUrl, signed.merge('url' => append_location(signed['url'], api)))
|
|
106
106
|
end
|
|
107
107
|
end
|
|
108
108
|
|
|
@@ -113,13 +113,13 @@ module CalmLens
|
|
|
113
113
|
# @param url [String] an asset URL from {#get_asset_url} or {#sign_asset_url}, without a resource
|
|
114
114
|
# @param viewer_claims [Hash, nil] the viewer's claims, as for {#get_asset_url}; the URL's own
|
|
115
115
|
# `viewerClaims` when omitted
|
|
116
|
-
# @return [
|
|
116
|
+
# @return [AssetInfo]
|
|
117
117
|
# @raise [APIError] with the CDN's code, such as `signed-url/expired` or `asset/not-available`
|
|
118
118
|
def get_asset_info(url, viewer_claims: nil)
|
|
119
119
|
base, claims = split_asset_url(url)
|
|
120
120
|
claims = Internal.normalize(viewer_claims) unless viewer_claims.nil?
|
|
121
121
|
info = @session.cdn_json("#{base}/info#{viewer_claims_query(claims)}")
|
|
122
|
-
return info if asset_info?(info)
|
|
122
|
+
return AssetInfo.new(info) if asset_info?(info)
|
|
123
123
|
|
|
124
124
|
raise InvalidResponseError.new(SdkErrorCodes::INVALID_RESPONSE, 'The CDN returned no asset info')
|
|
125
125
|
rescue TypeError, ArgumentError, JSON::GeneratorError, EncodingError, URI::Error => e
|
|
@@ -150,7 +150,7 @@ module CalmLens
|
|
|
150
150
|
# response.each_chunk { |chunk| file.write(chunk) }
|
|
151
151
|
# end
|
|
152
152
|
# end
|
|
153
|
-
def serve(options, &)
|
|
153
|
+
def serve(**options, &)
|
|
154
154
|
method, url, headers, request = call(options, __method__) do |api, request_options|
|
|
155
155
|
init = api.fetch('init', {})
|
|
156
156
|
raise Internal.invalid('init must be a Hash') unless init.is_a?(Hash)
|
|
@@ -251,4 +251,21 @@ module CalmLens
|
|
|
251
251
|
info['content'].is_a?(Hash) && %w[available age-restricted unavailable].include?(info['availability'])
|
|
252
252
|
end
|
|
253
253
|
end
|
|
254
|
+
|
|
255
|
+
# The display info of an asset URL, from {CDN#get_asset_info}: the asset's type, its content's
|
|
256
|
+
# size and files, and whether the viewer its claims describe can see it now (`availability`:
|
|
257
|
+
# `available`, `age-restricted` or `unavailable`).
|
|
258
|
+
class AssetInfo < Model
|
|
259
|
+
# The content's format, size, page count and files.
|
|
260
|
+
class Content < Model
|
|
261
|
+
declare(format: ['format', nil], width: ['width', nil], height: ['height', nil],
|
|
262
|
+
duration_seconds: ['durationSeconds', nil], page_count: ['pageCount', nil],
|
|
263
|
+
rendered: ['rendered', [:model, 'CalmLens::Asset::Content::Rendered']],
|
|
264
|
+
preview: ['preview', [:model, 'CalmLens::Asset::Content::Preview']],
|
|
265
|
+
hls: ['hls', [:model, 'CalmLens::Asset::Content::Hls']])
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
declare(id: ['id', nil], project_id: ['projectId', nil], type: ['type', nil], external_url: ['externalUrl', nil],
|
|
269
|
+
content: ['content', [:model, 'CalmLens::AssetInfo::Content']], availability: ['availability', nil])
|
|
270
|
+
end
|
|
254
271
|
end
|
data/lib/calmlens/error_codes.rb
CHANGED
|
@@ -70,9 +70,6 @@ module CalmLens
|
|
|
70
70
|
ASSET_NOT_RETRYABLE = 'asset/not-retryable'
|
|
71
71
|
ASSET_CONTENT_UNAVAILABLE = 'asset/content-unavailable'
|
|
72
72
|
ASSET_CONTENT_PURGED = 'asset/content-purged'
|
|
73
|
-
ASSET_NOT_DECIDED = 'asset/not-decided'
|
|
74
|
-
ASSET_POLICY_CHANGED = 'asset/policy-changed'
|
|
75
|
-
ASSET_REVIEWED = 'asset/reviewed'
|
|
76
73
|
SUPPORT_TICKET_NOT_FOUND = 'support-ticket/not-found'
|
|
77
74
|
DUPLICATE_EXTERNAL_ID = 'asset/duplicate-external-id'
|
|
78
75
|
FOLDER_NOT_FOUND = 'folder/not-found'
|
|
@@ -100,7 +97,7 @@ module CalmLens
|
|
|
100
97
|
UPLOAD_PRESET_DISABLED = 'upload-preset/disabled'
|
|
101
98
|
UPLOAD_PRESET_TOKEN_REQUIRED = 'upload-preset/token-required'
|
|
102
99
|
REMOTE_URL_NOT_ALLOWED = 'remote-url/not-allowed'
|
|
103
|
-
|
|
100
|
+
UPLOAD_RATE_LIMIT_REACHED = 'upload/rate-limit-reached'
|
|
104
101
|
UPLOAD_PRESET_DAILY_LIMIT_REACHED = 'upload-preset/daily-limit-reached'
|
|
105
102
|
UPLOAD_PRESET_ORIGIN_NOT_ALLOWED = 'upload-preset/origin-not-allowed'
|
|
106
103
|
UPLOAD_PRESET_FIELD_NOT_ALLOWED = 'upload-preset/field-not-allowed'
|
|
@@ -193,9 +190,6 @@ module CalmLens
|
|
|
193
190
|
'asset/not-retryable' => 409,
|
|
194
191
|
'asset/content-unavailable' => 409,
|
|
195
192
|
'asset/content-purged' => 409,
|
|
196
|
-
'asset/not-decided' => 409,
|
|
197
|
-
'asset/policy-changed' => 409,
|
|
198
|
-
'asset/reviewed' => 409,
|
|
199
193
|
'support-ticket/not-found' => 404,
|
|
200
194
|
'asset/duplicate-external-id' => 409,
|
|
201
195
|
'folder/not-found' => 404,
|
|
@@ -223,7 +217,7 @@ module CalmLens
|
|
|
223
217
|
'upload-preset/disabled' => 403,
|
|
224
218
|
'upload-preset/token-required' => 401,
|
|
225
219
|
'remote-url/not-allowed' => 403,
|
|
226
|
-
'upload/
|
|
220
|
+
'upload/rate-limit-reached' => 429,
|
|
227
221
|
'upload-preset/daily-limit-reached' => 429,
|
|
228
222
|
'upload-preset/origin-not-allowed' => 403,
|
|
229
223
|
'upload-preset/field-not-allowed' => 400,
|