235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/wechat/message.rb', line 235
def to_json(*_args)
keep_camel_case_key = message_hash[:MsgType] == 'template'
json_hash = deep_recursive(message_hash) do |key, value|
key = key.to_s
[(TO_JSON_KEY_MAP[key] || (keep_camel_case_key ? key : key.downcase)), value]
end
json_hash = json_hash.transform_keys(&:downcase).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 'mpnews'
json_hash = { 'articles' => json_hash['articles'] }
when 'draft_news'
json_hash = json_hash['articles']
when 'ref_mpnews'
json_hash['msgtype'] = 'mpnews'
json_hash.delete('articles')
when 'template'
json_hash = { 'touser' => json_hash['touser'] }.merge!(json_hash['template'])
end
JSON.generate(json_hash)
end
|