weixin_authorize 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/weixin_authorize/client.rb +27 -11
- data/lib/weixin_authorize/version.rb +1 -1
- data/spec/1_fetch_access_token_spec.rb +14 -3
- data/spec/spec_helper.rb +8 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bd5cb5899a192d80fe3b410c4a15540146603bb
|
4
|
+
data.tar.gz: 1a59ba515e57a12e9181c550aed3bbfbb838ee50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a88ff15ed28e94b9338fcdaf000d4cfb7e6cb5db78a8cb644cab435ab454961d3505851d3d146bd62407a2fe8a88fca6c312ce7ca63cddc811b150e83e6ae4b
|
7
|
+
data.tar.gz: ea555c6636e1b7ea42e1dca10ccd1d5fb15a4f95b3d59d98d279bcb1d02ff70b45e591d4cb04145cf70488015cde62182ed1db6db94c938f9ca453435884e78b
|
data/README.md
CHANGED
@@ -28,7 +28,11 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
|
31
|
+
# new a client
|
31
32
|
$client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
|
33
|
+
|
34
|
+
# valid app_id and app_secret
|
35
|
+
$client.is_valid? # return true or false
|
32
36
|
```
|
33
37
|
|
34
38
|
### Option: use [Redis](http://redis.io) to store your access_token (Recommend)
|
@@ -34,13 +34,14 @@ module WeixinAuthorize
|
|
34
34
|
@access_token
|
35
35
|
end
|
36
36
|
|
37
|
-
#
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
37
|
+
# 检查appid和app_secret是否有效。
|
38
|
+
def is_valid?
|
39
|
+
valid_result = http_get_access_token
|
40
|
+
if valid_result.keys.include?("access_token")
|
41
|
+
set_access_token_for_client(valid_result)
|
42
|
+
return true
|
43
|
+
end
|
44
|
+
false
|
44
45
|
end
|
45
46
|
|
46
47
|
def token_expired?
|
@@ -54,16 +55,31 @@ module WeixinAuthorize
|
|
54
55
|
|
55
56
|
private
|
56
57
|
|
58
|
+
# authenticate access_token
|
59
|
+
def authenticate
|
60
|
+
raise "APPID or APPSECRET is invalid" if !is_valid?
|
61
|
+
if is_weixin_redis_blank?
|
62
|
+
set_access_token_for_client
|
63
|
+
else
|
64
|
+
authenticate_with_redis
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
57
68
|
def authenticate_with_redis
|
58
|
-
|
69
|
+
set_access_token_for_client
|
59
70
|
weixin_redis.hmset(redis_key, :access_token, access_token, :expired_at, expired_at)
|
60
71
|
weixin_redis.expireat(redis_key, expired_at.to_i-10) # 提前10秒超时
|
61
72
|
end
|
62
73
|
|
74
|
+
def set_access_token_for_client(access_token_infos=nil)
|
75
|
+
token_infos = access_token_infos || http_get_access_token
|
76
|
+
self.access_token = token_infos["access_token"]
|
77
|
+
self.expired_at = Time.now.to_i + token_infos["expires_in"]
|
78
|
+
end
|
79
|
+
|
63
80
|
def http_get_access_token
|
64
|
-
hash_infos
|
65
|
-
|
66
|
-
self.expired_at = Time.now.to_i + hash_infos["expires_in"]
|
81
|
+
hash_infos = http_get_without_token("/token", authenticate_options)
|
82
|
+
hash_infos
|
67
83
|
end
|
68
84
|
|
69
85
|
def authenticate_options
|
@@ -6,9 +6,9 @@ describe WeixinAuthorize::Client do
|
|
6
6
|
expect($client.access_token).to eq(nil)
|
7
7
|
end
|
8
8
|
|
9
|
-
it "
|
10
|
-
$client.
|
11
|
-
expect(
|
9
|
+
it "appid and appsecret shoud be valid" do
|
10
|
+
valid_info = $client.is_valid?
|
11
|
+
expect(valid_info).to eq(true)
|
12
12
|
end
|
13
13
|
|
14
14
|
it "return the same access_token in the same thing twice" do
|
@@ -17,5 +17,16 @@ describe WeixinAuthorize::Client do
|
|
17
17
|
access_token_2 = $client.get_access_token
|
18
18
|
expect(access_token_1).to eq(access_token_2)
|
19
19
|
end
|
20
|
+
|
21
|
+
it "return errorcode and errormsg when appid or appsecret is invalid" do
|
22
|
+
$client_1 = WeixinAuthorize::Client.new("appid", "app_secret")
|
23
|
+
valid_info = $client_1.is_valid?
|
24
|
+
expect(valid_info).to eq(false)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "#get_access_token should raise error if app_secret or appid is invalid" do
|
28
|
+
$client_2 = WeixinAuthorize::Client.new("appid_2", "app_secret_2")
|
29
|
+
expect{$client_2.get_access_token}.to raise_error(RuntimeError)
|
30
|
+
end
|
20
31
|
end
|
21
32
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -23,13 +23,19 @@ require "pry-rails"
|
|
23
23
|
|
24
24
|
redis = Redis.new(:host => "127.0.0.1",:port => "6379")
|
25
25
|
|
26
|
-
|
26
|
+
namespace = "weixin_test:weixin_authorize"
|
27
|
+
|
28
|
+
# cleanup keys in the current namespace when restart server everytime.
|
29
|
+
exist_keys = redis.keys("#{namespace}:*")
|
30
|
+
exist_keys.each{|key|redis.del(key)}
|
31
|
+
|
32
|
+
redis_with_ns = Redis::Namespace.new("#{namespace}", :redis => redis)
|
33
|
+
|
27
34
|
WeixinAuthorize.configure do |config|
|
28
35
|
config.redis = redis_with_ns
|
29
36
|
end
|
30
37
|
|
31
38
|
$client = WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
|
32
|
-
|
33
39
|
RSpec.configure do |config|
|
34
40
|
# The settings below are suggested to provide a good initial experience
|
35
41
|
# with RSpec, but feel free to customize to your heart's content.
|
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.5.
|
4
|
+
version: 1.5.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-
|
11
|
+
date: 2014-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|