class WechatPublic::Message

Constants

JSON_KEY_MAP

Attributes

message_hash[R]

Public Class Methods

from_hash(message_hash) click to toggle source
# File lib/wechat/message.rb, line 12
def from_hash message_hash
  self.new(message_hash)
end
new(message_hash) click to toggle source
# File lib/wechat/message.rb, line 35
def initialize(message_hash)
  @message_hash = message_hash || {}
end
to(to_user) click to toggle source
# File lib/wechat/message.rb, line 16
def to to_user
  self.new(:ToUserName=>to_user, :CreateTime=>Time.now.to_i)
end

Public Instance Methods

[](key) click to toggle source
# File lib/wechat/message.rb, line 39
def [](key)
  message_hash[key]
end
as(type) click to toggle source
# File lib/wechat/message.rb, line 51
def as type
  case type
  when :text
    message_hash[:Content]

  when :image, :voice, :video
    WechatPublic.api.media(message_hash[:MediaId])

  when :location
    message_hash.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) click to toggle source
# 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={}) click to toggle source
# 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) { |article, item| ... } click to toggle source
# 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() click to toggle source
# File lib/wechat/message.rb, line 43
def reply
  Message.new(
    :ToUserName=>message_hash[:FromUserName], 
    :FromUserName=>message_hash[:ToUserName], 
    :CreateTime=>Time.now.to_i
  )
end
save_to!(model_class) click to toggle source
# File lib/wechat/message.rb, line 135
def save_to! model_class
  model = model_class.new(underscore_hash_keys(message_hash))
  model.save!
  return self
end
template(opts={}) click to toggle source
# 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) click to toggle source
# File lib/wechat/message.rb, line 71
def text content
  update(:MsgType=>"text", :Content=>content)
end
to(openid) click to toggle source
# File lib/wechat/message.rb, line 67
def to openid
  update(:ToUserName=>openid)
end
to_json() click to toggle source
# File lib/wechat/message.rb, line 117
def to_json
  json_hash = deep_recursive(message_hash) 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() click to toggle source
# File lib/wechat/message.rb, line 113
def to_xml
  message_hash.to_xml(root: "xml", children: "item", skip_instruct: true, skip_types: true)
end
video(media_id, opts={}) click to toggle source
# 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) click to toggle source
# File lib/wechat/message.rb, line 79
def voice media_id
  update(:MsgType=>"voice", :Voice=>{:MediaId=>media_id})
end

Private Instance Methods

camelize_hash_keys(hash) click to toggle source
# File lib/wechat/message.rb, line 142
def camelize_hash_keys hash
  deep_recursive(hash){|key, value| [key.to_s.camelize.to_sym, value]} 
end
deep_recursive(hash) { |key, value| ... } click to toggle source
# File lib/wechat/message.rb, line 155
def deep_recursive hash, &block
  hash.inject({}) do |memo, val|
    key,value = *val
    case value.class.name
    when "Hash"
      value = deep_recursive(value, &block)
    when "Array"
      value = value.collect{|item| item.is_a?(Hash) ? deep_recursive(item, &block) : item}
    end

    key,value = yield(key, value)
    memo.merge!(key => value)
  end
end
underscore_hash_keys(hash) click to toggle source
# File lib/wechat/message.rb, line 146
def underscore_hash_keys hash
  deep_recursive(hash){|key, value| [key.to_s.underscore.to_sym, value]} 
end
update(fields={}) click to toggle source
# File lib/wechat/message.rb, line 150
def update fields={}
  message_hash.merge!(fields)
  return self
end