weixin_authorize 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 597087b2445885d7272fe48b6dfae8b4ade852e7
4
- data.tar.gz: 091d7d77fa863078e05f9b0a86aa3adc45896723
3
+ metadata.gz: df74fa58c1890b6aa3ac5d5684b3542d368d521a
4
+ data.tar.gz: c296a7894161afd1bdda7240945da3529e1982cc
5
5
  SHA512:
6
- metadata.gz: 0515f0d280d6e995e7857ab3d7f8311b16895e6424755b9f7cc55035d198e75a7687968c7bf816f6365adc59364bc2361e471e4ae0cf3a58feccc430dafe27f7
7
- data.tar.gz: 8cea693671811943eb41fc4471d83d31a10f79ba3a86f5a937ef8752f766f223839c5ed5e47f0df28d434e06071d14358a14c60700eaacae1111c1a8ae68dc7e
6
+ metadata.gz: ba04e33192c97ce67831fe80dce94c472405aaaa670e0e70ee946dbd086a533ece2f202e852919d1c96d5233a6c31bfd0632025883a9bb57b81c1b29d695df33
7
+ data.tar.gz: b6c34f7484b0839220ea0353b63749796e5950bf9eb5455d1fbecc7399f83803e5af5dc51e46f7deb7adf19826024b913fa653730b11e2b9a55e858829bfb102
data/README.md CHANGED
@@ -104,6 +104,10 @@ $client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
104
104
 
105
105
  `$client.send_news_custom(to_user, *articles)`
106
106
 
107
+ ##
108
+
109
+ > 对于多用户微信营销平台的对接,需要把每次的expired_at, access_token保存在Redis中,每次使用,则可以从Redis中获取expired_at和access_token, 即 `@client = WeixinAuthorize::Client.new(appid, appsecret, expired_at, access_token)`, 获取access_token,则仍然是:`@client.get_access_token`来获取.
110
+
107
111
  ## Contributing
108
112
 
109
113
  1. Fork it
@@ -1,5 +1,4 @@
1
1
  # encoding: utf-8
2
-
3
2
  module WeixinAuthorize
4
3
  module Api
5
4
  module Custom
@@ -15,7 +14,7 @@ module WeixinAuthorize
15
14
  # }
16
15
  def send_text_custom(to_user, content)
17
16
  message = default_options(to_user).merge({text: {content: content}})
18
- JSON.parse(RestClient.post(custom_base_url, MultiJson.dump(message)))
17
+ http_post(custom_base_url, MultiJson.dump(message))
19
18
  end
20
19
 
21
20
  # 发送图片消息
@@ -29,7 +28,7 @@ module WeixinAuthorize
29
28
  # }
30
29
  def send_image_custom(to_user, media_id)
31
30
  message = default_options(to_user).merge({msgtype: image, image: {media_id: media_id}})
32
- JSON.parse(RestClient.post(custom_base_url, MultiJson.dump(message)))
31
+ http_post(custom_base_url, MultiJson.dump(message))
33
32
  end
34
33
 
35
34
  # 发送语音消息
@@ -43,7 +42,7 @@ module WeixinAuthorize
43
42
  # }
44
43
  def send_voice_custom(to_user, media_id)
45
44
  message = default_options(to_user).merge({msgtype: voice, voice: {media_id: media_id}})
46
- JSON.parse(RestClient.post(custom_base_url, MultiJson.dump(message)))
45
+ http_post(custom_base_url, MultiJson.dump(message))
47
46
  end
48
47
 
49
48
  # 发送视频消息
@@ -55,10 +54,10 @@ module WeixinAuthorize
55
54
  # "media_id":"MEDIA_ID"
56
55
  # }
57
56
  # }
58
- def send_video_custom(to_user, media_id, options)
57
+ def send_video_custom(to_user, media_id, options={})
59
58
  video_options = {media_id: media_id}.merge(options)
60
59
  message = default_options(to_user).merge({msgtype: video, video: video_options})
61
- JSON.parse(RestClient.post(custom_base_url, MultiJson.dump(message)))
60
+ http_post(custom_base_url, MultiJson.dump(message))
62
61
  end
63
62
 
