Class: MpWeixin::Message
- Inherits:
-
Object
- Object
- MpWeixin::Message
- Includes:
- ActiveModel::Model
- Defined in:
- lib/mp_weixin/models/message.rb
Overview
The MpWeixin::Message class
Direct Known Subclasses
ImageMessage, LinkMessage, LocationMessage, TextMessage, VideoMessage, VoiceMessage
Instance Attribute Summary collapse
-
#CreateTime ⇒ Object
Returns the value of attribute CreateTime.
-
#FromUserName ⇒ Object
Returns the value of attribute FromUserName.
-
#MsgId ⇒ Object
Returns the value of attribute MsgId.
-
#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) ⇒ Message
constructor
Instantiate a new Message with a hash of attributes.
- #msg_id ⇒ Object
-
#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) ⇒ Message
Instantiate a new Message with a hash of attributes
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mp_weixin/models/message.rb', line 14 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.
8 9 10 |
# File 'lib/mp_weixin/models/message.rb', line 8 def CreateTime @CreateTime end |
#FromUserName ⇒ Object
Returns the value of attribute FromUserName.
8 9 10 |
# File 'lib/mp_weixin/models/message.rb', line 8 def FromUserName @FromUserName end |
#MsgId ⇒ Object
Returns the value of attribute MsgId.
8 9 10 |
# File 'lib/mp_weixin/models/message.rb', line 8 def MsgId @MsgId end |
#MsgType ⇒ Object
Returns the value of attribute MsgType.
8 9 10 |
# File 'lib/mp_weixin/models/message.rb', line 8 def MsgType @MsgType end |
#ToUserName ⇒ Object
Returns the value of attribute ToUserName.
8 9 10 |
# File 'lib/mp_weixin/models/message.rb', line 8 def ToUserName @ToUserName end |
Class Method Details
.from_xml(xml) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mp_weixin/models/message.rb', line 112 def from_xml(xml) begin 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) Event.from_xml(xml) when 'voice' VoiceMessage.new(hash) when 'video' VideoMessage.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
28 29 30 |
# File 'lib/mp_weixin/models/message.rb', line 28 def create_time self.CreateTime.to_i end |
#created_at ⇒ Time
convert create_time to an Time instance
36 37 38 |
# File 'lib/mp_weixin/models/message.rb', line 36 def created_at Time.at create_time rescue nil end |
#msg_id ⇒ Object
40 41 42 |
# File 'lib/mp_weixin/models/message.rb', line 40 def msg_id self.MsgId.to_i end |
#reply(msg_type, attributes) ⇒ Object
initialize an ReplyMessage
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mp_weixin/models/message.rb', line 48 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
83 84 85 |
# File 'lib/mp_weixin/models/message.rb', line 83 def (attributes) reply("image", attributes) end |
#reply_music_message(attributes) ⇒ Object
initialize an MusicReplyMessage
101 102 103 |
# File 'lib/mp_weixin/models/message.rb', line 101 def (attributes) reply("music", attributes) end |
#reply_news_message(attributes) ⇒ Object
initialize an NewsReplyMessage
107 108 109 |
# File 'lib/mp_weixin/models/message.rb', line 107 def (attributes) reply("news", attributes) end |
#reply_text_message(attributes) ⇒ Object
initialize an TextReplyMessage
77 78 79 |
# File 'lib/mp_weixin/models/message.rb', line 77 def (attributes) reply("text", attributes) end |
#reply_video_message(attributes) ⇒ Object
initialize an VideoReplyMessage
95 96 97 |
# File 'lib/mp_weixin/models/message.rb', line 95 def (attributes) reply("video", attributes) end |
#reply_voice_message(attributes) ⇒ Object
initialize an VoiceReplyMessage
89 90 91 |
# File 'lib/mp_weixin/models/message.rb', line 89 def (attributes) reply("voice", attributes) end |