Class: Weixin::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/weixin/model.rb

Direct Known Subclasses

LocationMessage, VideoMessage, VoiceMessage

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Message

Returns a new instance of Message.



10
11
12
# File 'lib/weixin/model.rb', line 10

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



14
15
16
# File 'lib/weixin/model.rb', line 14

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

Class Method Details

.factory(xml) ⇒ Object



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

def Message.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)
    else
        raise ArgumentError, 'Unknown Message'
    end
end

Instance Method Details

#CreateTimeObject



18
19
20
# File 'lib/weixin/model.rb', line 18

def CreateTime
    @source.CreateTime.to_i
end

#MsgIdObject



22
23
24
# File 'lib/weixin/model.rb', line 22

def MsgId
    @source.MsgId.to_i
end