Class: MpWeixin::Message

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/mp_weixin/models/message.rb

Overview

The MpWeixin::Message class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveModel::Model

included, #persisted?

Constructor Details

#initialize(attributes = nil) ⇒ Message

Instantiate a new Message with a hash of attributes

Parameters:

  • attributes (Hash) (defaults to: nil)

    the attributes value



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

#CreateTimeObject

Returns the value of attribute CreateTime.



8
9
10
# File 'lib/mp_weixin/models/message.rb', line 8

def CreateTime
  @CreateTime
end

#FromUserNameObject

Returns the value of attribute FromUserName.



8
9
10
# File 'lib/mp_weixin/models/message.rb', line 8

def FromUserName
  @FromUserName
end

#MsgIdObject

Returns the value of attribute MsgId.



8
9
10
# File 'lib/mp_weixin/models/message.rb', line 8

def MsgId
  @MsgId
end

#MsgTypeObject

Returns the value of attribute MsgType.



8
9
10
# File 'lib/mp_weixin/models/message.rb', line 8

def MsgType
  @MsgType
end

#ToUserNameObject

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']
    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

Instance Method Details

#create_timeInteger

same as @attributes CreateTime of an Message instance

Returns:

  • (Integer)


28
29
30
# File 'lib/mp_weixin/models/message.rb', line 28

def create_time
  self.CreateTime.to_i
end

#created_atTime

convert create_time to an Time instance

Returns:

  • (Time)


36
37
38
# File 'lib/mp_weixin/models/message.rb', line 36

def created_at
  Time.at create_time rescue nil
end

#msg_idObject



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

Returns:



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 reply_image_message(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 reply_music_message(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 reply_news_message(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 reply_text_message(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 reply_video_message(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 reply_voice_message(attributes)
  reply("voice", attributes)
end