Class: Qwrapper::Message

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/qwrapper/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger, #logger=

Constructor Details

#initialize(body) ⇒ Message

Returns a new instance of Message.



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

def initialize(body)
  raise ArgumentError.new "Message body cannot be blank" if body.nil? or body.empty?
  @original_body = body
  begin
    begin
      @hash = eval(body.to_s)
    rescue SyntaxError => ex
      @hash = JSON.parse(body.to_s)
    end
    logger.info "Message evaluated: #{@hash}"
  rescue Exception => ex
    logger.error "Message body cannot be evaluated: #{body}"
    raise ex
  end
  validate!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



31
32
33
# File 'lib/qwrapper/message.rb', line 31

def method_missing(m, *args, &block)
  hash[m.to_s] || hash[m.to_s.to_sym]
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



8
9
10
# File 'lib/qwrapper/message.rb', line 8

def hash
  @hash
end

#original_bodyObject (readonly)

Returns the value of attribute original_body.



8
9
10
# File 'lib/qwrapper/message.rb', line 8

def original_body
  @original_body
end

Instance Method Details

#actionObject



27
28
29
# File 'lib/qwrapper/message.rb', line 27

def action
  ((hash[:action] || hash["action"])).to_sym
end