Class: Skyfall::Firehose::Message Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/skyfall/firehose/message.rb

Overview

This class is abstract.

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_object, data_object) ⇒ Message

Returns a new instance of Message.

Parameters:

  • first decoded CBOR frame with metadata

  • second decoded CBOR frame with payload



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_objectHash (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.

Returns:

API:

  • private



52
53
54
# File 'lib/skyfall/firehose/message.rb', line 52

def data_object
  @data_object
end

#didString? (readonly) Also known as: repo

DID of the account (repo) that the event is sent by.

Returns:



35
36
37
# File 'lib/skyfall/firehose/message.rb', line 35

def did
  @did
end

#seqInteger? (readonly)

Sequential number of the message, to be used as a cursor when reconnecting.

Returns:



39
40
41
# File 'lib/skyfall/firehose/message.rb', line 39

def seq
  @seq
end

#typeSymbol (readonly) Also known as: kind

Type of the message (e.g. :commit, :identity etc.)

Returns:



31
32
33
# File 'lib/skyfall/firehose/message.rb', line 31

def type
  @type
end

#type_objectHash (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.

Returns:

API:

  • private



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.

Parameters:

  • binary payload of a firehose websocket message

Returns:

Raises:

  • if the structure of the message is invalid

  • if the message has an unknown future version

  • if the data contains an error message from the server



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)

  message_class = 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 != message_class
    expected_type = self.name.split('::').last.gsub(/Message$/, '').downcase
    raise DecodeError, "Expected ##{expected_type} message, got #{type_object['t']}"
  end

  message = message_class.allocate
  message.send(:initialize, type_object, data_object)
  message
end

Instance Method Details

#inspectString

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

Returns:



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

#operationsArray<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.

Returns:



108
109
110
# File 'lib/skyfall/firehose/message.rb', line 108

def operations
  []
end

#timeTime?

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.

Returns:



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).

Returns:



115
116
117
# File 'lib/skyfall/firehose/message.rb', line 115

def unknown?
  self.is_a?(Firehose::UnknownMessage)
end