Commit 5bd539ea by lanrion

refactor groups.rb with "http_get" and "http_post" method

parent 3539338f
......@@ -6,46 +6,48 @@ module WeixinAuthorize
# 创建分组
# https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN
def create_group(group_name)
create_url = "#{group_base_url}/create?#{access_token_param}"
create_url = "#{group_base_url}/create"
group = MultiJson.dump({group: {name: group_name}})
JSON.parse(RestClient.post(create_url, group))
http_post(create_url, group)
end
# 查询所有分组
# https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN
def groups
groups_url = "#{group_base_url}/get?#{access_token_param}"
JSON.parse(RestClient.get(groups_url))
groups_url = "#{group_base_url}/get"
http_get(groups_url)
end
# 查询用户所在分组
# https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN
def get_group_for(openid)
group_url = "#{group_base_url}/getid?#{access_token_param}"
group_url = "#{group_base_url}/getid"
openid = MultiJson.dump({openid: openid})
JSON.parse(RestClient.post(group_url, openid))
http_post(group_url, openid)
end
# 修改分组名
# https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN
def update_group_name(group_id, new_group_name)
group_url = "#{group_base_url}/update?#{access_token_param}"
group_url = "#{group_base_url}/update"
group = MultiJson.dump({group: {id: openid, name: new_group_name}})
JSON.parse(RestClient.post(group_url, group))
http_post(group_url, group)
end
# 移动用户分组
# https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN
def update_group_for_openid(openid, to_groupid)
group_url = "#{group_base_url}/members/update?#{access_token_param}"
group_url = "#{group_base_url}/members/update"
group = MultiJson.dump({openid: openid, to_groupid: to_groupid})
JSON.parse(RestClient.post(group_url, group))
http_post(group_url, group)
end
private
def group_base_url
"#{endpoint}/groups"
"/groups"
end
end
......
......@@ -2,6 +2,11 @@ require "spec_helper"
describe WeixinAuthorize::Api::Groups do
it "create a group" do
response = $client.create_group("test")
response
puts response
end
it "get groups" do
groups = $client.groups
puts groups
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