calmlens 0.29.1 → 0.30.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55a4f4ba7407405cbfa72162ffac349011ffa9e3ebb645fc0bd70b32acf60489
4
- data.tar.gz: 8d452e156dd1ef001bf49b677e10d72a61aa432fdc7e34756365c6bb417f2706
3
+ metadata.gz: 34cc7decf11dccd3df4fc06875ea3652b32d6eacf4321f64953791eacaebfd80
4
+ data.tar.gz: 0ecffa743bc5f7a0df8efd3084c7fe94046dac06c763962b7ce998e360c78e71
5
5
  SHA512:
6
- metadata.gz: 1d2814a8f159bade80c35c1bd5e07434622433d62b754c1ca22c08e7537407a966d897d145e01a61dd0722849c08e6723f793dbaea9ec5c5965129873bd36f7a
7
- data.tar.gz: afc26fe093df1ede24bf413a83db6ce97217b68b93ac681b3fb3301b498db6100666a6cad5236d38bb26cb0687a97ed13364c8be8841574cead4e67d01907904
6
+ metadata.gz: 5ed41fed093ded33d5c6e87e64c76b2a5584a82dfcc4129ee9cdc6b43c3f7bb33709996212e3fc73d51aba7ad107814cd0742146d0ee63dde3941666083ed6ea
7
+ data.tar.gz: be5700de1ea19f67e65f933ee74016d33a61363434b2c5a37d02dbb4279949a845a58ede6b1cd5af63429012e6ad8254bbe030258440e1a49271fc7341262686
@@ -36,9 +36,10 @@ module CalmLens
36
36
  end
37
37
 
38
38
  # Retrieves the asset an event is about, as it is now, such as one from {#list} or a parsed
39
- # webhook delivery; events carry only the asset's ID. It sends the request {Assets#retrieve}
40
- # sends. The asset's `event_sequence` orders it against events: an event with a higher
41
- # `asset_sequence` happened after this state, one with an equal or lower one is reflected in it.
39
+ # webhook delivery; events carry only the asset's ID, in `resource`. It sends the request
40
+ # {Assets#retrieve} sends. The asset's `sequence` orders it against events: an event with a
41
+ # higher `resource.sequence` happened after this state, one with an equal or lower one is
42
+ # reflected in it.
42
43
  # @param event [Event, Hash] an event, or an event or delivery body with string keys
43
44
  # @param request_options [Hash, nil]
44
45
  # @return [Asset, nil] nil for an event that isn't about an asset, without sending a request
@@ -46,9 +47,11 @@ module CalmLens
46
47
  def retrieve_asset(event, request_options: nil)
47
48
  raise Internal.invalid('event must be an Event or a Hash') unless event.is_a?(Event) || event.is_a?(Hash)
48
49
 
49
- asset_id = event['assetId']
50
- return nil if asset_id.nil?
51
- raise Internal.invalid('event assetId must be a String') unless asset_id.is_a?(String)
50
+ resource = event['resource']
51
+ return nil unless (resource.is_a?(Hash) || resource.is_a?(Model)) && resource['kind'] == 'asset'
52
+
53
+ asset_id = resource['id']
54
+ raise Internal.invalid('event resource id must be a String') unless asset_id.is_a?(String)
52
55
 
53
56
  @assets.retrieve(asset_id, request_options: request_options)
54
57
  end
@@ -50,6 +50,7 @@ module CalmLens
50
50
  class Classification::Progress < Model; end
51
51
  class Event::Actor < Model; end
52
52
  class Event::Fact < Model; end
53
+ class Event::Resource < Model; end
53
54
  class MetadataFieldsResource::Field < Model; end
54
55
  class Moderation::ManualReview < Model; end
55
56
  class Moderation::PendingReview < Model; end
@@ -108,7 +109,7 @@ module CalmLens
108
109
  upload: ["upload", [:model, "CalmLens::Asset::Upload"]],
109
110
  classification: ["classification", [:model, "CalmLens::Classification"]],
