Commit 2912daf2 by lanrion

Merge pull request #7 from lanrion/added-media-api

Added media api
parents c1ee50ee 4531cdcd
......@@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
*DS_Store
......@@ -6,6 +6,7 @@ require "weixin_authorize/api/menu"
require "weixin_authorize/api/custom"
require "weixin_authorize/api/groups"
require "weixin_authorize/api/qrcode"
require "weixin_authorize/api/media"
require "weixin_authorize/client"
module WeixinAuthorize
......
# encoding: utf-8
module WeixinAuthorize
module Api
module Media
# 上传多媒体文件
# http请求方式: POST/FORM
# http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
# 支持传路径或者文件类型
def upload_media(media, type)
file = media.is_a?(File) ? media : File.new(media)
upload_media_url = "#{media_base_url}/upload"
http_upload(upload_media_url, {media: file, media_type: type})
end
# 目前仅仅把下载链接返回给第三方开发者,由第三方开发者处理下载
# php重新写入文件方式:
# http://www.cnblogs.com/txw1958/p/weixin80-upload-download-media-file.html
def download_media_url(media_id)
download_media_url = file_endpoint + "#{media_base_url}/get"
download_media_url += "?access_token=#{get_access_token}"
download_media_url += "&media_id=#{media_id}"
download_media_url
end
private
def media_base_url
"/media"
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
......@@ -9,6 +9,7 @@ module WeixinAuthorize
include Api::Custom
include Api::Groups
include Api::Qrcode
include Api::Media
attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
attr_accessor :access_token, :redis_key
......
require "spec_helper"
describe WeixinAuthorize::Api::Media do
let(:image_path) do
"#{File.dirname(__FILE__)}/medias/ruby-logo.jpg"
end
let(:image_file) do
File.new(image_path)
end
it "can upload a image" do
response = $client.upload_media(image_file, "image")
expect(response.keys).to eq(["type", "media_id", "created_at"])
end
# it "#download_media_url" do
# image = $client.upload_media(image_file, "image")
# media_id = image["media_id"]
# response = $client.download_media_url(media_id)
# response
# 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