Class: Skyfall::Firehose::Message Abstract
- Inherits:
-
Object
- Object
- Skyfall::Firehose::Message
- Defined in:
- lib/skyfall/firehose/message.rb
Overview
Abstract base class representing a CBOR firehose message.
Actual messages are returned as instances of one of the subclasses of this class, depending on the type of message, most commonly as CommitMessage.
The Message.new method is overridden here so that it can be called with a binary data message from the websocket, and it parses the type from the appropriate frame and builds an instance of a matching subclass.
You normally don’t need to call this class directly, unless you’re building a custom subclass of Stream, or reading raw data packets from the websocket through the Stream#on_raw_message event handler.
Direct Known Subclasses
AccountMessage, CommitMessage, IdentityMessage, InfoMessage, LabelsMessage, SyncMessage, UnknownMessage
Instance Attribute Summary collapse
-
#data_object ⇒ Hash
readonly
private
Second of the two CBOR objects forming the message payload, which contains the rest of the data.
-
#did ⇒ String?
(also: #repo)
readonly
DID of the account (repo) that the event is sent by.
-
#seq ⇒ Integer?
readonly
Sequential number of the message, to be used as a cursor when reconnecting.
-
#type ⇒ Symbol
(also: #kind)
readonly
Type of the message (e.g.
:commit,:identityetc.). -
#type_object ⇒ Hash
readonly
private
First of the two CBOR objects forming the message payload, which mostly just includes the type field.
Class Method Summary collapse
-
.new(data) ⇒ Skyfall::Firehose::Message
Parses the CBOR objects from the binary data and returns an instance of an appropriate subclass.
Instance Method Summary collapse
-
#initialize(type_object, data_object) ⇒ Message
constructor
A new instance of Message.
-
#inspect ⇒ String
Returns a string with a representation of the object for debugging purposes.
-
#operations ⇒ Array<Firehose::Operation>
List of operations on records included in the message.
-
#time ⇒ Time?
Timestamp decoded from the message.
-
#unknown? ⇒ Boolean
True if the message is UnknownMessage (of unrecognized type).
Constructor Details
#initialize(type_object, data_object) ⇒ Message
Returns a new instance of Message.
93 94 95 96 97 98 99 100 |
# File 'lib/skyfall/firehose/message.rb', line 93 def initialize(type_object, data_object) @type_object = type_object @data_object = data_object @type = @type_object['t'][1..-1].to_sym @did = @data_object['repo'] || @data_object['did'] @seq = @data_object['seq'] end |
Instance Attribute Details
#data_object ⇒ Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Second of the two CBOR objects forming the message payload, which contains the rest of the data.
52 53 54 |
# File 'lib/skyfall/firehose/message.rb', line 52 def data_object @data_object end |
#did ⇒ String? (readonly) Also known as: repo
DID of the account (repo) that the event is sent by.
35 36 37 |
# File 'lib/skyfall/firehose/message.rb', line 35 def did @did end |
#seq ⇒ Integer? (readonly)
Sequential number of the message, to be used as a cursor when reconnecting.
39 40 41 |
# File 'lib/skyfall/firehose/message.rb', line 39 def seq @seq end |
#type ⇒ Symbol (readonly) Also known as: kind
Type of the message (e.g. :commit, :identity etc.)
31 32 33 |
# File 'lib/skyfall/firehose/message.rb', line 31 def type @type end |
#type_object ⇒ Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
First of the two CBOR objects forming the message payload, which mostly just includes the type field.
47 48 49 |
# File 'lib/skyfall/firehose/message.rb', line 47 def type_object @type_object end |
Class Method Details
.new(data) ⇒ Skyfall::Firehose::Message
Parses the CBOR objects from the binary data and returns an instance of an appropriate subclass.
UnknownMessage is returned if the message type is not recognized.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/skyfall/firehose/message.rb', line 65 def self.new(data) type_object, data_object = decode_cbor_objects(data) = case type_object['t'] when '#account' then Firehose::AccountMessage when '#commit' then Firehose::CommitMessage when '#identity' then Firehose::IdentityMessage when '#info' then Firehose::InfoMessage when '#labels' then Firehose::LabelsMessage when '#sync' then Firehose::SyncMessage else Firehose::UnknownMessage end if self != Firehose::Message && self != expected_type = self.name.split('::').last.gsub(/Message$/, '').downcase raise DecodeError, "Expected ##{expected_type} message, got #{type_object['t']}" end = .allocate .send(:initialize, type_object, data_object) end |
Instance Method Details
#inspect ⇒ String
Returns a string with a representation of the object for debugging purposes.
142 143 144 145 |
# File 'lib/skyfall/firehose/message.rb', line 142 def inspect vars = inspectable_variables.map { |v| "#{v}=#{instance_variable_get(v).inspect}" }.join(", ") "#<#{self.class}:0x#{object_id} #{vars}>" end |
#operations ⇒ Array<Firehose::Operation>
List of operations on records included in the message. Only #commit messages include operations, but for convenience the method is declared here and returns an empty array in other messages.
108 109 110 |
# File 'lib/skyfall/firehose/message.rb', line 108 def operations [] end |
#time ⇒ Time?
Timestamp decoded from the message.
Note: this represents the time when the message was emitted from the original PDS, which might differ a lot from the created_at time saved in the record data, e.g. if user’s local time is set incorrectly, or if an archive of existing posts was imported from another platform.
128 129 130 |
# File 'lib/skyfall/firehose/message.rb', line 128 def time @time ||= @data_object['time'] && Time.iso8601(@data_object['time']) end |
#unknown? ⇒ Boolean
Returns true if the message is UnknownMessage (of unrecognized type).
115 116 117 |
# File 'lib/skyfall/firehose/message.rb', line 115 def unknown? self.is_a?(Firehose::UnknownMessage) end |