Class: Qwrapper::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Message

Returns a new instance of Message.



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

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



29
30
31
# File 'lib/qwrapper/message.rb', line 29

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.



6
7
8
# File 'lib/qwrapper/message.rb', line 6

def hash
  @hash
end

#original_bodyObject (readonly)

Returns the value of attribute original_body.



6
7
8
# File 'lib/qwrapper/message.rb', line 6

def original_body
  @original_body
end

Instance Method Details

#actionObject



25
26
27
# File 'lib/qwrapper/message.rb', line 25

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