class MpWeixin::Message
The MpWeixin::Message
class
Attributes
Public Class Methods
# File lib/mp_weixin/models/message.rb, line 112 def from_xml(xml) begin hash = MultiXml.parse(xml)['xml'] message = 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
Instantiate a new Message
with a hash of attributes
@param [Hash] attributes the attributes value
ActiveModel::Model::new
# 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
Public Instance Methods
same as @attributes CreateTime
of an Message
instance
@return [Integer]
# File lib/mp_weixin/models/message.rb, line 28 def create_time self.CreateTime.to_i end
convert create_time
to an Time instance
@return [Time]
# File lib/mp_weixin/models/message.rb, line 36 def created_at Time.at create_time rescue nil end
# File lib/mp_weixin/models/message.rb, line 40 def msg_id self.MsgId.to_i end
initialize an ReplyMessage
@msg_type [string] the MsgType
of ReplyMessage
@attributes [Hash] the attributes of ReplyMessage
@return an instance of #{MsgType}ReplyMessage
# 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
initialize an ImageReplyMessage
@attributes [Hash] the attributes of ImageReplyMessage
# File lib/mp_weixin/models/message.rb, line 83 def reply_image_message(attributes) reply("image", attributes) end
initialize an MusicReplyMessage
@attributes [Hash] the attributes of MusicReplyMessage
# File lib/mp_weixin/models/message.rb, line 101 def reply_music_message(attributes) reply("music", attributes) end
initialize an NewsReplyMessage
@attributes [Hash] the attributes of NewsReplyMessage
# File lib/mp_weixin/models/message.rb, line 107 def reply_news_message(attributes) reply("news", attributes) end
initialize an TextReplyMessage
@attributes [Hash] the attributes of TextReplyMessage
# File lib/mp_weixin/models/message.rb, line 77 def reply_text_message(attributes) reply("text", attributes) end
initialize an VideoReplyMessage
@attributes [Hash] the attributes of VideoReplyMessage
# File lib/mp_weixin/models/message.rb, line 95 def reply_video_message(attributes) reply("video", attributes) end
initialize an VoiceReplyMessage
@attributes [Hash] the attributes of VoiceReplyMessage
# File lib/mp_weixin/models/message.rb, line 89 def reply_voice_message(attributes) reply("voice", attributes) end