Commit 227b6594 by lanrion

Merge branch 'master' into fixed-#10-refactor-with-Adapter

parents 1884fb75 54121afb
require "rest-client"
require "multi_json"
require "weixin_authorize/config"
require "weixin_authorize/api/user"
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/api"
require "weixin_authorize/client"
module WeixinAuthorize
......
require "weixin_authorize/api/user"
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"
......@@ -7,19 +7,18 @@ module WeixinAuthorize
# http请求方式: POST/FORM
# 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)
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
# 目前仅仅把下载链接返回给第三方开发者,由第三方开发者处理下载
# 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}"
params = URI.encode_www_form("access_token" => get_access_token,
"media_id" => media_id)
download_media_url += "?#{params}"
download_media_url
end
......@@ -28,15 +27,6 @@ module WeixinAuthorize
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
......@@ -100,10 +100,11 @@ module WeixinAuthorize
end
# Refactor
def http_post(url, options={}, endpoint="plain")
post_api_url = endpoint_url(endpoint) + url + "?access_token=#{get_access_token}"
options = MultiJson.dump(options) # to json
JSON.parse(RestClient.post(post_api_url, options))
def http_post(url, payload={}, headers={}, endpoint="plain")
post_api_url = endpoint_url(endpoint) + url
payload = MultiJson.dump(payload) if endpoint == "plain" # to json
post_params = {:params => access_token_param.merge(headers)}
JSON.parse(RestClient.post(post_api_url, payload, post_params))
end
def endpoint_url(endpoint)
......
......@@ -11,8 +11,13 @@ describe WeixinAuthorize::Api::Groups do
it "create a group" do
response = $client.create_group(group_name)
if !response["errcode"].nil?
expect(response.keys).to eq(["errcode", "errmsg"])
puts "SB WEIXIN says: system error"
else
expect(response["group"]["name"]).to eq(group_name)
end
end
it "get groups" do
groups = $client.groups
......@@ -26,12 +31,17 @@ describe WeixinAuthorize::Api::Groups do
it "#update_group_name" do
response = $client.create_group(group_name)
if !response["errcode"].nil?
expect(response.keys).to eq(["errcode", "errmsg"])
puts "SB WEIXIN says: system error"
else
expect(response["group"]["name"]).to eq(group_name)
response = $client.update_group_name(response["group"]["id"], group_name_2)
expect(response["errcode"]).to eq(0)
groups = $client.groups
expect(groups["groups"][-1]["name"]).to eq(group_name_2)
end
end
it "#update_group_for_openid" do
groups = $client.groups
......
......@@ -15,11 +15,11 @@ describe WeixinAuthorize::Api::Media do
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
it "#download_media_url return a String url" do
image = $client.upload_media(image_file, "image")
media_id = image["media_id"]
response = $client.download_media_url(media_id)
expect(response.class).to eq(String)
end
end
......@@ -11,12 +11,10 @@ describe WeixinAuthorize::Api::User do
expect(valid_info).to eq(true)
followers = $client.followers
expect(followers.keys).to eq(["total", "count", "data", "next_openid"])
puts valid_info
valid_info = $client.is_valid?
expect(valid_info).to eq(true)
followers = $client.followers
expect(followers.keys).to eq(["total", "count", "data", "next_openid"])
puts valid_info
end
end
......@@ -19,8 +19,6 @@ require "multi_json"
require "redis"
require "redis-namespace"
require "pry-rails"
redis = Redis.new(:host => "127.0.0.1",:port => "6379")
namespace = "weixin_test:weixin_authorize"
......
......@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "redis-namespace", "~> 1.4.1"
spec.add_development_dependency "rspec", "~> 3.0.0.beta1"
......
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