weixin_authorize 1.1.0 → 1.5.0

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: 46bd1a765537e643f77dd3b7a112160131f031e5
4
- data.tar.gz: 40e96c73ebc3123b461986e673e504dc530e42c6
3
+ metadata.gz: 194d7f3905340f75447ec9401d845af02eedf8a8
4
+ data.tar.gz: 448c9b7656d7100801514abf18f5213456f28b01
5
5
  SHA512:
6
- metadata.gz: 72fe71c1509bcdaa2d8d5f58f325f03df41f1d996c6ce008a6bbafeb38ebe8b4261f2745fc0832cd7f49cffa4607168a14af5e5e9edea30f31cca43f414963a9
7
- data.tar.gz: 67f3c7642241afd83736d5702c93e3f5be1d4dbccaa13c2a78b59f985de0629f09f723ef70404952ee4b6a8dcadfe0853869637be519dcdc496943983ec16aa9
6
+ metadata.gz: aa2e813b5c406da0bf28e9987db44318ab369fc41401b3e12158cdce540f04d8e827f5cf777123a40a3c6a6ab246749aa6143c55e859f1ba2dee8d4c51665737
7
+ data.tar.gz: e7eac68aec0577c93f10dd5e1634d9a58d5bca61d667c6fcfbbe5dd50975e5d7e6aa5b15d3085ec42bcb1ba122da9d13327c701196d230f3a6b10c71b99d78ea
data/Gemfile CHANGED
@@ -7,4 +7,5 @@ group :development, :test do
7
7
  # For debugger
8
8
  gem "pry-rails", "~> 0.3.2"
9
9
  gem "pry-debugger", "~> 0.2.2"
