Commit 5bd539ea by lanrion

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

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