Class: Cola::Envelope

Inherits:
Object
  • Object
show all
Defined in:
lib/cola/envelope.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, version: 1, timestamp: Time.now, retries: 0, last_reason: nil, uuid: SecureRandom.uuid, ttl: 0) ⇒ Envelope

Returns a new instance of Envelope.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cola/envelope.rb', line 11

def initialize(message, 
				version: 1, 
				timestamp: Time.now, 
				retries: 0, 
				last_reason: nil, 
				uuid: SecureRandom.uuid,
				ttl: 0)
	@message = message
	@timestamp = timestamp
	@retries = retries
	@last_reason = last_reason
	@version = version
	@uuid = uuid
	@ttl = ttl 
end

Instance Attribute Details

#last_reasonObject (readonly)

Returns the value of attribute last_reason.



6
7
8
# File 'lib/cola/envelope.rb', line 6

def last_reason
  @last_reason
end

#messageObject (readonly)

Returns the value of attribute message.



3
4
5
# File 'lib/cola/envelope.rb', line 3

def message
  @message
end

#retriesObject (readonly)

Returns the value of attribute retries.



5
6
7
# File 'lib/cola/envelope.rb', line 5

def retries
  @retries
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



4
5
6
# File 'lib/cola/envelope.rb', line 4

def timestamp
  @timestamp
end

#ttlObject

in seconds



9
10
11
# File 'lib/cola/envelope.rb', line 9

def ttl
  @ttl
end

#uuidObject (readonly)

Returns the value of attribute uuid.



8
9
10
# File 'lib/cola/envelope.rb', line 8

def uuid
  @uuid
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/cola/envelope.rb', line 7

def version
  @version
end

Class Method Details

.from_payload(payload) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cola/envelope.rb', line 27

def self.from_payload(payload)
	return nil if payload.nil? 
	
	obj = JSON.parse(payload)

	return Cola::Envelope.new(
		obj['message'], 
		version: obj['version'],
		timestamp: Time.parse(obj['timestamp']),
		retries: obj['retries'],
		uuid: obj['uuid'],
		ttl: obj['ttl'],
		last_reason: obj['last_reason'])
rescue TypeError => exc 
	raise ::Cola::MessageError, 'malformed message'
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/cola/envelope.rb', line 49

def expired?
	return false if self.ttl == 0

	return Time.now > self.timestamp + self.ttl 
end

#inc_retries(reason: nil) ⇒ Object



44
45
46
47
# File 'lib/cola/envelope.rb', line 44

def inc_retries(reason: nil)
	@retries = @retries + 1
	@last_reason = reason if reason
end

#to_jsonObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cola/envelope.rb', line 55

def to_json 
	{
		version: @version,
		message: @message,
		timestamp: @timestamp.iso8601,
		retries: @retries,
		uuid: @uuid,
		ttl: @ttl, 
		last_reason: @last_reason
	}.to_json 
end