10
+ gem "redis-namespace"
10
11
  end
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # WeixinAuthorize
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/weixin_authorize.png)](http://badge.fury.io/rb/weixin_authorize)
4
+
5
+ Support using [Redis](http://redis.io) to store `access_token`
6
+
3
7
  ## Installation
4
8
 
5
9
  Add this line to your application's Gemfile:
@@ -24,18 +28,51 @@ Or install it yourself as:
24
28
 
25
29
  ```ruby
26
30
 
27
- $client ||= WeixinAuthorize.configure do |config|
28
- config.app_id = ENV["APPID"]
29
- config.app_secret = ENV["APPSECRET"]
30
- config.expired_at = Time.now.to_i
31
- end
31
+ $client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
32
+ ```
32
33
 
33
- # Or
34
+ ### Option: use [Redis](http://redis.io) to store your access_token (Recommend)
34
35
 
35
- $client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
36
+ **If you don't use Redis, it will send a request to get a new access_token everytime!**
37
+
38
+ * Added `redis-namespace` to you `Gemfile`
39
+
40
+ ```ruby
41
+ # Adds a Redis::Namespace class which can be used to namespace calls to Redis. This is useful when using a single instance of Redis with multiple, different applications.
42
+ # http://github.com/resque/redis-namespace
43
+ gem "redis-namespace", "~> 1.4.1"
44
+
45
+ ```
46
+
47
+ * Create file in: `config/initializers/weixin_authorize.rb`
48
+
49
+ ```ruby
50
+
51
+ # don't forget change namespace
52
+ namespace = "app_name_weixin:weixin_authorize"
53
+ redis = Redis.new(:host => "127.0.0.1", :port => "6379", :db => 15)
54
+
55
+ # cleanup keys in the current namespace when restart server everytime.
56
+ exist_keys = redis.keys("#{namespace}:*")
57
+ exist_keys.each{|key|redis.del(key)}
58
+
59
+ # Give a special namespace as prefix for Redis key, when your have more than one project used weixin_authorize, this config will make them work fine.
60
+ redis = Redis::Namespace.new("#{namespace}", :redis => redis)
61
+
62
+ WeixinAuthorize.configure do |config|
63
+ config.redis = redis
64
+ end
36
65
 
37
66
  ```
38
67
 
68
+ * You can also specify the `key`, but it is optionly.
69
+
70
+ ```ruby
71
+
72
+ $client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"], "your_store_key")
73
+ ```
74
+ **Note:** `your_store_key` should be unique for every account!
75
+
39
76
  ### 获取用户管理信息
40
77
 
41
78
  * [获取用户基本信息](http://mp.weixin.qq.com/wiki/index.php?title=获取用户基本信息)
@@ -46,7 +83,6 @@ $client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
46
83
 
47
84
  `followers = $client.followers`
48
85
 
49
-
50
86
  ### [分组管理接口](http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口)
51
87
 
52
88
  * 创建分组:
@@ -119,9 +155,6 @@ export OPENID="your weixin openid"
119
155
  ```
120
156
  Last, you have to **open a new terminal tag (Reload bash_profile)** , and run `rspec .`
121
157
 
122
- ## 多用户微信营销平台的对接
123
-
124
- > 对于多用户微信营销平台的对接,需要把每次的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`来获取.
125
158
 
126
159
  ## Contributing
127
160
 
@@ -1,4 +1,6 @@
1
1
  # encoding: utf-8
2
+ require "redis"
3
+ require 'digest'
2
4
  module WeixinAuthorize
3
5
 
4
6
  class Client
@@ -8,40 +10,62 @@ module WeixinAuthorize
8
10
  include Api::Groups
9
11
 
10
12
  attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
11
- attr_accessor :access_token
13
+ attr_accessor :access_token, :redis_key
12
14
 
13
15
  # 对于多用户微信营销平台的对接,需要把每次的expired_at, access_token保存在Redis中
14
16
  # 每次使用,则可以从Redis中获取expired_at和access_token,即
15
- # @client = WeixinAuthorize::Client.new(appid, appsecret, expired_at, access_token)
17
+ # @client = WeixinAuthorize::Client.new(appid, appsecret)
18
+ # 如果使用存在多个公众账号,请使用 务必传递: redis_key
16
19
  # 获取access_token,则仍然是:@client.get_access_token 来获取
17
- def initialize(app_id="", app_secret="", expired_at=nil, access_token=nil)
18
- @app_id = app_id
19
- @app_secret = app_secret
20
- @expired_at = (expired_at.to_i || Time.now.to_i)
21
- @access_token = access_token
22
- yield self if block_given?
20
+ def initialize(app_id, app_secret, redis_key=nil)
21
+ @app_id = app_id
22
+ @app_secret = app_secret
23
+ @expired_at = Time.now.to_i
24
+ @redis_key = security_redis_key((redis_key || "weixin_" + app_id))
23
25
  end
24
26
 
25
27
  # return token
26
28
  def get_access_token
27
- # 如果当前token过期时间小于现在的时间,则重新获取一次
28
29
  authenticate if token_expired?
30
+ if !is_weixin_redis_blank?
31
+ self.access_token = weixin_redis.hget(redis_key, "access_token")
32
+ self.expired_at = weixin_redis.hget(redis_key, "expired_at")
33
+ end
29
34
  @access_token
30
35
  end
31
36
 
32
37
  # authenticate access_token
33
38
  def authenticate
34
- hash_infos = http_get_without_token("/token", authenticate_options)
35
- self.access_token = hash_infos["access_token"]
36
- self.expired_at = Time.now.to_i + hash_infos["expires_in"]
39
+ if is_weixin_redis_blank?
40
+ http_get_access_token
41
+ else
42
+ authenticate_with_redis
43
+ end
37
44
  end
38
45
 
39
46
  def token_expired?
40
- @expired_at <= Time.now.to_i
47
+ if is_weixin_redis_blank?
48
+ # 如果当前token过期时间小于现在的时间,则重新获取一次
49
+ @expired_at <= Time.now.to_i
50
+ else
51
+ weixin_redis.hvals(redis_key).empty?
52
+ end
41
53
  end
42
54
 
43
55
  private
44
56
 
57
+ def authenticate_with_redis
58
+ http_get_access_token
59
+ weixin_redis.hmset(redis_key, :access_token, access_token, :expired_at, expired_at)
60
+ weixin_redis.expireat(redis_key, expired_at.to_i-10) # 提前10秒超时
61
+ end
62
+
63
+ def http_get_access_token
64
+ hash_infos = http_get_without_token("/token", authenticate_options)
65
+ self.access_token = hash_infos["access_token"]
66
+ self.expired_at = Time.now.to_i + hash_infos["expires_in"]
67
+ end
68
+
45
69
  def authenticate_options
46
70
  {grant_type: "client_credential", appid: app_id, secret: app_secret}
47
71
  end
@@ -79,5 +103,18 @@ module WeixinAuthorize
79
103
  "http://file.api.weixin.qq.com/cgi-bin"
80
104
  end
81
105
 
106
+ def weixin_redis
107
+ return nil if WeixinAuthorize.config.nil?
108
+ @redis ||= WeixinAuthorize.config.redis
109
+ end
110
+
111
+ def is_weixin_redis_blank?
112
+ weixin_redis.nil?
113
+ end
114
+
115
+ def security_redis_key(key)
116
+ Digest::MD5.hexdigest(key.to_s).upcase
117
+ end
118
+
82
119
  end
83
120
  end
@@ -0,0 +1,15 @@
1
+ module WeixinAuthorize
2
+
3
+ class << self
4
+
5
+ attr_accessor :config
6
+
7
+ def configure
8
+ yield self.config ||= Config.new
9
+ end
10
+ end
11
+
12
+ class Config
13
+ attr_accessor :redis
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module WeixinAuthorize
2
- VERSION = "1.1.0"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require "rest-client"
2
2
  require "multi_json"
3
- require "weixin_authorize/version"
3
+ require "weixin_authorize/config"
4
4
  require "weixin_authorize/api/user"
5
5
  require "weixin_authorize/api/menu"
6
6
  require "weixin_authorize/api/custom"
@@ -9,15 +9,4 @@ require "weixin_authorize/client"
9
9
 
10
10
  module WeixinAuthorize
11
11
 
12
- # @client ||= WeixinAuthorize.configure do |config|
13
- # config.app_id = "app_id-xxxxxxx"
14
- # config.app_secret = "app_secret-xxxxxxx"
15
- # end
16
- #
17
- class << self
18
- def configure(&block)
19
- Client.new(&block)
20
- end
21
- end
22
-
23
12
  end
data/spec/spec_helper.rb CHANGED
@@ -16,14 +16,17 @@
16
16
  require "rspec"
17
17
  require "weixin_authorize"
18
18
  require "multi_json"
19
+ require "redis"
20
+ require "redis-namespace"
19
21
 
20
22
  require "pry-rails"
21
23
 
22
- # $client ||= WeixinAuthorize.configure do |config|
23
- # config.app_id = ENV["APPID"]
24
- # config.app_secret = ENV["APPSECRET"]
25
- # config.expired_at = Time.now.to_i
26
- # end
24
+ redis = Redis.new(:host => "127.0.0.1",:port => "6379")
25
+
26
+ redis_with_ns = Redis::Namespace.new("weixin_#{Time.now.to_i}:weixin_authorize", :redis => redis)
27
+ WeixinAuthorize.configure do |config|
28
+ config.redis = redis_with_ns
29
+ end
27
30
 
28
31
  $client = WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
29
32
 
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "rest-client", ">= 1.6.7"
22
22
  spec.add_dependency "multi_json", "~> 1.9.0"
23
+ spec.add_dependency "redis", "~> 3.0.7"
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.3"
25
26
  spec.add_development_dependency "rake"
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.1.0
4
+ version: 1.5.0
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-20 00:00:00.000000000 Z
11
+ date: 2014-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.9.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.7
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -99,6 +113,7 @@ files:
99
113
  - lib/weixin_authorize/api/menu.rb
100
114
  - lib/weixin_authorize/api/user.rb
101
115
  - lib/weixin_authorize/client.rb
116
+ - lib/weixin_authorize/config.rb
102
117
  - lib/weixin_authorize/version.rb
103
118
  - spec/1_fetch_access_token_spec.rb
104
119
  - spec/api/custom_spec.rb