64
63
  # 发送音乐消息
@@ -74,11 +73,11 @@ module WeixinAuthorize
74
73
  # "thumb_media_id":"THUMB_MEDIA_ID"
75
74
  # }
76
75
  # }
77
- def send_music_custom(to_user, media_id, musicurl, hqmusicurl, options)
76
+ def send_music_custom(to_user, media_id, musicurl, hqmusicurl, options={})
78
77
  music_options = { thumb_media_id: media_id, musicurl: musicurl,
79
78
  hqmusicurl: hqmusicurl}.merge(options)
80
79
  message = default_options(to_user).merge({msgtype: music, music: music_options})
81
- JSON.parse(RestClient.post(custom_base_url, MultiJson.dump(message)))
80
+ http_post(custom_base_url, MultiJson.dump(message))
82
81
  end
83
82
 
84
83
  # 发送图文消息
@@ -104,14 +103,14 @@ module WeixinAuthorize
104
103
  # }
105
104
  def send_news_custom(to_user, *articles)
106
105
  message = default_options(to_user).merge({msgtype: "news", news: {articles: articles}})
107
- JSON.parse(RestClient.post(custom_base_url, MultiJson.dump(message)))
106
+ http_post(custom_base_url, MultiJson.dump(message))
108
107
  end
109
108
 
110
109
  private
111
110
 
112
111
  # https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
113
112
  def custom_base_url
114
- "#{endpoint}/message/custom/send?#{access_token_param}"
113
+ "/message/custom/send"
115
114
  end
116
115
 
117
116
  def default_options(to_user)
@@ -6,46 +6,46 @@ module WeixinAuthorize
6
6
  # 创建分组
7
7
  # https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN
8
8
  def create_group(group_name)
9
- create_url = "#{group_base_url}/create?#{access_token_param}"
9
+ create_url = "#{group_base_url}/create"
10
10
  group = MultiJson.dump({group: {name: group_name}})
11
- JSON.parse(RestClient.post(create_url, group))
11
+ http_post(create_url, group)
12
12
  end
13
13
 
14
14
  # 查询所有分组
15
15
  # https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN
16
16
  def groups
17
- groups_url = "#{group_base_url}/get?#{access_token_param}"
18
- JSON.parse(RestClient.get(groups_url))
17
+ groups_url = "#{group_base_url}/get"
18
+ http_get(groups_url)
19
19
  end
20
20
 
21
21
  # 查询用户所在分组
22
22
  # https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN
23
23
  def get_group_for(openid)
24
- group_url = "#{group_base_url}/getid?#{access_token_param}"
24
+ group_url = "#{group_base_url}/getid"
25
25
  openid = MultiJson.dump({openid: openid})
26
- JSON.parse(RestClient.post(group_url, openid))
26
+ http_post(group_url, openid)
27
27
  end
28
28
 
29
29
  # 修改分组名
30
30
  # https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN
31
31
  def update_group_name(group_id, new_group_name)
32
- group_url = "#{group_base_url}/update?#{access_token_param}"
32
+ group_url = "#{group_base_url}/update"
33
33
  group = MultiJson.dump({group: {id: openid, name: new_group_name}})
34
- JSON.parse(RestClient.post(group_url, group))
34
+ http_post(group_url, group)
35
35
  end
36
36
 
37
37
  # 移动用户分组
38
38
  # https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN
39
39
  def update_group_for_openid(openid, to_groupid)
40
- group_url = "#{group_base_url}/members/update?#{access_token_param}"
40
+ group_url = "#{group_base_url}/members/update"
41
41
  group = MultiJson.dump({openid: openid, to_groupid: to_groupid})
42
- JSON.parse(RestClient.post(group_url, group))
42
+ http_post(group_url, group)
43
43
  end
44
44
 
45
45
  private
46
46
 
47
47
  def group_base_url
48
- "#{endpoint}/groups"
48
+ "/groups"
49
49
  end
50
50
 
51
51
  end
@@ -6,28 +6,28 @@ module WeixinAuthorize
6
6
  # 自定义菜单查询接口
7
7
  # https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN
8
8
  def menu
