class MpWeixin::Event

The MpWeixin::Message class

Attributes

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

Public Class Methods

from_xml(xml) click to toggle source
# 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
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/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

Public Instance Methods

create_time() click to toggle source

same as @attributes CreateTime of an Message instance

@return [Integer]

# File lib/mp_weixin/models/event.rb, line 27
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/event.rb, line 35
def created_at
  Time.at create_time rescue nil
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/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) click to toggle source

initialize an ImageReplyMessage @attributes [Hash] the attributes of ImageReplyMessage

# File lib/mp_weixin/models/event.rb, line 78
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/event.rb, line 96
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/event.rb, line 102
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/event.rb, line 72
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/event.rb, line 90
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/event.rb, line 84
def reply_voice_message(attributes)
  reply("voice", attributes)
end