class MpWeixin::Message

The MpWeixin::Message class

Attributes

CreateTime[RW]
FromUserName[RW]
MsgId[RW]
MsgType[RW]
ToUserName[RW]

Public Class Methods

from_xml(xml) click to toggle source
# 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
new(attributes = nil) click to toggle source

Instantiate a new Message with a hash of attributes

@param [Hash] attributes the attributes value

Calls superclass method 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

create_time() click to toggle source

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
created_at() click to toggle source

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
msg_id() click to toggle source
# File lib/mp_weixin/models/message.rb, line 40
def msg_id
  self.MsgId.to_i
end
reply(msg_type, attributes) click to toggle source

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
reply_image_message(attributes) click to toggle source

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
reply_music_message(attributes) click to toggle source

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
reply_news_message(attributes) click to toggle source

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
reply_text_message(attributes) click to toggle source

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
reply_video_message(attributes) click to toggle source

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
reply_voice_message(attributes) click to toggle source

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