Commit 27e67364 by lanrion

refactor custom.rb

parent caa01769
...@@ -14,7 +14,7 @@ module WeixinAuthorize ...@@ -14,7 +14,7 @@ module WeixinAuthorize
# } # }
def send_text_custom(to_user, content) def send_text_custom(to_user, content)
message = default_options(to_user).merge({text: {content: content}}) message = default_options(to_user).merge({text: {content: content}})
http_post(custom_base_url, MultiJson.dump(message)) http_post(custom_base_url, message)
end end
# 发送图片消息 # 发送图片消息
...@@ -27,8 +27,8 @@ module WeixinAuthorize ...@@ -27,8 +27,8 @@ module WeixinAuthorize
# } # }
# } # }
def send_image_custom(to_user, media_id) def send_image_custom(to_user, media_id)
message = default_options(to_user).merge({msgtype: "image", image: {media_id: media_id}}) message = default_options(to_user, "image").merge({image: {media_id: media_id}})
http_post(custom_base_url, MultiJson.dump(message)) http_post(custom_base_url, message)
end end
# 发送语音消息 # 发送语音消息
...@@ -41,8 +41,8 @@ module WeixinAuthorize ...@@ -41,8 +41,8 @@ module WeixinAuthorize
# } # }
# } # }
def send_voice_custom(to_user, media_id) def send_voice_custom(to_user, media_id)
message = default_options(to_user).merge({msgtype: "voice", voice: {media_id: media_id}}) message = default_options(to_user, "voice").merge({voice: {media_id: media_id}})
http_post(custom_base_url, MultiJson.dump(message)) http_post(custom_base_url, message)
end end
# 发送视频消息 # 发送视频消息
...@@ -56,8 +56,8 @@ module WeixinAuthorize ...@@ -56,8 +56,8 @@ module WeixinAuthorize
# } # }
def send_video_custom(to_user, media_id, options={}) def send_video_custom(to_user, media_id, options={})
video_options = {media_id: media_id}.merge(options) video_options = {media_id: media_id}.merge(options)
message = default_options(to_user).merge({msgtype: "video", video: video_options}) message = default_options(to_user, "video").merge({video: video_options})
http_post(custom_base_url, MultiJson.dump(message)) http_post(custom_base_url, message)
end end
# 发送音乐消息 # 发送音乐消息
...@@ -74,10 +74,12 @@ module WeixinAuthorize ...@@ -74,10 +74,12 @@ module WeixinAuthorize
# } # }
# } # }
def send_music_custom(to_user, media_id, musicurl, hqmusicurl, options={}) def send_music_custom(to_user, media_id, musicurl, hqmusicurl, options={})
music_options = { thumb_media_id: media_id, musicurl: musicurl, music_options = { thumb_media_id: media_id,
hqmusicurl: hqmusicurl}.merge(options) musicurl: musicurl,
message = default_options(to_user).merge({msgtype: "music", music: music_options}) hqmusicurl: hqmusicurl
http_post(custom_base_url, MultiJson.dump(message)) }.merge(options)
message = default_options(to_user, "music").merge({music: music_options})
http_post(custom_base_url, message)
end end
# 发送图文消息 # 发送图文消息
...@@ -101,9 +103,9 @@ module WeixinAuthorize ...@@ -101,9 +103,9 @@ module WeixinAuthorize
# ] # ]
# } # }
# } # }
def send_news_custom(to_user, *articles) def send_news_custom(to_user, articles=[])
message = default_options(to_user).merge({msgtype: "news", news: {articles: articles}}) message = default_options(to_user, "news").merge({news: {articles: articles}})
http_post(custom_base_url, MultiJson.dump(message)) http_post(custom_base_url, message)
end end
private private
...@@ -113,8 +115,8 @@ module WeixinAuthorize ...@@ -113,8 +115,8 @@ module WeixinAuthorize
"/message/custom/send" "/message/custom/send"
end end
def default_options(to_user) def default_options(to_user, msgtype="text")
{touser: to_user, msgtype: "text"} {touser: to_user, msgtype: msgtype}
end end
end end
......
require "spec_helper" require "spec_helper"
describe WeixinAuthorize::Api::Custom do describe WeixinAuthorize::Api::Custom do
it "can send a text Custom message" do let(:text_message) do
response = $client.send_text_custom(ENV["OPENID"], "test send Custom Message") "text Custom message"
puts response
end end
it "#send_text_custom" do
response = $client.send_text_custom(ENV["OPENID"], text_message)
expect(response["errcode"]).to eq(0)
end
it "#send_news_custom" do
articles = [{
"title" => "Happy Day",
"description" => "Is Really A Happy Day",
"url" => "http://www.baidu.com",
"picurl" => "http://www.baidu.com/img/bdlogo.gif"
},
{
"title" => "Happy Day",
"description" => "Is Really A Happy Day",
"url" => "http://www.baidu.com",
"picurl"=> "http://www.baidu.com/img/bdlogo.gif"
}]
response = $client.send_news_custom(ENV["OPENID"], articles)
expect(response["errcode"]).to eq(0)
end
it "#send_image_custom" do
pending("The test must have a media_id")
this_should_not_get_executed
end
it "#send_video_custom" do
pending("The test must have a media_id")
this_should_not_get_executed
end
it "#send_music_custom" do
pending("The test must have a media_id")
this_should_not_get_executed
end
it "#send_voice_custom" do
pending("The test must have a media_id")
this_should_not_get_executed
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment