Class: MTProto::Type::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/type/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_key_id:, msg_id:, body:) ⇒ Message

Returns a new instance of Message.



8
9
10
11
12
# File 'lib/mtproto/type/message.rb', line 8

def initialize(auth_key_id:, msg_id:, body:)
  @auth_key_id = auth_key_id
  @msg_id = msg_id
  @body = body
end

Instance Attribute Details

#auth_key_idObject (readonly)

Returns the value of attribute auth_key_id.



6
7
8
# File 'lib/mtproto/type/message.rb', line 6

def auth_key_id
  @auth_key_id
end

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/mtproto/type/message.rb', line 6

def body
  @body
end

#msg_idObject (readonly)

Returns the value of attribute msg_id.



6
7
8
# File 'lib/mtproto/type/message.rb', line 6

def msg_id
  @msg_id
end

Class Method Details

.deserialize(data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mtproto/type/message.rb', line 22

def self.deserialize(data)
  if data.bytesize < 20
    raise(ArgumentError,
      "Invalid MTProto message: expected at least 20 bytes, got #{data.bytesize} bytes (hex: #{data.unpack1('H*')})",
    )
  end

  auth_key_id = data[0, 8].unpack1('Q<')
  msg_id = data[8, 8].unpack1('Q<')
  body_length = data[16, 4].unpack1('L<')
  body = data[20, body_length]

  new(auth_key_id: auth_key_id, msg_id: msg_id, body: body)
end

Instance Method Details

#serializeObject



14
15
16
17
18
19
20
# File 'lib/mtproto/type/message.rb', line 14

def serialize
  auth_key_id_bytes = [@auth_key_id].pack('Q<')
  msg_id_bytes = [@msg_id].pack('Q<')
  body_length = [@body.bytesize].pack('L<')

  auth_key_id_bytes + msg_id_bytes + body_length + @body
end