Class: MpWeixin::Event

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/mp_weixin/models/event.rb

Overview

The MpWeixin::Message class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveModel::Model

included, #persisted?

Constructor Details

#initialize(attributes = nil) ⇒ Event

Instantiate a new Message with a hash of attributes

Parameters:

  • attributes (Hash) (defaults to: nil)

    the attributes value



13
14
15
16
17
18
19
20
21
22
# File 'lib/mp_weixin/models/event.rb', line 13

def initialize(attributes = nil)
  # Dynamic attr_accessible
  # maybe cause secret problem
  singleton_class.class_eval do
    attr_accessor *attributes.keys
  end

  super
  @source = ActiveSupport::HashWithIndifferentAccess.new(attributes)
end

Instance Attribute Details

#CreateTimeObject

Returns the value of attribute CreateTime.



7
8
9
# File 'lib/mp_weixin/models/event.rb', line 7

def CreateTime
  @CreateTime
end

#FromUserNameObject

Returns the value of attribute FromUserName.



7
8
9
# File 'lib/mp_weixin/models/event.rb', line 7

def FromUserName
  @FromUserName
end

#MsgTypeObject

Returns the value of attribute MsgType.



7
8
9
# File 'lib/mp_weixin/models/event.rb', line 7

def MsgType
  @MsgType
end

#ToUserNameObject

Returns the value of attribute ToUserName.



7
8
9
# File 'lib/mp_weixin/models/event.rb', line 7

def ToUserName
  @ToUserName
end

Class Method Details

.from_xml(xml) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mp_weixin/models/event.rb', line 107

def from_xml(xml)
  begin
    hash = MultiXml.parse(xml)['xml']

    message = case hash['MsgType']
                when 'event'
                  Event.new(hash)
                else
                  # raise 'Unknown Message data'
              end
  rescue
    logger.info('Unknown Message data #{xml}') if self.respond_to?(:logger)
  end
end

Instance Method Details

#create_timeInteger

same as @attributes CreateTime of an Message instance

Returns:

  • (Integer)


27
28
29
# File 'lib/mp_weixin/models/event.rb', line 27

def create_time
  self.CreateTime.to_i
end

#created_atTime

convert create_time to an Time instance

Returns:

  • (Time)


35
36
37
# File 'lib/mp_weixin/models/event.rb', line 35

def created_at
  Time.at create_time rescue nil
end

#reply(msg_type, attributes) ⇒ Object

initialize an ReplyMessage

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mp_weixin/models/event.rb', line 43

def reply(msg_type, attributes)
  if attributes.is_a?(Hash)
    attributes = attributes.deep_symbolize_keys
    attributes.reverse_merge!({
      ToUserName: self.FromUserName,
      FromUserName: self.ToUserName
    })
  end

  case msg_type
    when 'text'
      MpWeixin::TextReplyMessage.new(attributes)
    when 'image'
      MpWeixin::ImageReplyMessage.new(attributes)
    when 'voice'
      MpWeixin::VoiceReplyMessage.new(attributes)
    when 'video'
      MpWeixin::VideoReplyMessage.new(attributes)
    when 'music'
      MpWeixin::MusicReplyMessage.new(attributes)
    when 'news'
      MpWeixin::NewsReplyMessage.new(attributes)
    else
      # raise 'Unknown Message data'
  end
end

#reply_image_message(attributes) ⇒ Object

initialize an ImageReplyMessage



78
79
80
# File 'lib/mp_weixin/models/event.rb', line 78

def reply_image_message(attributes)
  reply("image", attributes)
end

#reply_music_message(attributes) ⇒ Object

initialize an MusicReplyMessage



96
97
98
# File 'lib/mp_weixin/models/event.rb', line 96

def reply_music_message(attributes)
  reply("music", attributes)
end

#reply_news_message(attributes) ⇒ Object

initialize an NewsReplyMessage



102
103
104
# File 'lib/mp_weixin/models/event.rb', line 102

def reply_news_message(attributes)
  reply("news", attributes)
end

#reply_text_message(attributes) ⇒ Object

initialize an TextReplyMessage



72
73
74
# File 'lib/mp_weixin/models/event.rb', line 72

def reply_text_message(attributes)
  reply("text", attributes)
end

#reply_video_message(attributes) ⇒ Object

initialize an VideoReplyMessage



90
91
92
# File 'lib/mp_weixin/models/event.rb', line 90

def reply_video_message(attributes)
  reply("video", attributes)
end

#reply_voice_message(attributes) ⇒ Object

initialize an VoiceReplyMessage



84
85
86
# File 'lib/mp_weixin/models/event.rb', line 84

def reply_voice_message(attributes)
  reply("voice", attributes)
end