Class: MpWeixin::Event
- Inherits:
-
Object
- Object
- MpWeixin::Event
- Includes:
- ActiveModel::Model
- Defined in:
- lib/mp_weixin/models/event.rb
Overview
The MpWeixin::Message class
Instance Attribute Summary collapse
-
#CreateTime ⇒ Object
Returns the value of attribute CreateTime.
-
#FromUserName ⇒ Object
Returns the value of attribute FromUserName.
-
#MsgType ⇒ Object
Returns the value of attribute MsgType.
-
#ToUserName ⇒ Object
Returns the value of attribute ToUserName.
Class Method Summary collapse
Instance Method Summary collapse
-
#create_time ⇒ Integer
same as @attributes CreateTime of an Message instance.
-
#created_at ⇒ Time
convert create_time to an Time instance.
-
#initialize(attributes = nil) ⇒ Event
constructor
Instantiate a new Message with a hash of attributes.
-
#reply(msg_type, attributes) ⇒ Object
initialize an ReplyMessage.
-
#reply_image_message(attributes) ⇒ Object
initialize an ImageReplyMessage.
-
#reply_music_message(attributes) ⇒ Object
initialize an MusicReplyMessage.
-
#reply_news_message(attributes) ⇒ Object
initialize an NewsReplyMessage.
-
#reply_text_message(attributes) ⇒ Object
initialize an TextReplyMessage.
-
#reply_video_message(attributes) ⇒ Object
initialize an VideoReplyMessage.
-
#reply_voice_message(attributes) ⇒ Object
initialize an VoiceReplyMessage.
Methods included from ActiveModel::Model
Constructor Details
#initialize(attributes = nil) ⇒ Event
Instantiate a new Message with a hash of attributes
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
#CreateTime ⇒ Object
Returns the value of attribute CreateTime.
7 8 9 |
# File 'lib/mp_weixin/models/event.rb', line 7 def CreateTime @CreateTime end |
#FromUserName ⇒ Object
Returns the value of attribute FromUserName.
7 8 9 |
# File 'lib/mp_weixin/models/event.rb', line 7 def FromUserName @FromUserName end |
#MsgType ⇒ Object
Returns the value of attribute MsgType.
7 8 9 |
# File 'lib/mp_weixin/models/event.rb', line 7 def MsgType @MsgType end |
#ToUserName ⇒ Object
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'] = 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_time ⇒ Integer
same as @attributes CreateTime of an Message instance
27 28 29 |
# File 'lib/mp_weixin/models/event.rb', line 27 def create_time self.CreateTime.to_i end |
#created_at ⇒ Time
convert create_time to an Time instance
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
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 (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 (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 (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 (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 (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 (attributes) reply("voice", attributes) end |