Class: EventHub::Message

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

Constant Summary collapse

VERSION =
'1.0.0'
REQUIRED_HEADERS =

Headers that are required (value can be nil) in order to pass valid?

[
  'message_id',
  'version',
  'created_at',
  'origin.module_id',
  'origin.type',
  'origin.site_id',
  'process.name',
  'process.step_position',
  'process.execution_id',
  'status.retried_count',
  'status.code',
  'status.message'
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, body, raw = nil) ⇒ Message

process_step_position should be



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/eventhub/message.rb', line 46

def initialize(header, body,raw=nil)

  @header = header || {}
  @body   = body || {}
  @raw    = raw

  # set message defaults, that we have required headers
  now = Time.now
  @header.set('message_id',UUIDTools::UUID.timestamp_create.to_s,false)
  @header.set('version',VERSION,false)
  @header.set('created_at',now.utc.strftime("%Y-%m-%dT%H:%M:%S.#{now.usec/1000}Z"),false)

  @header.set('process.name',nil,false)
  @header.set('process.execution_id',UUIDTools::UUID.timestamp_create.to_s,false)
  @header.set('process.step_position',0,false)

  @header.set('status.retried_count',0,false)
  @header.set('status.code',STATUS_INITIAL,false)
  @header.set('status.message',nil,false)

end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



23
24
25
# File 'lib/eventhub/message.rb', line 23

def body
  @body
end

#headerObject

Returns the value of attribute header.



23
24
25
# File 'lib/eventhub/message.rb', line 23

def header
  @header
end

#rawObject

Returns the value of attribute raw.



23
24
25
# File 'lib/eventhub/message.rb', line 23

def raw
  @raw
end

#routing_keyObject

Returns the value of attribute routing_key.



23
24
25
# File 'lib/eventhub/message.rb', line 23

def routing_key
  @routing_key
end

#vhostObject

Returns the value of attribute vhost.



23
24
25
# File 'lib/eventhub/message.rb', line 23

def vhost
  @vhost
end

Class Method Details

.from_json(json) ⇒ Object



38
39
40
41
42
43
# File 'lib/eventhub/message.rb', line 38

def self.from_json(json)
  data = JSON.parse(json)
  Message.new(data.get('header'), data.get('body'),json)
rescue => e
  Message.new({ "status" =>  { "code" => STATUS_INVALID, "message" => "JSON parse error: #{e}" }} ,{},json)
end

Instance Method Details

#copy(args = {}) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/eventhub/message.rb', line 96

def copy(args={})
  copied_header = self.header.dup
  copied_body   = self.body.dup

  args.each { |key,value| copied_header.set(key,value) } if args.is_a?(Hash)
  Message.new(copied_header, copied_body)
end

#initial?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/eventhub/message.rb', line 80

def initial?
  self.status_code == STATUS_INITIAL
end

#retried?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/eventhub/message.rb', line 84

def retried?
  self.status_code == STATUS_RETRIED
end

#retry?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/eventhub/message.rb', line 76

def retry?
  !success?
end

#success?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/eventhub/message.rb', line 72

def success?
  self.status_code == STATUS_SUCCESS
end

#to_jsonObject



88
89
90
# File 'lib/eventhub/message.rb', line 88

def to_json
  {'header' => self.header, 'body' => self.body}.to_json
end

#to_sObject



92
93
94
# File 'lib/eventhub/message.rb', line 92

def to_s
  "Message: message_id [#{self.message_id}], status.code [#{status_code}], status.message [#{status_message}], status.retried_count [#{status_retried_count}] "
end

#valid?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/eventhub/message.rb', line 68

def valid?
  REQUIRED_HEADERS.all? { |key| @header.all_keys_with_path.include?(key) }
end