Class: Collective::Messager::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/collective/messager.rb

Overview


Message contains the body and critical headers for Messager


Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Message

Returns a new instance of Message.



94
95
96
97
98
99
100
101
102
# File 'lib/collective/messager.rb', line 94

def initialize( data )
  data         = ::Collective::Messager.symbolize(data)
  @to          = data[:to] or raise "must specify to address"
  @from        = data[:from] or raise "must specify from address"
  @at          = (data[:at] || Time.now).to_f
  @body        = data[:body]
  @id          = data[:id] || Digest::MD5.hexdigest([from,at,body].join)
  @reply_to_id = data[:reply_to_id]
end

Instance Attribute Details

#atObject (readonly)

timestamp of message generation



89
90
91
# File 'lib/collective/messager.rb', line 89

def at
  @at
end

#bodyObject (readonly)

JSON-compatible



90
91
92
# File 'lib/collective/messager.rb', line 90

def body
  @body
end

#fromObject (readonly)

source host



88
89
90
# File 'lib/collective/messager.rb', line 88

def from
  @from
end

#idObject (readonly)

autogenerated if not supplied



91
92
93
# File 'lib/collective/messager.rb', line 91

def id
  @id
end

#reply_to_idObject (readonly)

optional



92
93
94
# File 'lib/collective/messager.rb', line 92

def reply_to_id
  @reply_to_id
end

#toObject (readonly)

destination host



87
88
89
# File 'lib/collective/messager.rb', line 87

def to
  @to
end

Class Method Details

.parse(json) ⇒ Object



118
119
120
# File 'lib/collective/messager.rb', line 118

def self.parse( json )
    new( JSON.parse(json) )
end

Instance Method Details

#to_hashObject



104
105
106
107
108
# File 'lib/collective/messager.rb', line 104

def to_hash
  blob = { to: to, from: from, at: at, body: body, id: id }
  blob[:reply_to_id] = reply_to_id if reply_to_id
  blob
end

#to_jsonObject



110
111
112
# File 'lib/collective/messager.rb', line 110

def to_json
  to_hash.to_json
end

#to_sObject



114
115
116
# File 'lib/collective/messager.rb', line 114

def to_s
  to_json
end