weixin_authorize 1.5.0 → 1.5.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: 194d7f3905340f75447ec9401d845af02eedf8a8
4
- data.tar.gz: 448c9b7656d7100801514abf18f5213456f28b01
3
+ metadata.gz: 6bd5cb5899a192d80fe3b410c4a15540146603bb
4
+ data.tar.gz: 1a59ba515e57a12e9181c550aed3bbfbb838ee50
5
5
  SHA512:
6
- metadata.gz: aa2e813b5c406da0bf28e9987db44318ab369fc41401b3e12158cdce540f04d8e827f5cf777123a40a3c6a6ab246749aa6143c55e859f1ba2dee8d4c51665737
7
- data.tar.gz: e7eac68aec0577c93f10dd5e1634d9a58d5bca61d667c6fcfbbe5dd50975e5d7e6aa5b15d3085ec42bcb1ba122da9d13327c701196d230f3a6b10c71b99d78ea
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
- # authenticate access_token
38
- def authenticate
39
- if is_weixin_redis_blank?
40
- http_get_access_token
41
- else
42
- authenticate_with_redis
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
- http_get_access_token
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 = 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"]
81
+ hash_infos = http_get_without_token("/token", authenticate_options)
82
+ hash_infos
67
83
  end
68
84
 
69
85
  def authenticate_options
@@ -1,3 +1,3 @@
1
1
  module WeixinAuthorize
2
- VERSION = "1.5.0"
2
+ VERSION = "1.5.1"
3
3
  end
@@ -6,9 +6,9 @@ describe WeixinAuthorize::Client do
6
6
  expect($client.access_token).to eq(nil)
7
7
  end
8
8
 
9
- it "return access_token after authenticate" do
10
- $client.authenticate
11
- expect($client.access_token).not_to eq(nil)
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
- redis_with_ns = Redis::Namespace.new("weixin_#{Time.now.to_i}:weixin_authorize", :redis => redis)
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.0
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-25 00:00:00.000000000 Z
11
+ date: 2014-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client