xi_wechat_corp 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 +4 -4
- data/lib/xi_wechat_corp/api/connection.rb +25 -3
- data/lib/xi_wechat_corp/callback/grape_xml_parser.rb.rb +18 -0
- data/lib/xi_wechat_corp/version.rb +1 -1
- data/lib/xi_wechat_corp.rb +15 -1
- data/test/test_helper.rb +1 -0
- data/test/xi_wechat_corp/api/connection_test.rb +22 -0
- data/xi_wechat_corp.gemspec +1 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f43b1974370248c09d051e2e6892767be0991916
|
4
|
+
data.tar.gz: 3597d0baf0dee464ddae6ac9a6124694607cbff3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bae479c83066ae4b2950a44804aec6ef600731f4bd36e9dbdfe3242f9c89df8e611c094d4596463b293ba18941fb185f3c112130d3c4c80c2155646ae2d8df7
|
7
|
+
data.tar.gz: c0fc91b01d1f986e8589a8cd6cc9a8e696319d92bfee9c6f83c48274b6df0d8c0e3c160723b73a51a94ba83ed9a73058988b7e65193496794affc1efe99558fb
|
@@ -59,9 +59,10 @@ module XiWechatCorp
|
|
59
59
|
def get_access_token(corp_id = nil, secret = nil)
|
60
60
|
@corp_id = corp_id unless corp_id.nil?
|
61
61
|
@secret = secret unless secret.nil?
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
@access_token = maybe_cache do
|
63
|
+
resp = get('gettoken', corpid: @corp_id, corpsecret: @secret)
|
64
|
+
resp.body['access_token']
|
65
|
+
end
|
65
66
|
end
|
66
67
|
|
67
68
|
alias_method :auth, :get_access_token
|
@@ -72,6 +73,27 @@ module XiWechatCorp
|
|
72
73
|
end
|
73
74
|
|
74
75
|
private
|
76
|
+
def maybe_cache
|
77
|
+
if XiWechatCorp.cache.nil?
|
78
|
+
AccessToken.new(yield)
|
79
|
+
else
|
80
|
+
expired_at = 0
|
81
|
+
key = ['xi_wechat_corp', SHA1Signer.new(@secret).sign(@corp_id)].join('/')
|
82
|
+
cached = XiWechatCorp.cache.read(key)
|
83
|
+
if cached && cached =~ /\A\d+:/
|
84
|
+
expired_at, token = cached.split(':', 2)
|
85
|
+
expired_at = expired_at.to_i
|
86
|
+
end
|
87
|
+
if token && expired_at != 0 && expired_at > Time.now.to_i
|
88
|
+
AccessToken.new(token, expired_at)
|
89
|
+
else
|
90
|
+
ret = AccessToken.new(yield)
|
91
|
+
XiWechatCorp.cache.write(key, [ret.expired_at, ret.token].join(':'))
|
92
|
+
ret
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
75
97
|
def can_get_access_token?
|
76
98
|
!@corp_id.nil? && !@secret.nil?
|
77
99
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module XiWechatCorp
|
2
|
+
module Callback
|
3
|
+
# Ensure that XiWechatCorp::Callback::Rack middleware has bee added.
|
4
|
+
#
|
5
|
+
# parser :xml, XiWechatCorp::Callback::GrapeXmlParser
|
6
|
+
module GrapeXmlParser
|
7
|
+
class << self
|
8
|
+
def call(object, env)
|
9
|
+
if env['xi_wechat_corp.params']
|
10
|
+
env['xi_wechat_corp.params']
|
11
|
+
else
|
12
|
+
Grape::Parser::Xml.call(object, env)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/xi_wechat_corp.rb
CHANGED
@@ -7,6 +7,20 @@ module XiWechatCorp
|
|
7
7
|
autoload :AesCrypt, 'xi_wechat_corp/aes_crypt'
|
8
8
|
|
9
9
|
class << self
|
10
|
-
|
10
|
+
# logger must support method `error(string)`
|
11
|
+
attr_reader :logger
|
12
|
+
# value = cache.read(key)
|
13
|
+
# cache.write(key, value)
|
14
|
+
attr_reader :cache
|
15
|
+
|
16
|
+
def logger=(logger)
|
17
|
+
raise ArgumentError if logger && !logger.respond_to?(:error)
|
18
|
+
@logger = logger
|
19
|
+
end
|
20
|
+
|
21
|
+
def cache=(cache)
|
22
|
+
raise ArgumentError if cache && (!cache.respond_to?(:read) || !cache.respond_to?(:write))
|
23
|
+
@cache = cache
|
24
|
+
end
|
11
25
|
end
|
12
26
|
end
|
data/test/test_helper.rb
CHANGED
@@ -4,8 +4,20 @@ require 'xi_wechat_corp/api/access_token'
|
|
4
4
|
module XiWechatCorp
|
5
5
|
module API
|
6
6
|
class ConnectionTest < Minitest::Test
|
7
|
+
class Cache < Hash
|
8
|
+
alias_method :read, :[]
|
9
|
+
alias_method :write, :[]=
|
10
|
+
end
|
11
|
+
|
7
12
|
def setup
|
13
|
+
WebMock.reset!
|
8
14
|
stub_request(:any, %r{https://qyapi.weixin.qq.com/cgi-bin/.*}).to_return(body: '{"access_token":"at"}')
|
15
|
+
@remember_cache = XiWechatCorp.cache
|
16
|
+
XiWechatCorp.cache = Cache.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def teardown
|
20
|
+
XiWechatCorp.cache = @remember_cache
|
9
21
|
end
|
10
22
|
|
11
23
|
def conn
|
@@ -31,6 +43,16 @@ module XiWechatCorp
|
|
31
43
|
assert_requested :get, 'https://qyapi.weixin.qq.com/cgi-bin/test',
|
32
44
|
query: { 'access_token' => 'at' }
|
33
45
|
end
|
46
|
+
|
47
|
+
test 'access token cache' do
|
48
|
+
conn = Connection.new corp_id: 'corptest', secret: 'secret'
|
49
|
+
conn.get_access_token
|
50
|
+
|
51
|
+
conn = Connection.new corp_id: 'corptest', secret: 'secret'
|
52
|
+
conn.get_access_token
|
53
|
+
|
54
|
+
assert_requested :get, 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=corptest&corpsecret=secret', times: 1
|
55
|
+
end
|
34
56
|
end
|
35
57
|
end
|
36
58
|
end
|
data/xi_wechat_corp.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xi_wechat_corp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian Yang
|
@@ -150,6 +150,20 @@ dependencies:
|
|
150
150
|
- - ">="
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: pry
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
153
167
|
description: Wechat Corp development toolkits
|
154
168
|
email:
|
155
169
|
- ian@3pjgames.com
|
@@ -169,6 +183,7 @@ files:
|
|
169
183
|
- lib/xi_wechat_corp/api/connection.rb
|
170
184
|
- lib/xi_wechat_corp/callback.rb
|
171
185
|
- lib/xi_wechat_corp/callback/config.rb
|
186
|
+
- lib/xi_wechat_corp/callback/grape_xml_parser.rb.rb
|
172
187
|
- lib/xi_wechat_corp/callback/rack.rb
|
173
188
|
- lib/xi_wechat_corp/callback/request.rb
|
174
189
|
- lib/xi_wechat_corp/callback/response.rb
|