110
111
  content: ["content", [:model, "CalmLens::Asset::Content"]],
111
- event_sequence: ["eventSequence", nil]
112
+ sequence: ["sequence", nil]
112
113
  )
113
114
 
114
115
  AssetBatch.declare(
@@ -209,8 +210,7 @@ module CalmLens
209
210
  severity: ["severity", nil],
210
211
  title: ["title", nil],
211
212
  description: ["description", nil],
212
- asset_id: ["assetId", nil],
213
- asset_sequence: ["assetSequence", nil],
213
+ resource: ["resource", [:model, "CalmLens::Event::Resource"]],
214
214
  facts: ["facts", [:array, [:model, "CalmLens::Event::Fact"]]],
215
215
  created_at: ["createdAt", nil]
216
216
  )
@@ -489,6 +489,12 @@ module CalmLens
489
489
  url: ["url", nil]
490
490
  )
491
491
 
492
+ Event::Resource.declare(
493
+ kind: ["kind", nil],
494
+ id: ["id", nil],
495
+ sequence: ["sequence", nil]
496
+ )
497
+
492
498
  MetadataFieldsResource::Field.declare(
493
499
  id: ["id", nil],
494
500
  title: ["title", nil],
@@ -174,8 +174,8 @@ module CalmLens
174
174
  @metadata_fields = []
175
175
  @settings_revision = 1
176
176
  @request_count = 0
177
- # Each asset's latest `assetSequence`, which the API keeps on the asset row and returns as its
178
- # `eventSequence`. Kept here as well as on stored assets, which a deleted asset's events outlive.
177
+ # Each asset's latest `resource.sequence`, which the API keeps on the asset row and returns as
178
+ # its `sequence`. Kept here as well as on stored assets, which a deleted asset's events outlive.
179
179
  @asset_event_sequences = Hash.new(0)
180
180
  # The processing options each asset was uploaded with, which the API keeps but doesn't return.
181
181
  @processing_options = {}
@@ -204,20 +204,20 @@ module CalmLens
204
204
  end
205
205
 
206
206
  # Adds an event to the feed, filling the fields the API always sets. Its `actor` is the mock's
207
- # API key unless the event names another.
207
+ # API key unless the event names another. With an `assetId` it is about that asset; without,
208
+ # about no resource.
208
209
  def add_event(event)
209
210
  time = @now.call
211
+ asset_id = event['assetId']
210
212
  record = { 'id' => @create_id.call('evt'), 'projectId' => @project_id, 'actor' => deep_copy(API_KEY_ACTOR),
211
- 'severity' => 'low', 'description' => nil,
212
- 'assetId' => nil, 'assetSequence' => nil, 'facts' => [],
213
- 'createdAt' => time }.merge(event)
213
+ 'severity' => 'low', 'description' => nil, 'facts' => [],
214
+ 'createdAt' => time }.merge(event.except('assetId', 'resource'))
214
215
  record['title'] ||= record['type']
215
216
  # Like the API: each event about an asset takes the asset's next number; others have none.
216
- asset_id = record['assetId']
217
- record['assetSequence'] = asset_id ? (@asset_event_sequences[asset_id] += 1) : nil
218
- # The asset's `eventSequence` follows, so every copy of it returned from now on has it.
217
+ record['resource'] = asset_id ? { 'kind' => 'asset', 'id' => asset_id, 'sequence' => (@asset_event_sequences[asset_id] += 1) } : nil
218
+ # The asset's `sequence` follows, so every copy of it returned from now on has it.
219
219
  stored = asset_id && @state.assets[asset_id]
220
- stored['eventSequence'] = record['assetSequence'] if stored
220
+ stored['sequence'] = record['resource']['sequence'] if stored
221
221
  @state.events << record
222
222
  record
223
223
  end
@@ -673,7 +673,9 @@ module CalmLens
673
673
  asset_id = query['assetId']
674
674
  invalid!('validation/invalid-format', 'assetId') if asset_id && !asset_id.match?(ASSET_ID_PATTERN)
675
675
  filter = [JSON.generate([types.uniq.sort, asset_id])].pack('m0')
676
- matches = ->(event) { (types.empty? || types.include?(event['type'])) && (asset_id.nil? || event['assetId'] == asset_id) }
676
+ matches = lambda { |event|
677
+ (types.empty? || types.include?(event['type'])) && (asset_id.nil? || event.dig('resource', 'id') == asset_id)
678
+ }
677
679
  feed = @state.events
678
680
  unless query['cursor']
679
681
  return { 'items' => [], 'nextCursor' => "mock_#{feed.size}~#{filter}", 'hasMore' => false, 'excludedTypes' => [] }
@@ -969,7 +971,7 @@ module CalmLens
969
971
  'context' => context.empty? ? nil : { 'items' => context }, 'tags' => options['tags'] || [],
970
972
  'visibility' => options.fetch('visibility', 'private'),
971
973
  # Set as the asset's events are added (add_event).
972
- 'eventSequence' => 0,
974
+ 'sequence' => 0,
973
975
  **sections(now: now, open: open, keep: keep, format: mime, size: data&.bytesize) }
974
976
  @state.assets[asset['id']] = asset
975
977
  @processing_options[asset['id']] = processing
@@ -1465,7 +1467,7 @@ module CalmLens
1465
1467
  holder = project_assets.find { |other| other['id'] != item['id'] && other['externalId'] == json['externalId'] }
1466
1468
  duplicate_external_id!(holder) if holder
1467
1469
  end
1468
- touch(item.merge!(json.except('id', 'projectId', 'upload', 'classification', 'content', 'eventSequence')))
1470
+ touch(item.merge!(json.except('id', 'projectId', 'upload', 'classification', 'content', 'sequence')))
1469
1471
  end
1470
1472
 
1471
1473
  # Like the API: the record and report stay; stored content, extracted text, metadata, the
@@ -335,10 +335,12 @@ module CalmLens
335
335
  end
336
336
 
337
337
  # Adds an event to the mock's events feed. Fields you omit (`id`, `projectId`, `createdAt`,
338
- # `severity`, `title`, `facts`, and nil `description` and `assetId`) are filled in, and `actor`
339
- # is the mock API key (`{ 'type' => 'api-key', 'id' => API_KEY_ID }`) unless you pass one, such as
340
- # `actor: { type: 'user', id: nil }`. An event with an `asset_id` takes that asset's next
341
- # `assetSequence`, which becomes the stored asset's `eventSequence`; it changes nothing else.
338
+ # `severity`, `title`, `facts`, and nil `description`) are filled in, and `actor` is the mock
339
+ # API key (`{ 'type' => 'api-key', 'id' => API_KEY_ID }`) unless you pass one, such as
340
+ # `actor: { type: 'user', id: nil }`. An event with an `asset_id` is about that asset: its
341
+ # `resource` is `{ 'kind' => 'asset', 'id' => asset_id, 'sequence' => n }`, with the asset's next
342
+ # number, which becomes the stored asset's `sequence`; it changes nothing else. Without one,
343
+ # its `resource` is nil.
342
344
  # @param event [Hash] at least `type:`; snake_case keys are converted.
343
345
  # @return [Event] the stored event
344
346
  def enqueue_event(event)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CalmLens
4
4
  # The gem version. The gemspec and the User-Agent header read it from here.
5
- VERSION = '0.29.1'
5
+ VERSION = '0.30.0'
6
6
  end
@@ -15,17 +15,18 @@ module CalmLens
15
15
  WEBHOOK_SIGNATURE_TOLERANCE_SECONDS = 300
16
16
 
17
17
  # An event from {Events#list} or a verified webhook delivery ({Webhooks.parse_event}). `type` is
18
- # such as `asset.allowed`; new types may appear, so ignore unknown ones. `asset_sequence`
19
- # increases with each event about the same asset and is nil for events not about an asset;
18
+ # such as `asset.allowed`; new types may appear, so ignore unknown ones. `resource` is the asset,
19
+ # membership or API key the event is about (`kind`, `id`, `sequence`), or nil for events about no
20
+ # resource; new kinds may appear. Its `sequence` increases with each event about the same resource;
20
21
  # deliveries can arrive out of order, see {#newer_than?}. {#to_h} is the body as received.
