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", "TemplateId"=>"template_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
- #template(opts = {}) ⇒ 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.
35 36 37 |
# File 'lib/wechat/message.rb', line 35 def initialize() @message_hash = || {} end |
Instance Attribute Details
#message_hash ⇒ Object (readonly)
Returns the value of attribute message_hash.
33 34 35 |
# File 'lib/wechat/message.rb', line 33 def @message_hash end |
Class Method Details
.from_hash(message_hash) ⇒ Object
12 13 14 |
# File 'lib/wechat/message.rb', line 12 def from_hash self.new() end |
.to(to_user) ⇒ Object
16 17 18 |
# File 'lib/wechat/message.rb', line 16 def to to_user self.new(:ToUserName=>to_user, :CreateTime=>Time.now.to_i) end |
Instance Method Details
#[](key) ⇒ Object
39 40 41 |
# File 'lib/wechat/message.rb', line 39 def [](key) [key] end |
#as(type) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/wechat/message.rb', line 51 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
75 76 77 |
# File 'lib/wechat/message.rb', line 75 def image media_id update(:MsgType=>"image", :Image=>{:MediaId=>media_id}) end |
#music(thumb_media_id, music_url, opts = {}) ⇒ Object
88 89 90 91 |
# File 'lib/wechat/message.rb', line 88 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/wechat/message.rb', line 93 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).reject{|k,v| v.nil? }) end end update(:MsgType=>"news", :ArticleCount=> items.count, :Articles=> items.collect{|item| camelize_hash_keys(item)}) end |
#reply ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/wechat/message.rb', line 43 def reply Message.new( :ToUserName=>[:FromUserName], :FromUserName=>[:ToUserName], :CreateTime=>Time.now.to_i ) end |
#save_to!(model_class) ⇒ Object
135 136 137 138 139 |
# File 'lib/wechat/message.rb', line 135 def save_to! model_class model = model_class.new(underscore_hash_keys()) model.save! return self end |
#template(opts = {}) ⇒ Object
108 109 110 111 |
# File 'lib/wechat/message.rb', line 108 def template opts={} template_fields = camelize_hash_keys(opts.symbolize_keys.slice(:template_id, :topcolor, :url, :data)) update(:MsgType=>"template",:Template=> template_fields) end |
#text(content) ⇒ Object
71 72 73 |
# File 'lib/wechat/message.rb', line 71 def text content update(:MsgType=>"text", :Content=>content) end |
#to(openid) ⇒ Object
67 68 69 |
# File 'lib/wechat/message.rb', line 67 def to openid update(:ToUserName=>openid) end |
#to_json ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/wechat/message.rb', line 117 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","template").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")} when "template" json_hash.merge! json_hash['template'] end JSON.generate(json_hash) end |
#to_xml ⇒ Object
113 114 115 |
# File 'lib/wechat/message.rb', line 113 def to_xml .to_xml(root: "xml", children: "item", skip_instruct: true, skip_types: true) end |
#video(media_id, opts = {}) ⇒ Object
83 84 85 86 |
# File 'lib/wechat/message.rb', line 83 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
79 80 81 |
# File 'lib/wechat/message.rb', line 79 def voice media_id update(:MsgType=>"voice", :Voice=>{:MediaId=>media_id}) end |