Class: Skyfall::Jetstream::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/skyfall/jetstream/operation.rb

Overview

A single record operation from a Jetstream commit event. An operation is a new record being created, or an existing record modified or deleted. It includes the URI and other details of the record in question, type of the action taken, and record data for "created" and "update" actions.

Note: when a record is deleted, the previous record data is not included in the commit, only its URI. This means that if you're tracking records which are referencing other records, e.g. follow, block, or like records, you need to store information about this referencing record including an URI or rkey, because in case of a delete, you will not get information about which post was unliked or which account was unfollowed, only which like/follow record was deleted.

At the moment, Skyfall doesn't parse the record data into any rich models specific for a given record type with a convenient API, but simply returns them as Hash objects (see #raw_record). In the future, a second #record method might be added which returns a parsed record model.

Instance Method Summary collapse

Constructor Details

#initialize(message, json) ⇒ Operation

Returns a new instance of Operation.

Parameters:



32
33
34
35
# File 'lib/skyfall/jetstream/operation.rb', line 32

def initialize(message, json)
  @message = message
  @json = json
end

Instance Method Details

#actionSymbol

Returns type of the operation (:create, :update or :delete).

Returns:

  • (Symbol)

    type of the operation (:create, :update or :delete)



50
51
52
# File 'lib/skyfall/jetstream/operation.rb', line 50

def action
  @json['operation'].to_sym
end

#cidOxygene::CID?

Returns CID (Content Identifier) of the record (nil for delete operations).

Returns:

  • (Oxygene::CID, nil)

    CID (Content Identifier) of the record (nil for delete operations)



70
71
72
# File 'lib/skyfall/jetstream/operation.rb', line 70

def cid
  @cid ||= @json['cid'] && Oxygene::CID.from_json(@json['cid'])
end

#collectionString

Returns record collection NSID.

Returns:

  • (String)

    record collection NSID



55
56
57
# File 'lib/skyfall/jetstream/operation.rb', line 55

def collection
  @json['collection']
end

#inspectString

Returns a string with a representation of the object for debugging purposes.

Returns:

  • (String)


92
93
94
95
# File 'lib/skyfall/jetstream/operation.rb', line 92

def inspect
  vars = inspectable_variables.map { |v| "#{v}=#{instance_variable_get(v).inspect}" }.join(", ")
  "#<#{self.class}:0x#{object_id} #{vars}>"
end

#pathString

Returns path part of the record URI (collection + rkey).

Returns:

  • (String)

    path part of the record URI (collection + rkey)



45
46
47
# File 'lib/skyfall/jetstream/operation.rb', line 45

def path
  @path ||= "#{@json['collection']}/#{@json['rkey']}"
end

#raw_recordHash?

Returns record data as a plain Ruby Hash (nil for delete operations).

Returns:

  • (Hash, nil)

    record data as a plain Ruby Hash (nil for delete operations)



75
76
77
# File 'lib/skyfall/jetstream/operation.rb', line 75

def raw_record
  @json['record']
end

#repoString Also known as: did

Returns DID of the account/repository in which the operation happened.

Returns:

  • (String)

    DID of the account/repository in which the operation happened



38
39
40
# File 'lib/skyfall/jetstream/operation.rb', line 38

def repo
  @message.repo
end

#rkeyString

Returns record rkey.

Returns:

  • (String)

    record rkey



60
61
62
# File 'lib/skyfall/jetstream/operation.rb', line 60

def rkey
  @json['rkey']
end

#typeSymbol

Symbol short code of the collection, like :bsky_post. If the collection NSID is not recognized, the type is :unknown. The full NSID is always available through the #collection property.

Returns:

  • (Symbol)

See Also:



86
87
88
# File 'lib/skyfall/jetstream/operation.rb', line 86

def type
  Collection.short_code(collection)
end

#uriString

Returns full AT URI of the record.

Returns:

  • (String)

    full AT URI of the record



65
66
67
# File 'lib/skyfall/jetstream/operation.rb', line 65

def uri
  "at://#{repo}/#{collection}/#{rkey}"
end