21
22
  class Event
22
23
  # @return [Boolean] true for a test send from the portal or CLI.
23
24
  def test? = Webhooks.test_event?(self)
24
25
 
25
- # @param last_sequence [Integer, nil] the highest `asset_sequence` applied for this event's asset
26
+ # @param last_sequence [Integer, nil] the highest `resource.sequence` applied for this event's resource
26
27
  # @return [Boolean] whether to apply this event after it.
27
- # @see Webhooks.newer_asset_event?
28
- def newer_than?(last_sequence) = Webhooks.newer_asset_event?(self, last_sequence)
28
+ # @see Webhooks.newer_event?
29
+ def newer_than?(last_sequence) = Webhooks.newer_event?(self, last_sequence)
29
30
  end
30
31
 
31
32
  # Webhook signing and verification. Always verify the exact bytes received: read the raw body
@@ -69,25 +70,25 @@ module CalmLens
69
70
  facts.is_a?(Array) && facts.any? { |fact| (fact.is_a?(Hash) || fact.is_a?(Model)) && fact['key'] == 'test' && fact['value'] == true }
70
71
  end
71
72
 
72
- # Whether to apply `event` after `last_sequence`, the highest `assetSequence` you have applied
73
- # for its asset (nil when none). Deliveries can arrive out of order: an event about an asset is
74
- # newer only with a higher `assetSequence`, so a retried or repeated delivery of an earlier one
75
- # returns false. Events that aren't about an asset have no sequence and are always new. Store
76
- # the highest `assetSequence` applied per `assetId` and ignore lower ones.
73
+ # Whether to apply `event` after `last_sequence`, the highest `resource.sequence` you have applied
74
+ # for its resource (nil when none). Deliveries can arrive out of order: an event about a resource
75
+ # is newer only with a higher sequence, so a retried or repeated delivery of an earlier one
76
+ # returns false. Events about no resource have no sequence and are always new. Store the highest
77
+ # `resource.sequence` applied per `resource.id` and ignore lower ones.
77
78
  # @param event [Event, Hash] a parsed delivery, or a delivery body with string keys
78
79
  # @param last_sequence [Integer, nil]
79
80
  # @return [Boolean]
80
- def newer_asset_event?(event, last_sequence)
81
- sequence = event['assetSequence']
82
- sequence.nil? || last_sequence.nil? || sequence > last_sequence
81
+ def newer_event?(event, last_sequence)
82
+ resource = event['resource']
83
+ resource.nil? || last_sequence.nil? || resource['sequence'] > last_sequence
83
84
  end
84
85
 
85
86
  # Converts an event from {Events#list} to the JSON body of its webhook delivery, which has the
86
- # same fields: `description`, `assetId` and `assetSequence` are nil when absent, and
87
- # `actor` is left out when the event has none.
87
+ # same fields: `description` and `resource` are nil when absent, and `actor` is left out when
88
+ # the event has none.
88
89
  # @param event [Event, Hash] an {Events#list} item, or an event with string keys
89
- # @return [Hash] `{ 'id', 'projectId', 'actor', 'type', 'severity', 'title', 'description', 'assetId',
90
- # 'assetSequence', 'facts', 'createdAt' }`
90
+ # @return [Hash] `{ 'id', 'projectId', 'actor', 'type', 'severity', 'title', 'description', 'resource',
91
+ # 'facts', 'createdAt' }`
91
92
  def to_payload(event)
92
93
  event = event.to_h if event.is_a?(Model)
93
94
  raise Internal.invalid('event must be an Event or a Hash') unless event.is_a?(Hash)
@@ -95,9 +96,8 @@ module CalmLens
95
96
  payload = { 'id' => event['id'], 'projectId' => event['projectId'] }
96
97
  payload['actor'] = event['actor'] if event.key?('actor')
