Commit b4228a30 by lanrion

refactor media apis

parent c6eea351
...@@ -7,19 +7,18 @@ module WeixinAuthorize ...@@ -7,19 +7,18 @@ module WeixinAuthorize
# http请求方式: POST/FORM # http请求方式: POST/FORM
# http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE # http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
# 支持传路径或者文件类型 # 支持传路径或者文件类型
def upload_media(media, type) def upload_media(media, media_type)
file = media.is_a?(File) ? media : File.new(media) file = media.is_a?(File) ? media : File.new(media)
upload_media_url = "#{media_base_url}/upload" upload_media_url = "#{media_base_url}/upload"
http_upload(upload_media_url, {media: file, media_type: type}) http_post(upload_media_url, {media: file}, {type: media_type}, "file")
end end
# 目前仅仅把下载链接返回给第三方开发者,由第三方开发者处理下载 # 目前仅仅把下载链接返回给第三方开发者,由第三方开发者处理下载
# php重新写入文件方式:
# http://www.cnblogs.com/txw1958/p/weixin80-upload-download-media-file.html
def download_media_url(media_id) def download_media_url(media_id)
download_media_url = file_endpoint + "#{media_base_url}/get" download_media_url = file_endpoint + "#{media_base_url}/get"
download_media_url += "?access_token=#{get_access_token}" params = URI.encode_www_form("access_token" => get_access_token,
download_media_url += "&media_id=#{media_id}" "media_id" => media_id)
download_media_url += "?#{params}"
download_media_url download_media_url
end end
...@@ -28,15 +27,6 @@ module WeixinAuthorize ...@@ -28,15 +27,6 @@ module WeixinAuthorize
def media_base_url def media_base_url
"/media" "/media"
end end
def http_upload(url, options)
media_type = options.delete(:media_type)
upload_url = file_endpoint + url
upload_url += "?access_token=#{get_access_token}"
upload_url += "&type=#{media_type}"
JSON.parse(RestClient.post(upload_url, options))
end
end end
end end
end end
...@@ -15,11 +15,11 @@ describe WeixinAuthorize::Api::Media do ...@@ -15,11 +15,11 @@ describe WeixinAuthorize::Api::Media do
expect(response.keys).to eq(["type", "media_id", "created_at"]) expect(response.keys).to eq(["type", "media_id", "created_at"])
end end
# it "#download_media_url" do it "#download_media_url return a String url" do
# image = $client.upload_media(image_file, "image") image = $client.upload_media(image_file, "image")
# media_id = image["media_id"] media_id = image["media_id"]
# response = $client.download_media_url(media_id) response = $client.download_media_url(media_id)
# response expect(response.class).to eq(String)
# end 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