Class: Wechat::Message
- Inherits:
-
Object
- Object
- Wechat::Message
- Defined in:
- lib/wechat/message.rb
Defined Under Namespace
Classes: ArticleBuilder
Constant Summary collapse
- JSON_KEY_MAP =
{ "ToUserName" => "touser", "MediaId" => "media_id", "ThumbMediaId" => "thumb_media_id" }
Instance Attribute Summary collapse
-
#message_hash ⇒ Object
readonly
Returns the value of attribute message_hash.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #as(type) ⇒ Object
- #image(media_id) ⇒ Object
-
#initialize(message_hash) ⇒ Message
constructor
A new instance of Message.
- #music(thumb_media_id, music_url, opts = {}) ⇒ Object
- #news(collection, &block) ⇒ Object
- #reply ⇒ Object
- #save_to!(model_class) ⇒ Object
- #text(content) ⇒ Object
- #to(openid) ⇒ Object
- #to_json ⇒ Object
- #to_xml ⇒ Object
- #video(media_id, opts = {}) ⇒ Object
- #voice(media_id) ⇒ Object
Constructor Details
#initialize(message_hash) ⇒ Message
Returns a new instance of Message.
34 35 36 |
# File 'lib/wechat/message.rb', line 34 def initialize() @message_hash = || {} end |
Instance Attribute Details
#message_hash ⇒ Object (readonly)
Returns the value of attribute message_hash.
32 33 34 |
# File 'lib/wechat/message.rb', line 32 def @message_hash end |
Class Method Details
.from_hash(message_hash) ⇒ Object
11 12 13 |
# File 'lib/wechat/message.rb', line 11 def from_hash self.new() end |
.to(to_user) ⇒ Object
15 16 17 |
# File 'lib/wechat/message.rb', line 15 def to to_user self.new(:ToUserName=>to_user, :CreateTime=>Time.now.to_i) end |
Instance Method Details
#[](key) ⇒ Object
38 39 40 |
# File 'lib/wechat/message.rb', line 38 def [](key) [key] end |
#as(type) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wechat/message.rb', line 50 def as type case type when :text [:Content] when :image, :voice, :video Wechat.api.media([:MediaId]) when :location .slice(:Location_X, :Location_Y, :Scale, :Label).inject({}){|results, value| results[value[0].to_s.underscore.to_sym] = value[1]; results} else raise "Don't know how to parse message as #{type}" end end |
#image(media_id) ⇒ Object
74 75 76 |
# File 'lib/wechat/message.rb', line 74 def image media_id update(:MsgType=>"image", :Image=>{:MediaId=>media_id}) end |
#music(thumb_media_id, music_url, opts = {}) ⇒ Object
87 88 89 90 |
# File 'lib/wechat/message.rb', line 87 def music thumb_media_id, music_url, opts={} music_fields = camelize_hash_keys(opts.slice(:title, :description, :HQ_music_url).merge(music_url: music_url, thumb_media_id: thumb_media_id)) update(:MsgType=>"music", :Music=>music_fields) end |
#news(collection, &block) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/wechat/message.rb', line 92 def news collection, &block if block_given? article = ArticleBuilder.new collection.each{|item| yield(article, item)} items = article.items else items = collection.collect do |item| camelize_hash_keys(item.symbolize_keys.slice(:title, :description, :pic_url, :url)) end end update(:MsgType=>"news", :ArticleCount=> items.count, :Articles=> items.collect{|item| camelize_hash_keys(item)}) end |
#reply ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/wechat/message.rb', line 42 def reply Message.new( :ToUserName=>[:FromUserName], :FromUserName=>[:ToUserName], :CreateTime=>Time.now.to_i ) end |
#save_to!(model_class) ⇒ Object
127 128 129 130 131 |
# File 'lib/wechat/message.rb', line 127 def save_to! model_class model = model_class.new(underscore_hash_keys()) model.save! return self end |
#text(content) ⇒ Object
70 71 72 |
# File 'lib/wechat/message.rb', line 70 def text content update(:MsgType=>"text", :Content=>content) end |
#to(openid) ⇒ Object
66 67 68 |
# File 'lib/wechat/message.rb', line 66 def to openid update(:ToUserName=>openid) end |
#to_json ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/wechat/message.rb', line 111 def to_json json_hash = deep_recursive() do |key, value| key = key.to_s [(JSON_KEY_MAP[key] || key.downcase), value] end json_hash.slice!("touser", "msgtype", "content", "image", "voice", "video", "music", "news", "articles").to_hash case json_hash["msgtype"] when "text" json_hash["text"] = {"content" => json_hash.delete("content")} when "news" json_hash["news"] = {"articles" => json_hash.delete("articles")} end JSON.generate(json_hash) end |
#to_xml ⇒ Object
107 108 109 |
# File 'lib/wechat/message.rb', line 107 def to_xml .to_xml(root: "xml", children: "item", skip_instruct: true, skip_types: true) end |
#video(media_id, opts = {}) ⇒ Object
82 83 84 85 |
# File 'lib/wechat/message.rb', line 82 def video media_id, opts={} video_fields = camelize_hash_keys({media_id: media_id}.merge(opts.slice(:title, :description))) update(:MsgType=>"video", :Video=>video_fields) end |
#voice(media_id) ⇒ Object
78 79 80 |
# File 'lib/wechat/message.rb', line 78 def voice media_id update(:MsgType=>"voice", :Voice=>{:MediaId=>media_id}) end |