Commit d2df2d80 by lanrion

refactor groups.rb

parent 27e67364
......@@ -7,8 +7,7 @@ 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"
group = MultiJson.dump({group: {name: group_name}})
http_post(create_url, group)
http_post(create_url, {group: {name: group_name}})
end
# 查询所有分组
......@@ -22,24 +21,21 @@ module WeixinAuthorize
# https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN
def get_group_for(openid)
group_url = "#{group_base_url}/getid"
openid = MultiJson.dump({openid: openid})
http_post(group_url, openid)
http_post(group_url, {openid: 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"
group = MultiJson.dump({group: {id: openid, name: new_group_name}})
http_post(group_url, group)
http_post(group_url, {group: {id: group_id, name: new_group_name}})
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"
group = MultiJson.dump({openid: openid, to_groupid: to_groupid})
http_post(group_url, group)
http_post(group_url, {openid: openid, to_groupid: to_groupid})
end
private
......
require "spec_helper"
describe WeixinAuthorize::Api::Groups do
let(:group_name) do
"test group_name"
end
let(:group_name_2) do
"test group_name_2"
end
it "create a group" do
response = $client.create_group("test")
puts response
response = $client.create_group(group_name)
expect(response["group"]["name"]).to eq(group_name)
end
it "get groups" do
groups = $client.groups
puts groups
expect(groups["groups"][-1]["name"]).to eq(group_name)
end
it "#get_group_for ENV['OPENID']" do
group = $client.get_group_for(ENV["OPENID"])
expect(group.keys).to eq(["groupid"])
end
it "#update_group_name" do
response = $client.create_group(group_name)
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
it "#update_group_for_openid" do
groups = $client.groups
last_group_id = groups["groups"][-1]["id"]
$client.update_group_for_openid(ENV["OPENID"], last_group_id)
group = $client.get_group_for(ENV["OPENID"])
expect(group["groupid"]).to eq(last_group_id)
$client.update_group_for_openid(ENV["OPENID"], 0)
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