Class: Wechat::Message
- Inherits:
-
Object
- Object
- Wechat::Message
- Defined in:
- lib/wechat/message.rb
Defined Under Namespace
Classes: ArticleBuilder
Constant Summary collapse
- TO_JSON_KEY_MAP =
{ 'ToUserName' => 'touser', 'MediaId' => 'media_id', 'ThumbMediaId' => 'thumb_media_id', 'TemplateId' => 'template_id' }
- TO_JSON_ALLOWED =
%w(touser msgtype content image voice video music news articles template agentid)
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
- #agent_id(agentid) ⇒ 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
- #transfer_customer_service ⇒ Object
- #video(media_id, opts = {}) ⇒ Object
- #voice(media_id) ⇒ Object
Constructor Details
#initialize(message_hash) ⇒ Message
Returns a new instance of Message.
27 28 29 |
# File 'lib/wechat/message.rb', line 27 def initialize() = || {} end |
Instance Attribute Details
#message_hash ⇒ Object (readonly)
Returns the value of attribute message_hash.
25 26 27 |
# File 'lib/wechat/message.rb', line 25 def end |
Class Method Details
.from_hash(message_hash) ⇒ Object
4 5 6 |
# File 'lib/wechat/message.rb', line 4 def from_hash() new() end |
.to(to_user) ⇒ Object
8 9 10 |
# File 'lib/wechat/message.rb', line 8 def to(to_user) new(ToUserName: to_user, CreateTime: Time.now.to_i) end |
Instance Method Details
#[](key) ⇒ Object
31 32 33 |
# File 'lib/wechat/message.rb', line 31 def [](key) [key] end |
#agent_id(agentid) ⇒ Object
64 65 66 |
# File 'lib/wechat/message.rb', line 64 def agent_id(agentid) update(AgentId: agentid) end |
#as(type) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wechat/message.rb', line 43 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).each_with_object({}) do |value, results| results[value[0].to_s.underscore.to_sym] = value[1] end else fail "Don't know how to parse message as #{type}" end end |
#image(media_id) ⇒ Object
76 77 78 |
# File 'lib/wechat/message.rb', line 76 def image(media_id) update(MsgType: 'image', Image: { MediaId: media_id }) end |
#music(thumb_media_id, music_url, opts = {}) ⇒ Object
89 90 91 92 |
# File 'lib/wechat/message.rb', line 89 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/wechat/message.rb', line 94 def news(collection, &block) if block_given? article = ArticleBuilder.new collection.take(10).each_with_index { |item, index| yield(article, item, index) } 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
35 36 37 38 39 40 41 |
# File 'lib/wechat/message.rb', line 35 def reply Message.new( ToUserName: [:FromUserName], FromUserName: [:ToUserName], CreateTime: Time.now.to_i ) end |
#save_to!(model_class) ⇒ Object
145 146 147 148 149 |
# File 'lib/wechat/message.rb', line 145 def save_to!(model_class) model = model_class.new(underscore_hash_keys()) model.save! self end |
#template(opts = {}) ⇒ Object
109 110 111 112 |
# File 'lib/wechat/message.rb', line 109 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
68 69 70 |
# File 'lib/wechat/message.rb', line 68 def text(content) update(MsgType: 'text', Content: content) end |
#to(openid) ⇒ Object
60 61 62 |
# File 'lib/wechat/message.rb', line 60 def to(openid) update(ToUserName: openid) end |
#to_json ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/wechat/message.rb', line 127 def to_json json_hash = deep_recursive() do |key, value| key = key.to_s [(TO_JSON_KEY_MAP[key] || key.downcase), value] end json_hash = json_hash.select { |k, _v| TO_JSON_ALLOWED.include? k } 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
114 115 116 |
# File 'lib/wechat/message.rb', line 114 def to_xml .to_xml(root: 'xml', children: 'item', skip_instruct: true, skip_types: true) end |
#transfer_customer_service ⇒ Object
72 73 74 |
# File 'lib/wechat/message.rb', line 72 def transfer_customer_service update(MsgType: 'transfer_customer_service') end |
#video(media_id, opts = {}) ⇒ Object
84 85 86 87 |
# File 'lib/wechat/message.rb', line 84 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
80 81 82 |
# File 'lib/wechat/message.rb', line 80 def voice(media_id) update(MsgType: 'voice', Voice: { MediaId: media_id }) end |