9
- get_menu_url = "#{menu_base_url}/get?#{access_token_param}"
10
- JSON.parse(RestClient.get(get_menu_url))
9
+ get_menu_url = "#{menu_base_url}/get"
10
+ http_get(get_menu_url)
11
11
  end
12
12
 
13
13
  # 自定义菜单删除接口
14
14
  # https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
15
15
  def delete_menu
16
- delete_menu_url = "#{menu_base_url}/delete?#{access_token_param}"
17
- JSON.parse(RestClient.get(delete_menu_url))
16
+ delete_menu_url = "#{menu_base_url}/delete"
17
+ http_get(delete_menu_url)
18
18
  end
19
19
 
20
20
  # 自定义菜单创建接口
21
21
  # https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
22
22
  def create_menu(menu)
23
- create_menu_url = "#{menu_base_url}/create?#{access_token_param}"
24
- JSON.parse(RestClient.post(create_menu_url, menu))
23
+ create_menu_url = "#{menu_base_url}/create"
24
+ http_post(create_menu_url, menu)
25
25
  end
26
26
 
27
27
  private
28
28
 
29
29
  def menu_base_url
30
- "#{endpoint}/menu"
30
+ "/menu"
31
31
  end
32
32
 
33
33
  end
@@ -7,21 +7,21 @@ module WeixinAuthorize
7
7
  # https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
8
8
  # lang: zh_CN, zh_TW, en
9
9
  def user(openid, lang="zh_CN")
10
- user_info_url = "#{user_base_url}/info?#{access_token_param}&openid=#{openid}&lang=#{lang}"
11
- JSON.parse(RestClient.get(user_info_url))
10
+ user_info_url = "#{user_base_url}/info"
11
+ http_get(user_info_url, {openid: openid, lang: lang})
12
12
  end
13
13
 
14
- # https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
15
14
  # 获取关注者列表
16
- def followers(next_openid=nil)
17
- users_url = "#{user_base_url}/get?#{access_token_param}&next_openid#{next_openid}"
18
- JSON.parse(RestClient.get(users_url))
15
+ # https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
16
+ def followers(next_openid="")
17
+ followers_url = "#{user_base_url}/get"
18
+ http_get(followers_url, {next_openid: next_openid})
19
19
  end
20
20
 
21
21
  private
22
22
 
23
23
  def user_base_url
24
- "#{endpoint}/user"
24
+ "/user"
25
25
  end
26
26
 
27
27
  end
@@ -1,5 +1,4 @@
1
1
  # encoding: utf-8
2
-
3
2
  module WeixinAuthorize
4
3
 
5
4
  class Client
@@ -11,41 +10,72 @@ module WeixinAuthorize
11
10
  attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
12
11
  attr_accessor :access_token
13
12
 
14
- def initialize(app_id="", app_secret="", expired_at=nil)
13
+ # 对于多用户微信营销平台的对接,需要把每次的expired_at, access_token保存在Redis中
14
+ # 每次使用,则可以从Redis中获取expired_at和access_token,即
15
+ # @client = WeixinAuthorize::Client.new(appid, appsecret, expired_at, access_token)
16
+ # 获取access_token,则仍然是:@client.get_access_token 来获取
17
+ def initialize(app_id="", app_secret="", expired_at=nil, access_token=nil)
15
18
  @app_id = app_id
16
19
  @app_secret = app_secret
17
20
  @expired_at = (expired_at.to_i || Time.now.to_i)
21
+ @access_token = access_token
18
22
  yield self if block_given?
19
23
  end
20
24
 
21
25
  # return token
22
26
  def get_access_token
23
27
  # 如果当前token过期时间小于现在的时间,则重新获取一次
24
- if @expired_at <= Time.now.to_i
25
- authenticate
26
- end
28
+ authenticate if token_expired?
27
29
  @access_token
28
30
  end
29
31
 
30
32
  # authenticate access_token
31
33
  def authenticate
32
- hash_infos = JSON.parse(RestClient.get(authenticate_url))
34
+ hash_infos = http_get_without_token("/token", authenticate_options)
33
35
  self.access_token = hash_infos["access_token"]
34
36
  self.expired_at = Time.now.to_i + hash_infos["expires_in"]
