Class: Pebbles::River::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/pebbles/river/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, delivery_info, queue = nil) ⇒ Message



31
32
33
34
35
36
# File 'lib/pebbles/river/message.rb', line 31

def initialize(content, delivery_info, queue = nil)
  @queue = queue
  @delivery_info = delivery_info
  @payload = self.class.deserialize_payload(content)
  @replied = false
end

Instance Attribute Details

#delivery_infoObject (readonly)

Returns the value of attribute delivery_info.



19
20
21
# File 'lib/pebbles/river/message.rb', line 19

def delivery_info
  @delivery_info
end

#payloadObject (readonly)

Returns the value of attribute payload.



17
18
19
# File 'lib/pebbles/river/message.rb', line 17

def payload
  @payload
end

#queueObject (readonly)

Returns the value of attribute queue.



18
19
20
# File 'lib/pebbles/river/message.rb', line 18

def queue
  @queue
end

Class Method Details

.deserialize_payload(content) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/pebbles/river/message.rb', line 21

def self.deserialize_payload(content)
  if content
    begin
      return JSON.parse(content)
    rescue => e
      raise InvalidPayloadError.new(e.message, content)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
41
42
# File 'lib/pebbles/river/message.rb', line 38

def ==(other)
  other &&
    other.is_a?(Message) &&
    other.payload == @payload
end

#ackObject



48
49
50
51
52
53
# File 'lib/pebbles/river/message.rb', line 48

def ack
  if !@replied && (tag = delivery_tag)
    @queue.channel.ack(tag)
    @replied = true
  end
end

#delivery_tagObject



44
45
46
# File 'lib/pebbles/river/message.rb', line 44

def delivery_tag
  @delivery_info.delivery_tag if @delivery_info
end

#nackObject



55
56
57
58
59
60
# File 'lib/pebbles/river/message.rb', line 55

def nack
  if !@replied && (tag = delivery_tag)
    @queue.channel.nack(tag, false, true)
    @replied = true
  end
end