Class: Nezu::Runtime::Consumer

Inherits:
Object
  • Object
show all
Extended by:
Common
Defined in:
lib/nezu/runtime/consumer.rb

Instance Method Summary collapse

Methods included from Common

descendants, inherited

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *params) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/nezu/runtime/consumer.rb', line 27

def method_missing(meth, *params)
  if meth.to_s.match(/_result$/)
    {}
  else
    super
  end
end

Instance Method Details

#handle_message(metadata, payload) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nezu/runtime/consumer.rb', line 6

def handle_message(, payload)
  Nezu.logger.debug("NEZU Consumer[#{self.class}] payload: #{payload}")
  params = JSON.parse(payload.to_s)
  action = params.delete('__action')
  reply_to = params.delete('__reply_to')

  result = self.send(action.to_sym, params)

  if reply_to
    result.reverse_merge!({'__action' => "#{action}_result"})
    recipient = Nezu::Runtime::Recipient.new(reply_to)
    Nezu.logger.debug("sending result #{result}of #{action} to #{recipient}")
    recipient.push!(result)
  end
rescue JSON::ParserError => e
  Nezu.logger.error('Please send only json in the message body')
  Nezu.logger.debug(e)
rescue NoMethodError => e
  Nezu.logger.error(e)
end