97
98
  payload.merge('type' => event['type'], 'severity' => event['severity'], 'title' => event['title'],
98
- 'description' => event['description'], 'assetId' => event['assetId'],
99
- 'assetSequence' => event['assetSequence'], 'facts' => event['facts'],
100
- 'createdAt' => event['createdAt'])
99
+ 'description' => event['description'], 'resource' => event['resource'],
100
+ 'facts' => event['facts'], 'createdAt' => event['createdAt'])
101
101
  end
102
102
 
103
103
  # Checks a delivery's signature and freshness in constant time. Pass the `signature` and
@@ -171,17 +171,24 @@ module CalmLens
171
171
 
172
172
  optional = ->(key, *types) { !body.key?(key) || types.any? { |type| type.nil? ? body[key].nil? : body[key].is_a?(type) } }
173
173
  optional.call('severity', String) && optional.call('title', String) && optional.call('description', String, nil) &&
174
- optional.call('assetId', String, nil) && optional.call('assetSequence', Numeric, nil) && nested?(body)
174
+ nested?(body)
175
175
  end
176
176
 
177
177
  # The nested fields, when present: `actor` is `{type, id}` with a String type and a String or null
178
- # ID and `facts` lists `{key, value, url?}` entries.
178
+ # ID, `resource` is null or `{kind, id, sequence}` with any String kind, and `facts` lists
179
+ # `{key, value, url?}` entries.
179
180
  def nested?(body)
180
181
  actor = body.fetch('actor', { 'type' => '' })
181
182
  actor.is_a?(Hash) && actor['type'].is_a?(String) && (actor['id'].nil? || actor['id'].is_a?(String)) &&
183
+ resource?(body['resource']) &&
182
184
  (!body.key?('facts') || (body['facts'].is_a?(Array) && body['facts'].all? { |fact| fact?(fact) }))
183
185
  end
184
186
 
187
+ def resource?(resource)
188
+ resource.nil? ||
189
+ (resource.is_a?(Hash) && resource['kind'].is_a?(String) && resource['id'].is_a?(String) && resource['sequence'].is_a?(Numeric))
190
+ end
191
+
185
192
  def fact?(fact)
186
193
  fact.is_a?(Hash) && fact['key'].is_a?(String) && fact.key?('value') && (!fact.key?('url') || fact['url'].is_a?(String))
187
194
  end
@@ -204,7 +211,7 @@ module CalmLens
204
211
  raise Internal.invalid("Webhook payload must be the raw body String, not a #{payload.class}. " \
205
212
  'Read request.raw_post (Rails) or request.body.read (Rack, Sinatra) before parsing.')
206
213
  end
207
- private_class_method :decode_event, :event?, :nested?, :fact?, :header, :raw_body
214
+ private_class_method :decode_event, :event?, :nested?, :resource?, :fact?, :header, :raw_body
208
215
  end
209
216
 
210
217
  # @see Webhooks.signature_header
@@ -228,8 +235,8 @@ module CalmLens
228
235
  # @see Webhooks.test_event?
229
236
  def self.test_webhook_event?(...) = Webhooks.test_event?(...)
230
237
 
231
- # @see Webhooks.newer_asset_event?
232
- def self.newer_asset_event?(...) = Webhooks.newer_asset_event?(...)
238
+ # @see Webhooks.newer_event?
239
+ def self.newer_event?(...) = Webhooks.newer_event?(...)
233
240
 
234
241
  # @see EventFacts.value
235
242
  def self.get_event_fact(...) = EventFacts.value(...)
data/sig/calmlens.rbs CHANGED
@@ -374,7 +374,7 @@ module CalmLens
374
374
  class Event
375
375
  # Whether the event is a test send from the portal or CLI.
376
376
  def test?: () -> bool
377
- # Whether to apply this event after `last_sequence`, the highest `asset_sequence` applied for its asset.
377
+ # Whether to apply this event after `last_sequence`, the highest `resource.sequence` applied for its resource.
378
378
  def newer_than?: (Integer? last_sequence) -> bool
