Class: TgMq::Message

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tg_mq/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seq, chunks, msg_id: Time.now.strftime('%L')) ⇒ Message

Returns a new instance of Message.



11
12
13
14
15
# File 'lib/tg_mq/message.rb', line 11

def initialize(seq, chunks, msg_id: Time.now.strftime('%L'))
  @seq = seq
  @msg_id = msg_id
  @chunks = chunks
end

Class Method Details

.pack(seq, data, msg_id: Time.now.strftime('%L'), chunked: 4080) ⇒ Object



5
6
7
8
9
# File 'lib/tg_mq/message.rb', line 5

def self.pack(seq, data, msg_id: Time.now.strftime('%L'), chunked: 4080)
  (data + Frame::END_MSG).scan(/.{1,#{chunked}}/).each_with_index.map do |chunk, idx|
    Frame.new(seq, msg_id, idx, chunk)
  end
end

Instance Method Details

#eachObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/tg_mq/message.rb', line 17

def each
  if block_given?
    idx = -1
    @chunks.each do |chunk|
      yield(Frame.new(@seq, @msg_id, idx += 1, chunk))
    end
  else
    to_enum(:each)
  end
end