Class: Cola::Envelope
- Inherits:
-
Object
- Object
- Cola::Envelope
- Defined in:
- lib/cola/envelope.rb
Instance Attribute Summary collapse
-
#last_reason ⇒ Object
readonly
Returns the value of attribute last_reason.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#ttl ⇒ Object
in seconds.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #expired? ⇒ Boolean
- #inc_retries(reason: nil) ⇒ Object
-
#initialize(message, version: 1, timestamp: Time.now, retries: 0, last_reason: nil, uuid: SecureRandom.uuid, ttl: 0) ⇒ Envelope
constructor
A new instance of Envelope.
- #to_json ⇒ Object
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(, version: 1, timestamp: Time.now, retries: 0, last_reason: nil, uuid: SecureRandom.uuid, ttl: 0) @message = @timestamp = @retries = retries @last_reason = last_reason @version = version @uuid = uuid @ttl = ttl end |
Instance Attribute Details
#last_reason ⇒ Object (readonly)
Returns the value of attribute last_reason.
6 7 8 |
# File 'lib/cola/envelope.rb', line 6 def last_reason @last_reason end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
3 4 5 |
# File 'lib/cola/envelope.rb', line 3 def @message end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
5 6 7 |
# File 'lib/cola/envelope.rb', line 5 def retries @retries end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
4 5 6 |
# File 'lib/cola/envelope.rb', line 4 def @timestamp end |
#ttl ⇒ Object
in seconds
9 10 11 |
# File 'lib/cola/envelope.rb', line 9 def ttl @ttl end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
8 9 10 |
# File 'lib/cola/envelope.rb', line 8 def uuid @uuid end |
#version ⇒ Object (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
49 50 51 52 53 |
# File 'lib/cola/envelope.rb', line 49 def expired? return false if self.ttl == 0 return Time.now > self. + 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_json ⇒ Object
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 |