Method: Wechat::Message#to_json

Defined in:
lib/wechat/message.rb

#to_jsonObject



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(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").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