Class: WeixinRailsMiddleware::Message

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Message

Returns a new instance of Message.



9
10
11
# File 'lib/weixin_rails_middleware/models/message.rb', line 9

def initialize(hash)
  @source = OpenStruct.new(hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



13
14
15
# File 'lib/weixin_rails_middleware/models/message.rb', line 13

def method_missing(method, *args, &block)
  @source.send(method, *args, &block)
end

Class Method Details

.factory(xml) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/weixin_rails_middleware/models/message.rb', line 25

def self.factory(xml)
  hash = MultiXml.parse(xml)['xml']
  case hash['MsgType']
  when 'text'
    TextMessage.new(hash)
  when 'image'
    ImageMessage.new(hash)
  when 'location'
    LocationMessage.new(hash)
  when 'link'
    LinkMessage.new(hash)
  when 'event'
    EventMessage.new(hash)
  when 'voice'
    VoiceMessage.new(hash)
  when 'video'
    VideoMessage.new(hash)
  when 'shortvideo'
    ShortVideo.new(hash)
  else
    raise ArgumentError, 'Unknown Weixin Message'
  end
end

Instance Method Details

#CreateTimeObject



17
18
19
# File 'lib/weixin_rails_middleware/models/message.rb', line 17

def CreateTime
  @source.CreateTime.to_i
end

#MsgIdObject



21
22
23
# File 'lib/weixin_rails_middleware/models/message.rb', line 21

def MsgId
  @source.MsgId.to_i
end