379
379
  end
380
380
 
@@ -425,7 +425,7 @@ module CalmLens
425
425
  def self.parse_event: (payload: String, secret: String, ?signature: String?, ?timestamp: String?, ?headers: webhook_headers?,
426
426
  ?tolerance_seconds: Integer, ?now: number) -> Event
427
427
  def self.test_event?: (Event | Hash[String, untyped] event) -> bool
428
- def self.newer_asset_event?: (Event | Hash[String, untyped] event, Integer? last_sequence) -> bool
428
+ def self.newer_event?: (Event | Hash[String, untyped] event, Integer? last_sequence) -> bool
429
429
  # The value of an event's fact by key (the first, when the event repeats it), or nil without one.
430
430
  def self.get_event_fact: (Event | Hash[String, untyped] event, String key) -> event_fact_value
431
431
  # An event's facts as values by key; a repeated key keeps its first value.
@@ -447,7 +447,7 @@ module CalmLens
447
447
  def self.parse_webhook_event: (payload: String, secret: String, ?signature: String?, ?timestamp: String?,
448
448
  ?headers: webhook_headers?, ?tolerance_seconds: Integer, ?now: number) -> Event
449
449
  def self.test_webhook_event?: (Event | Hash[String, untyped] event) -> bool
450
- def self.newer_asset_event?: (Event | Hash[String, untyped] event, Integer? last_sequence) -> bool
450
+ def self.newer_event?: (Event | Hash[String, untyped] event, Integer? last_sequence) -> bool
451
451
  # The value of an event's fact by key (the first, when the event repeats it), or nil without one.
452
452
  def self.get_event_fact: (Event | Hash[String, untyped] event, String key) -> event_fact_value
453
453
  # An event's facts as values by key; a repeated key keeps its first value.
data/sig/models.rbs CHANGED
@@ -25,7 +25,7 @@ module CalmLens
25
25
  def upload: () -> Asset::Upload
26
26
  def classification: () -> Classification
27
27
  def content: () -> Asset::Content
28
- def event_sequence: () -> Integer
28
+ def sequence: () -> Integer
29
29
  end
30
30
  class AssetBatch < Model
31
31
  def batch_id: () -> String
@@ -114,8 +114,7 @@ module CalmLens
114
114
  def severity: () -> event_severity
115
115
  def title: () -> String
116
116
  def description: () -> String?
117
- def asset_id: () -> String?
118
- def asset_sequence: () -> Integer?
117
+ def resource: () -> Event::Resource?
119
118
  def facts: () -> Array[Event::Fact]
120
119
  def created_at: () -> Integer
121
120
  end
@@ -357,6 +356,11 @@ module CalmLens
357
356
  def value: () -> (Array[classification_category] | String | Integer | bool)?
358
357
  def url: () -> String?
359
358
  end
359
+ class Event::Resource < Model
360
+ def kind: () -> ("asset" | "member" | "api-key")
361
+ def id: () -> String
362
+ def sequence: () -> Integer
363
+ end
360
364
  class MetadataFieldsResource::Field < Model
361
365
  def id: () -> String
362
366
  def title: () -> String
data/sig/types.rbs CHANGED
@@ -398,14 +398,8 @@ module CalmLens
398
398
  ?severity: event_severity,
399
399
  ?title: String,
400
400
  ?description: String?,
401
+ # The asset the event is about; it takes the asset's next `resource.sequence`. Without one, the event is about no resource.
401
402
  ?asset_id: String?,
402
- ?asset: {
403
- id: String,
404
- ?external_id: String?,
405
- upload: { status: "uploading" | "completed" },
406
- classification: { status: asset_classification_status, moderation: { decision: asset_moderation_decision? } },
407
- content: { status: asset_content_status }
408
- }?,
409
403
  ?facts: Array[event_fact_input],
410
404
  ?created_at: Integer
411
405
  }
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.29.1
4
+ version: 0.30.0
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-29 00:00:00.000000000 Z
11
+ date: 2026-11-30 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.'