35
37
  end
36
38
 
39
+ def token_expired?
40
+ @expired_at <= Time.now.to_i
41
+ end
42
+
37
43
  private
38
44
 
39
- def authenticate_url
40
- "#{endpoint}/token?grant_type=client_credential&appid=#{app_id}&secret=#{app_secret}"
45
+ def authenticate_options
46
+ {grant_type: "client_credential", appid: app_id, secret: app_secret}
47
+ end
48
+
49
+ def access_token_param
50
+ {access_token: get_access_token}
51
+ end
52
+
53
+ def http_get_without_token(url, options={}, endpoint="plain")
54
+ get_api_url = endpoint_url(endpoint) + url
55
+ JSON.parse(RestClient.get(get_api_url, :params => options))
56
+ end
57
+
58
+ def http_get(url, options={})
59
+ options = options.merge(access_token_param)
60
+ http_get_without_token(url, options)
61
+ end
62
+
63
+ # Refactor
64
+ def http_post(url, options={}, endpoint="plain")
65
+ post_api_url = endpoint_url(endpoint) + url + "?access_token=#{get_access_token}"
66
+ JSON.parse(RestClient.post(post_api_url ,options))
67
+ end
68
+
69
+ def endpoint_url(endpoint)
70
+ send("#{endpoint}_endpoint")
41
71
  end
42
72
 
43
- def endpoint
73
+ def plain_endpoint
44
74
  "https://api.weixin.qq.com/cgi-bin"
45
75
  end
46
76
 
47
- def access_token_param
48
- "access_token=#{get_access_token}"
77
+ def file_endpoint
78
+ "http://file.api.weixin.qq.com/cgi-bin"
49
79
  end
50
80
 
51
81
  end
@@ -1,3 +1,3 @@
1
1
  module WeixinAuthorize
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -2,6 +2,11 @@ require "spec_helper"
2
2
  describe WeixinAuthorize::Api::Groups do
3
3
  it "create a group" do
4
4
  response = $client.create_group("test")
5
- response
5
+ puts response
6
+ end
7
+
8
+ it "get groups" do
9
+ groups = $client.groups
10
+ puts groups
6
11
  end
7
12
  end
@@ -6,7 +6,9 @@ describe WeixinAuthorize::Api::Menu do
6
6
  it "can create a menu" do
7
7
  menu = '{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},{"type":"click","name":"歌手简介","key":"V1001_TODAY_SINGER"},{"name":"菜单","sub_button":[{"type":"view","name":"搜索","url":"http://www.soso.com/"},{"type":"view","name":"视频","url":"http://v.qq.com/"},{"type":"click","name":"赞一下我们","key":"V1001_GOOD"}]}]}'
8
8
  response = $client.create_menu(menu)
9
+ puts response
9
10
  expect(response["errcode"]).to eq(0)
11
+
10
12
  end
11
13
 
12
14
  it "can get a weixin Menu info" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weixin_authorize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - lanrion
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-15 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -100,11 +100,11 @@ files:
100
100
  - lib/weixin_authorize/api/user.rb
101
101
  - lib/weixin_authorize/client.rb
102
102
  - lib/weixin_authorize/version.rb
103
+ - spec/1_fetch_access_token_spec.rb
103
104
  - spec/api/custom_spec.rb
104
105
  - spec/api/groups_spec.rb
105
106
  - spec/api/menu_spec.rb
106
107
  - spec/api/user_spec.rb
107
- - spec/fetch_access_token_spec.rb
108
108
  - spec/spec_helper.rb
109
109
  - weixin_authorize.gemspec
110
110
  homepage: ''
@@ -132,9 +132,9 @@ signing_key:
132
132
  specification_version: 4
133
133
  summary: weixin api authorize access_token
134
134
  test_files:
135
+ - spec/1_fetch_access_token_spec.rb
135
136
  - spec/api/custom_spec.rb
136
137
  - spec/api/groups_spec.rb
137
138
  - spec/api/menu_spec.rb
138
139
  - spec/api/user_spec.rb
139
- - spec/fetch_access_token_spec.rb
140
140
  - spec/spec_helper.rb