calmlens 0.27.6 → 0.28.1
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/lib/calmlens/cdn.rb +30 -10
- data/lib/calmlens/model.rb +14 -0
- data/lib/calmlens/option_keys.rb +1 -1
- data/lib/calmlens/testing/fake_api.rb +55 -15
- data/lib/calmlens/testing/mock_decision.rb +74 -0
- data/lib/calmlens/testing/test_input_scenario.rb +1 -1
- data/lib/calmlens/testing/test_inputs.rb +1 -1
- data/lib/calmlens/testing/url_asset_types.rb +49 -0
- data/lib/calmlens/testing.rb +2 -2
- data/lib/calmlens/version.rb +1 -1
- data/sig/calmlens.rbs +20 -2
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 94b8637150fe6233d51968439ef4e67ff9d99a981c80fa3a25d3103e4852a19d
|
|
4
|
+
data.tar.gz: ef01648eb2f0adbd02746a82134ec6681ebe17126ca985c0b54710fdb4829374
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 54dc330aeb4777824aae12955e14814fc50299451cf2d650cbf5af45bda7ee66fa26954793f0f4b281c88b9b005b84da0f2c95cf6083ca03b0981538fdd9cb6b
|
|
7
|
+
data.tar.gz: 2d3f8a12badc9cc39103c8672beca83f93ad380e3cc395ac0bcf5661beecdabcbcf7aac885bf3d2661da386b7a507660acbca6a4d4661312b66917c8d1e5f8a2
|
data/lib/calmlens/cdn.rb
CHANGED
|
@@ -76,16 +76,15 @@ module CalmLens
|
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
# Signs a private asset's URL on your server, so browsers and apps can load it without an API key
|
|
79
|
-
# until it expires.
|
|
80
|
-
#
|
|
81
|
-
# grant
|
|
79
|
+
# until it expires. With `resource`, it is signed for that resource and ends in it; only a URL
|
|
80
|
+
# signed for `original` serves the exact upload, and any other serves every other resource beneath
|
|
81
|
+
# its grant. Lenses, transforms and viewer claims are appended as {#get_asset_url} appends them and
|
|
82
82
|
# aren't part of the signature.
|
|
83
83
|
# @option options [String] :asset_id give it or `external_id`, not both
|
|
84
84
|
# @option options [String] :external_id
|
|
85
85
|
# @option options [String] :project_id the client's by default
|
|
86
86
|
# @option options [Integer] :expires_in whole seconds from 1 to 86400; 900 by default
|
|
87
|
-
# @option options [
|
|
88
|
-
# @option options [String] :resource see {#get_asset_url}
|
|
87
|
+
# @option options [String] :resource the resource the URL is signed for and ends in; see {#get_asset_url}
|
|
89
88
|
# @option options [String] :lens
|
|
90
89
|
# @option options [String, Hash] :transform
|
|
91
90
|
# @option options [Hash] :viewer_claims
|
|
@@ -96,13 +95,13 @@ module CalmLens
|
|
|
96
95
|
project = Internal.segment(api.fetch('projectId', @session.project_id))
|
|
97
96
|
body = api.slice('assetId', 'externalId')
|
|
98
97
|
body['expiresIn'] = api.fetch('expiresIn', DEFAULT_SIGNED_URL_EXPIRES_IN)
|
|
99
|
-
body['
|
|
98
|
+
body['resource'] = api['resource'] if api.key?('resource')
|
|
100
99
|
signed = request('POST', "/projects/#{project}/signed-asset-urls", body: body, request_options: request)
|
|
101
100
|
unless signed.is_a?(Hash) && signed['url'].is_a?(String) && !signed['url'].empty? && signed['expiresAt'].is_a?(Integer)
|
|
102
101
|
raise InvalidResponseError.new(SdkErrorCodes::INVALID_RESPONSE, 'The API returned no signed asset URL')
|
|
103
102
|
end
|
|
104
103
|
|
|
105
|
-
build(SignedAssetUrl, signed.merge('url' =>
|
|
104
|
+
build(SignedAssetUrl, signed.merge('url' => append_signed_location(signed['url'], api)))
|
|
106
105
|
end
|
|
107
106
|
end
|
|
108
107
|
|
|
@@ -110,7 +109,7 @@ module CalmLens
|
|
|
110
109
|
# size, rendered file, preview and stream, and whether the viewer its claims describe can see it
|
|
111
110
|
# now (`availability`: `available`, `age-restricted` or `unavailable`). It sends no API key, as a
|
|
112
111
|
# browser would, so a private asset's info needs a signed URL.
|
|
113
|
-
# @param url [String] an asset URL from {#get_asset_url} or {#sign_asset_url},
|
|
112
|
+
# @param url [String] an asset URL from {#get_asset_url} or {#sign_asset_url}, or a resource URL beneath one
|
|
114
113
|
# @param viewer_claims [Hash, nil] the viewer's claims, as for {#get_asset_url}; the URL's own
|
|
115
114
|
# `viewerClaims` when omitted
|
|
116
115
|
# @return [AssetInfo]
|
|
@@ -203,13 +202,24 @@ module CalmLens
|
|
|
203
202
|
"#{url}#{viewer_claims_query(api['viewerClaims'])}"
|
|
204
203
|
end
|
|
205
204
|
|
|
205
|
+
# The lens, transform and viewer claims added to a URL the API signed for `api['resource']`,
|
|
206
|
+
# which already ends in that resource.
|
|
207
|
+
def append_signed_location(url, api)
|
|
208
|
+
resource_path = api['resource'] ? "/#{RESOURCE_PATHS.fetch(api['resource'])}" : ''
|
|
209
|
+
unless url.end_with?(resource_path)
|
|
210
|
+
raise InvalidResponseError.new(SdkErrorCodes::INVALID_RESPONSE,
|
|
211
|
+
"The signed asset URL doesn't end in the resource it was signed for")
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
url + append_location('', api)[resource_path.length..]
|
|
215
|
+
end
|
|
216
|
+
|
|
206
217
|
# Checks sign_asset_url's own options as the API does, then the location, before anything is sent.
|
|
207
218
|
def check_signing(api)
|
|
208
219
|
expires = api.fetch('expiresIn', DEFAULT_SIGNED_URL_EXPIRES_IN)
|
|
209
220
|
unless expires.is_a?(Integer) && expires.between?(1, MAX_SIGNED_URL_EXPIRES_IN)
|
|
210
221
|
raise Internal.invalid("expires_in must be a whole number of seconds from 1 to #{MAX_SIGNED_URL_EXPIRES_IN}")
|
|
211
222
|
end
|
|
212
|
-
raise Internal.invalid('original must be true or false') unless [true, false].include?(api.fetch('original', false))
|
|
213
223
|
|
|
214
224
|
asset_segment(api)
|
|
215
225
|
append_location('', api)
|
|
@@ -243,7 +253,17 @@ module CalmLens
|
|
|
243
253
|
rescue JSON::ParserError
|
|
244
254
|
raise Internal.invalid("The asset URL's viewerClaims are not JSON")
|
|
245
255
|
end
|
|
246
|
-
[path
|
|
256
|
+
[asset_url_of(path), claims]
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# The asset URL of a path: an asset URL (`…/assets/{projectId}/{asset}`) as it is, and a resource
|
|
260
|
+
# URL beneath one, such as a URL signed for `original`, cut back to the asset URL its resources
|
|
261
|
+
# are paths beneath.
|
|
262
|
+
def asset_url_of(path)
|
|
263
|
+
path = path.sub(%r{/+\z}, '')
|
|
264
|
+
parts = path.split('/', -1)
|
|
265
|
+
assets = parts.rindex('assets')
|
|
266
|
+
assets && assets >= 3 && parts.length > assets + 3 ? parts[0, assets + 3].join('/') : path
|
|
247
267
|
end
|
|
248
268
|
|
|
249
269
|
def asset_info?(info)
|
data/lib/calmlens/model.rb
CHANGED
|
@@ -110,6 +110,20 @@ module CalmLens
|
|
|
110
110
|
rest.empty? || value.nil? ? value : value.dig(*rest)
|
|
111
111
|
end
|
|
112
112
|
|
|
113
|
+
# The value of a field, as Hash#fetch reads one: a field the API didn't send returns the block's
|
|
114
|
+
# value or `default`, or raises KeyError. A field sent as null returns nil.
|
|
115
|
+
# asset.fetch(:description, '') # => '' when the API sent no description
|
|
116
|
+
def fetch(key, *default)
|
|
117
|
+
raise ArgumentError, "wrong number of arguments (given #{default.size + 1}, expected 1..2)" if default.size > 1
|
|
118
|
+
|
|
119
|
+
name = name_of(key)
|
|
120
|
+
return @values[name] if @values.key?(name)
|
|
121
|
+
return yield(key) if block_given?
|
|
122
|
+
return default.first unless default.empty?
|
|
123
|
+
|
|
124
|
+
raise KeyError.new("key not found: #{key.inspect}", receiver: self, key: key)
|
|
125
|
+
end
|
|
126
|
+
|
|
113
127
|
# The values of several fields, by reader name or API key.
|
|
114
128
|
def values_at(*keys) = keys.map { |key| self[key] }
|
|
115
129
|
|
data/lib/calmlens/option_keys.rb
CHANGED
|
@@ -31,7 +31,7 @@ module CalmLens
|
|
|
31
31
|
'assets.update' => %w[assetId description externalId externalUrl metadata name tags visibility].freeze,
|
|
32
32
|
'assets.uploadPart' => %w[assetId part wait].freeze,
|
|
33
33
|
'cdn.signAssetUrl' => %w[
|
|
34
|
-
assetId expiresIn externalId lens
|
|
34
|
+
assetId expiresIn externalId lens projectId resource resource transform viewerClaims
|
|
35
35
|
].freeze,
|
|
36
36
|
'ensureLens' => %w[active description identifier name transform type].freeze,
|
|
37
37
|
'ensureLenses' => %w[lenses].freeze,
|
|
@@ -7,6 +7,7 @@ require 'uri'
|
|
|
7
7
|
require_relative 'placeholder'
|
|
8
8
|
require_relative 'mock_decision'
|
|
9
9
|
require_relative 'rsql'
|
|
10
|
+
require_relative 'url_asset_types'
|
|
10
11
|
|
|
11
12
|
module CalmLens
|
|
12
13
|
module Testing
|
|
@@ -56,6 +57,11 @@ module CalmLens
|
|
|
56
57
|
DEFAULT_PAGE_SIZE = 50
|
|
57
58
|
MAX_PAGE_SIZE = 100
|
|
58
59
|
REVIEW_DECISIONS = %w[allowed limited age-restricted blocked purged].freeze
|
|
60
|
+
# Where the API's signed URL for each resource ends.
|
|
61
|
+
SIGNED_RESOURCE_PATHS = {
|
|
62
|
+
'original' => 'original', 'rendered' => 'rendered', 'preview' => 'preview',
|
|
63
|
+
'hls' => 'hls/index.m3u8', 'embed' => 'embed', 'placeholder' => 'placeholder', 'info' => 'info'
|
|
64
|
+
}.freeze
|
|
59
65
|
# The moderation status each `asset.<status>` event announces.
|
|
60
66
|
ANNOUNCED_STATUSES = REVIEW_DECISIONS.to_h { |status| ["asset.#{status}", status] }.freeze
|
|
61
67
|
MAX_DECISION_NOTE_LENGTH = 1499
|
|
@@ -823,7 +829,7 @@ module CalmLens
|
|
|
823
829
|
end
|
|
824
830
|
|
|
825
831
|
def kind(mime, url)
|
|
826
|
-
return
|
|
832
|
+
return url_asset_type(url) if url
|
|
827
833
|
return mime.split('/').first if mime.start_with?('image/', 'video/', 'audio/')
|
|
828
834
|
return 'plaintext-document' if mime.start_with?('text/')
|
|
829
835
|
return 'multimedia-document' if mime.include?('pdf') || mime.include?('document')
|
|
@@ -831,6 +837,20 @@ module CalmLens
|
|
|
831
837
|
'file'
|
|
832
838
|
end
|
|
833
839
|
|
|
840
|
+
# A URL upload's type: the type of its path's file extension ({MOCK_URL_EXTENSION_TYPES}),
|
|
841
|
+
# compared lower-case without the query or fragment, or `website` for any other extension or
|
|
842
|
+
# none. The mock fetches nothing, so the extension stands in for the fetched content's type.
|
|
843
|
+
def url_asset_type(url)
|
|
844
|
+
path = begin
|
|
845
|
+
URI.parse(url.to_s).path.to_s
|
|
846
|
+
rescue URI::InvalidURIError
|
|
847
|
+
url.to_s.split(/[?#]/, 2).first.to_s
|
|
848
|
+
end
|
|
849
|
+
segment = path.split('/', -1).last.to_s
|
|
850
|
+
dot = segment.rindex('.')
|
|
851
|
+
dot ? MOCK_URL_EXTENSION_TYPES.fetch(segment[(dot + 1)..].downcase, 'website') : 'website'
|
|
852
|
+
end
|
|
853
|
+
|
|
834
854
|
def create_asset(options, queue, url: nil, data: nil, open: false)
|
|
835
855
|
check_upload(options)
|
|
836
856
|
check_folder(options['folderId']) if options['folderId']
|
|
@@ -1075,9 +1095,13 @@ module CalmLens
|
|
|
1075
1095
|
# Without `moderate`, queued work stays pending until completed, which allows it.
|
|
1076
1096
|
def default_moderation(trigger, queued) = queued && trigger != 'complete' ? 'pending' : 'allowed'
|
|
1077
1097
|
|
|
1078
|
-
# Applies `decision` to `asset` with its report. The mock reads
|
|
1079
|
-
#
|
|
1080
|
-
#
|
|
1098
|
+
# Applies `decision` to `asset` with its report. The mock reads nothing in an image, so a
|
|
1099
|
+
# decided image reports what the decision supplies ({MockDecision#with_ocr},
|
|
1100
|
+
# {MockDecision#with_link_detection}, {MockDecision#with_person_detection},
|
|
1101
|
+
# {MockDecision#with_face_detection}), and no people or faces without them, for the features
|
|
1102
|
+
# its upload ran: OCR and person and face detection when turned on, link detection unless
|
|
1103
|
+
# turned off. Like the API, a face has its `ageRange` only when the upload asked for age
|
|
1104
|
+
# estimation.
|
|
1081
1105
|
def decided(asset, decision)
|
|
1082
1106
|
apply_outcome(asset, decision.status, 'policy', decision.report(@now.call))
|
|
1083
1107
|
classification = asset['classification']
|
|
@@ -1086,12 +1110,23 @@ module CalmLens
|
|
|
1086
1110
|
processing = @processing_options[asset['id']]
|
|
1087
1111
|
features = processing.is_a?(Hash) && processing['features'].is_a?(Hash) ? processing['features'] : {}
|
|
1088
1112
|
setting = ->(name) { features[name].is_a?(Hash) ? features[name]['enabled'] : nil }
|
|
1089
|
-
|
|
1113
|
+
if setting.call('ocr') == true
|
|
1114
|
+
ocr = decision.ocr_findings || { 'text' => nil, 'textRegions' => [] }
|
|
1115
|
+
classification['ocr'] = { 'status' => 'evaluated', 'textRegions' => ocr['textRegions'] }
|
|
1116
|
+
if !ocr['text'].nil? && asset['content']['status'] == 'stored'
|
|
1117
|
+
asset['content']['extractedText'] = { 'type' => 'string', 'content' => ocr['text'] }
|
|
1118
|
+
end
|
|
1119
|
+
end
|
|
1090
1120
|
unless setting.call('linkDetection') == false
|
|
1091
|
-
classification['linkDetection'] = { 'status' => 'evaluated'
|
|
1121
|
+
classification['linkDetection'] = { 'status' => 'evaluated' }.merge(decision.link_findings || { 'qrCodes' => [], 'links' => [] })
|
|
1092
1122
|
end
|
|
1093
|
-
|
|
1094
|
-
classification[
|
|
1123
|
+
if setting.call('personDetection') == true
|
|
1124
|
+
classification['personDetection'] = { 'status' => 'evaluated', 'people' => decision.person_findings || [] }
|
|
1125
|
+
end
|
|
1126
|
+
if setting.call('faceDetection') == true
|
|
1127
|
+
ages = features['faceDetection']['ageEstimation'] == true
|
|
1128
|
+
faces = (decision.face_findings || []).map { |face| ages ? face : face.except('ageRange') }
|
|
1129
|
+
classification['faceDetection'] = { 'status' => 'evaluated', 'faces' => faces }
|
|
1095
1130
|
end
|
|
1096
1131
|
asset
|
|
1097
1132
|
end
|
|
@@ -1292,11 +1327,14 @@ module CalmLens
|
|
|
1292
1327
|
end
|
|
1293
1328
|
|
|
1294
1329
|
# Signs an asset URL like the API (`POST /projects/:projectId/signed-asset-urls`): a grant to one
|
|
1295
|
-
# asset, by ID or external ID, until it expires,
|
|
1296
|
-
# mock's CDN accepts the grant.
|
|
1330
|
+
# asset, by ID or external ID, until it expires, ending in the resource it was signed for and
|
|
1331
|
+
# serving the original only when signed for it. Only the mock's CDN accepts the grant.
|
|
1297
1332
|
def sign_asset_url(json)
|
|
1298
|
-
# Like the API's schema,
|
|
1299
|
-
%w[assetId externalId expiresIn
|
|
1333
|
+
# Like the API's schema, it takes only these fields, and none of them takes null.
|
|
1334
|
+
fields = %w[assetId externalId expiresIn resource]
|
|
1335
|
+
unknown = json.keys.find { |key| !fields.include?(key) }
|
|
1336
|
+
invalid!('validation/unknown-field', unknown) if unknown
|
|
1337
|
+
fields.each do |key|
|
|
1300
1338
|
invalid!('validation/invalid-type', key) if json.key?(key) && json[key].nil?
|
|
1301
1339
|
end
|
|
1302
1340
|
if json.key?('assetId') == json.key?('externalId')
|
|
@@ -1308,8 +1346,8 @@ module CalmLens
|
|
|
1308
1346
|
invalid!('validation/invalid-type', 'expiresIn') unless expires.is_a?(Integer) || (expires.is_a?(Float) && expires == expires.floor)
|
|
1309
1347
|
invalid!('validation/too-small', 'expiresIn') if expires < 1
|
|
1310
1348
|
invalid!('validation/too-big', 'expiresIn') if expires > CDN::MAX_SIGNED_URL_EXPIRES_IN
|
|
1311
|
-
|
|
1312
|
-
invalid!('validation/invalid-
|
|
1349
|
+
resource = json['resource']
|
|
1350
|
+
invalid!('validation/invalid-value', 'resource') unless resource.nil? || SIGNED_RESOURCE_PATHS.key?(resource)
|
|
1313
1351
|
|
|
1314
1352
|
item = if json.key?('externalId')
|
|
1315
1353
|
project_assets.find { |asset| asset['externalId'] == json['externalId'] }
|
|
@@ -1320,7 +1358,9 @@ module CalmLens
|
|
|
1320
1358
|
fail!(ErrorCodes::ASSET_HELD) if item['content']['safetyHold']
|
|
1321
1359
|
|
|
1322
1360
|
expires_at = @now.call + (expires.to_i * 1000)
|
|
1323
|
-
|
|
1361
|
+
path = resource ? "/#{SIGNED_RESOURCE_PATHS.fetch(resource)}" : ''
|
|
1362
|
+
grant = MockGrant.encode(item['id'], resource == 'original', expires_at)
|
|
1363
|
+
{ 'url' => "#{@cdn_base_url}/assets/#{Internal.component(@project_id)}/#{grant}#{path}",
|
|
1324
1364
|
'expiresAt' => expires_at }
|
|
1325
1365
|
end
|
|
1326
1366
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'json'
|
|
3
4
|
require_relative 'classification'
|
|
4
5
|
|
|
5
6
|
module CalmLens
|
|
@@ -106,6 +107,79 @@ module CalmLens
|
|
|
106
107
|
self
|
|
107
108
|
end
|
|
108
109
|
|
|
110
|
+
# The text OCR finds in an image, for an upload that turns OCR on: `classification.ocr` reports
|
|
111
|
+
# `text_regions` (each `{ 'box' => { 'left', 'top', 'width', 'height' }, 'personalInformation' }`),
|
|
112
|
+
# and `content.extractedText` holds `text` when the upload keeps its content.
|
|
113
|
+
# @param text [String, nil]
|
|
114
|
+
# @param text_regions [Array<Hash>]
|
|
115
|
+
# @return [self]
|
|
116
|
+
def with_ocr(text, text_regions = [])
|
|
117
|
+
raise Internal.invalid('text must be a String or nil') unless text.nil? || text.is_a?(String)
|
|
118
|
+
raise Internal.invalid('text_regions must be an Array') unless text_regions.is_a?(Array)
|
|
119
|
+
|
|
120
|
+
@ocr = { 'text' => text, 'textRegions' => MockDecision.json_copy(text_regions) }
|
|
121
|
+
self
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The QR codes (`{ 'value', 'box' }`) and links (`{ 'url', 'source' }`, `source` being `text`
|
|
125
|
+
# or `qr-code`) link detection finds in an image, reported as `classification.linkDetection`
|
|
126
|
+
# unless the upload turns link detection off.
|
|
127
|
+
# @param qr_codes [Array<Hash>]
|
|
128
|
+
# @param links [Array<Hash>]
|
|
129
|
+
# @return [self]
|
|
130
|
+
def with_link_detection(qr_codes = [], links = [])
|
|
131
|
+
raise Internal.invalid('qr_codes and links must be Arrays') unless qr_codes.is_a?(Array) && links.is_a?(Array)
|
|
132
|
+
|
|
133
|
+
@link_detection = { 'qrCodes' => MockDecision.json_copy(qr_codes), 'links' => MockDecision.json_copy(links) }
|
|
134
|
+
self
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The people that person detection finds in an image (each `{ 'box' => { 'left', 'top', 'width',
|
|
138
|
+
# 'height' }, 'confidence' }`, as fractions from 0 to 1), reported as
|
|
139
|
+
# `classification.personDetection` when the upload turns person detection on. Without them, the
|
|
140
|
+
# mock finds nobody.
|
|
141
|
+
# @param people [Array<Hash>]
|
|
142
|
+
# @return [self]
|
|
143
|
+
def with_person_detection(people)
|
|
144
|
+
raise Internal.invalid('people must be an Array') unless people.is_a?(Array)
|
|
145
|
+
|
|
146
|
+
@people = MockDecision.json_copy(people)
|
|
147
|
+
self
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The faces that face detection finds in an image (each `{ 'box', 'confidence' }` with an optional
|
|
151
|
+
# `'ageRange' => { 'low', 'high' }`), reported as `classification.faceDetection` when the upload
|
|
152
|
+
# turns face detection on; `ageRange` only when the upload also asks for age estimation. Without
|
|
153
|
+
# them, the mock finds no faces.
|
|
154
|
+
# @param faces [Array<Hash>]
|
|
155
|
+
# @return [self]
|
|
156
|
+
def with_face_detection(faces)
|
|
157
|
+
raise Internal.invalid('faces must be an Array') unless faces.is_a?(Array)
|
|
158
|
+
|
|
159
|
+
@faces = MockDecision.json_copy(faces)
|
|
160
|
+
self
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# What {#with_person_detection} set, or nil.
|
|
164
|
+
# @api private
|
|
165
|
+
def person_findings = @people && MockDecision.json_copy(@people)
|
|
166
|
+
|
|
167
|
+
# What {#with_face_detection} set, or nil.
|
|
168
|
+
# @api private
|
|
169
|
+
def face_findings = @faces && MockDecision.json_copy(@faces)
|
|
170
|
+
|
|
171
|
+
# What {#with_ocr} set, or nil.
|
|
172
|
+
# @api private
|
|
173
|
+
def ocr_findings = @ocr && MockDecision.json_copy(@ocr)
|
|
174
|
+
|
|
175
|
+
# What {#with_link_detection} set, or nil.
|
|
176
|
+
# @api private
|
|
177
|
+
def link_findings = @link_detection && MockDecision.json_copy(@link_detection)
|
|
178
|
+
|
|
179
|
+
# A deep copy with String keys, as the API's JSON has.
|
|
180
|
+
# @api private
|
|
181
|
+
def self.json_copy(value) = JSON.parse(JSON.generate(value))
|
|
182
|
+
|
|
109
183
|
# The report a decided asset gets, as the API sends it: every category (unflagged ones
|
|
110
184
|
# evaluated with score 0), the policy's actions, the automated status and `flaggedCategories`.
|
|
111
185
|
# @param requested_at [Integer] when a pending review joined the queue, in epoch milliseconds
|
|
@@ -9,7 +9,7 @@ module CalmLens
|
|
|
9
9
|
# Checks test input scenarios and writes them as the backend's canonical JSON.
|
|
10
10
|
# @api private
|
|
11
11
|
module Scenario
|
|
12
|
-
ACTIONS = %w[
|
|
12
|
+
ACTIONS = %w[limit age-restrict block].freeze
|
|
13
13
|
REGIONAL_ACTIONS = %w[limit age-restrict block].freeze
|
|
14
14
|
# The qualifiers a scenario may list for each category: all but those the input type sets.
|
|
15
15
|
ALLOWED_QUALIFIERS = MockClassification::QUALIFIERS.transform_values do |list|
|
|
@@ -17,7 +17,7 @@ module CalmLens
|
|
|
17
17
|
# 'instances' => [{ 'category' => 'harassment', 'score' => 0.9, 'action' => 'block', 'qualifiers' => ['harassment-abuse'] }] }`.
|
|
18
18
|
# `instances` (at most 128) and each instance's `qualifiers` (unique qualifiers of its category, at
|
|
19
19
|
# most 64; input-type qualifiers such as `sexual-text` are automatic) are required; `score` (0 to
|
|
20
|
-
# 1) and `action` (`
|
|
20
|
+
# 1) and `action` (`limit`, `age-restrict` or `block`, on every instance or none) are
|
|
21
21
|
# optional. `unassessedCategories` lists categories reported as not assessed, none with instances;
|
|
22
22
|
# `regionalActions` gives up to one `limit`, `age-restrict` or `block` action for each of
|
|
23
23
|
# {REGIONS}. `detections` (optional, `people` and `faces` each optional) sets the person and face
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Generated by packages/sdk-contract/generate.ts from packages/sdk-contract/mockUrlAssetTypes.ts; do not edit.
|
|
4
|
+
# The asset type a mock gives a URL upload: the type of the URL path's file extension (the text
|
|
5
|
+
# after the last dot of its last segment, compared lower-case, without the query or fragment), or
|
|
6
|
+
# website for any other extension or none.
|
|
7
|
+
|
|
8
|
+
module CalmLens
|
|
9
|
+
module Testing
|
|
10
|
+
MOCK_URL_EXTENSION_TYPES = {
|
|
11
|
+
'jpg' => 'image',
|
|
12
|
+
'jpeg' => 'image',
|
|
13
|
+
'png' => 'image',
|
|
14
|
+
'gif' => 'image',
|
|
15
|
+
'webp' => 'image',
|
|
16
|
+
'avif' => 'image',
|
|
17
|
+
'heic' => 'image',
|
|
18
|
+
'heif' => 'image',
|
|
19
|
+
'bmp' => 'image',
|
|
20
|
+
'tif' => 'image',
|
|
21
|
+
'tiff' => 'image',
|
|
22
|
+
'svg' => 'image',
|
|
23
|
+
'mp4' => 'video',
|
|
24
|
+
'm4v' => 'video',
|
|
25
|
+
'mov' => 'video',
|
|
26
|
+
'webm' => 'video',
|
|
27
|
+
'mkv' => 'video',
|
|
28
|
+
'avi' => 'video',
|
|
29
|
+
'mp3' => 'audio',
|
|
30
|
+
'm4a' => 'audio',
|
|
31
|
+
'aac' => 'audio',
|
|
32
|
+
'wav' => 'audio',
|
|
33
|
+
'ogg' => 'audio',
|
|
34
|
+
'oga' => 'audio',
|
|
35
|
+
'opus' => 'audio',
|
|
36
|
+
'flac' => 'audio',
|
|
37
|
+
'pdf' => 'multimedia-document',
|
|
38
|
+
'doc' => 'multimedia-document',
|
|
39
|
+
'docx' => 'multimedia-document',
|
|
40
|
+
'odt' => 'multimedia-document',
|
|
41
|
+
'ppt' => 'multimedia-document',
|
|
42
|
+
'pptx' => 'multimedia-document',
|
|
43
|
+
'odp' => 'multimedia-document',
|
|
44
|
+
'rtf' => 'multimedia-document',
|
|
45
|
+
'txt' => 'plaintext-document',
|
|
46
|
+
'md' => 'plaintext-document'
|
|
47
|
+
}.freeze
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/calmlens/testing.rb
CHANGED
|
@@ -27,7 +27,7 @@ module CalmLens
|
|
|
27
27
|
# for a private asset unless `api_key` comes from {.create_api_key} with an `assets:download`
|
|
28
28
|
# grant (the default key has no grants), or the URL is signed: {CDN#sign_asset_url} returns
|
|
29
29
|
# `{cdn_base_url}/assets/{project_id}/sig_…`, a readable grant that only the mock's CDN accepts,
|
|
30
|
-
# which serves `original` only when signed
|
|
30
|
+
# which serves `original` only when signed for it. {#fetch_cdn} loads such a URL
|
|
31
31
|
# without the API key, as a browser would; without the key or a grant, a private asset answers
|
|
32
32
|
# as an unknown one does, `asset/not-found` (404) whatever its state, including its placeholder
|
|
33
33
|
# and display info. {#decide} applies a reviewer's decision, and {Assets#wait_for_decision} takes no real
|
|
@@ -160,7 +160,7 @@ module CalmLens
|
|
|
160
160
|
# increasing `createdAt` and order predictably. A clock you pass is used as is.
|
|
161
161
|
# @param create_id [#call] receives the prefix of the kind of object (`asset`, `fldr`, `hl`, `hle`, `lens`,
|
|
162
162
|
# `evt` or `rev`) and returns a new ID. The default is like the API's: the prefix, `_` and 20 random base62
|
|
163
|
-
# characters, such as `
|
|
163
|
+
# characters, such as `asset_e5Gm2Rk8XqT3wLp9VbN4`.
|
|
164
164
|
# @param moderate [#call, nil] decides each asset the mock moderates. It receives a
|
|
165
165
|
# {MockModerationInput} (the stored asset, its content, the trigger and whether the API would
|
|
166
166
|
# queue the work) and returns a status from {MockDecision::STATUSES} or a {MockDecision}. It is
|
data/lib/calmlens/version.rb
CHANGED
data/sig/calmlens.rbs
CHANGED
|
@@ -81,6 +81,9 @@ module CalmLens
|
|
|
81
81
|
def []: (Symbol | String key) -> untyped
|
|
82
82
|
def key?: (Symbol | String key) -> bool
|
|
83
83
|
def dig: (Symbol | String key, *untyped rest) -> untyped
|
|
84
|
+
def fetch: (Symbol | String key) -> untyped
|
|
85
|
+
| [T] (Symbol | String key, T default) -> untyped
|
|
86
|
+
| [T] (Symbol | String key) { (Symbol | String key) -> T } -> untyped
|
|
84
87
|
def values_at: (*(Symbol | String) keys) -> Array[untyped]
|
|
85
88
|
def keys: () -> Array[Symbol]
|
|
86
89
|
def to_h: () -> Hash[String, json_value]
|
|
@@ -357,7 +360,7 @@ module CalmLens
|
|
|
357
360
|
# The ISO 3166-1 alpha-2 countries a viewer's `region` can be.
|
|
358
361
|
VIEWER_COUNTRIES: Array[String]
|
|
359
362
|
def get_asset_url: (?asset_id: String, ?external_id: String, ?project_id: String, ?resource: cdn_resource, ?lens: String, ?transform: transform_input, ?viewer_claims: viewer_claims, ?request_options: request_options) -> String
|
|
360
|
-
def sign_asset_url: (?asset_id: String, ?external_id: String, ?project_id: String, ?expires_in: Integer, ?
|
|
363
|
+
def sign_asset_url: (?asset_id: String, ?external_id: String, ?project_id: String, ?expires_in: Integer, ?resource: cdn_resource, ?lens: String, ?transform: transform_input, ?viewer_claims: viewer_claims, ?request_options: request_options) -> SignedAssetUrl
|
|
361
364
|
def get_asset_info: (String url, ?viewer_claims: viewer_claims?) -> AssetInfo
|
|
362
365
|
def serve: (?asset_id: String, ?external_id: String, ?project_id: String, ?viewer_claims: viewer_claims, ?resource: cdn_resource, ?lens: String, ?transform: transform_input, ?init: { ?method: "GET" | "HEAD", ?headers: Hash[String, String] }, ?request_options: request_options) -> Response
|
|
363
366
|
| [T] (?asset_id: String, ?external_id: String, ?project_id: String, ?viewer_claims: viewer_claims, ?resource: cdn_resource, ?lens: String, ?transform: transform_input, ?init: { ?method: "GET" | "HEAD", ?headers: Hash[String, String] }, ?request_options: request_options) { (Response) -> T } -> T
|
|
@@ -543,6 +546,21 @@ module CalmLens
|
|
|
543
546
|
def not_evaluated: (String | Symbol category) -> self
|
|
544
547
|
# Leaves the decision waiting for human review: `sampled`, or `uncertain` with the categories that made it so.
|
|
545
548
|
def pending_review: ("sampled" | "uncertain" | :sampled | :uncertain reason, ?Array[String | Symbol] uncertain_categories) -> self
|
|
549
|
+
# The text OCR finds in an image: `classification.ocr` reports `text_regions`, and
|
|
550
|
+
# `content.extractedText` holds `text` when the upload keeps its content.
|
|
551
|
+
def with_ocr: (String? text, ?Array[Hash[String | Symbol, untyped]] text_regions) -> self
|
|
552
|
+
# The QR codes and links link detection finds in an image (`classification.linkDetection`).
|
|
553
|
+
def with_link_detection: (?Array[Hash[String | Symbol, untyped]] qr_codes, ?Array[Hash[String | Symbol, untyped]] links) -> self
|
|
554
|
+
def ocr_findings: () -> Hash[String, untyped]?
|
|
555
|
+
def link_findings: () -> Hash[String, untyped]?
|
|
556
|
+
# The people that person detection finds in an image (`classification.personDetection`).
|
|
557
|
+
def with_person_detection: (Array[Hash[String | Symbol, untyped]] people) -> self
|
|
558
|
+
# The faces that face detection finds in an image (`classification.faceDetection`); `ageRange`
|
|
559
|
+
# only when the upload asks for age estimation.
|
|
560
|
+
def with_face_detection: (Array[Hash[String | Symbol, untyped]] faces) -> self
|
|
561
|
+
def person_findings: () -> Array[Hash[String, untyped]]?
|
|
562
|
+
def face_findings: () -> Array[Hash[String, untyped]]?
|
|
563
|
+
def self.json_copy: (untyped value) -> untyped
|
|
546
564
|
# The `categories`, `score` and `policy` a decided asset gets; nil for `pending` and `failed`.
|
|
547
565
|
# `requested_at` is when a pending review joined the queue, in epoch milliseconds.
|
|
548
566
|
def report: (?Integer requested_at) -> Hash[String, json_value]?
|
|
@@ -583,7 +601,7 @@ module CalmLens
|
|
|
583
601
|
# Signed test inputs: text codes, test images and media phrases signed with a project's secret
|
|
584
602
|
# test input key from the portal.
|
|
585
603
|
module TestInputs
|
|
586
|
-
type action = "
|
|
604
|
+
type action = "limit" | "age-restrict" | "block"
|
|
587
605
|
type region = moderation_region
|
|
588
606
|
type regional_action = { "region" => region, "action" => "limit" | "age-restrict" | "block" }
|
|
589
607
|
# One qualifying instance; set an action on every instance or on none.
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: calmlens
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.28.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- CalmLens
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: 'Server-side client for the CalmLens API: moderation, uploads, events,
|
|
14
14
|
webhooks and CDN delivery.'
|
|
@@ -50,6 +50,7 @@ files:
|
|
|
50
50
|
- lib/calmlens/testing/test_input_detections.rb
|
|
51
51
|
- lib/calmlens/testing/test_input_scenario.rb
|
|
52
52
|
- lib/calmlens/testing/test_inputs.rb
|
|
53
|
+
- lib/calmlens/testing/url_asset_types.rb
|
|
53
54
|
- lib/calmlens/text.rb
|
|
54
55
|
- lib/calmlens/transform_rules.rb
|
|
55
56
|
- lib/calmlens/transform_schema.rb
|