wechat 0.7.3 → 0.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README-CN.md +7 -0
- data/README.md +8 -0
- data/lib/generators/wechat/redis_store_generator.rb +16 -0
- data/lib/generators/wechat/templates/config/initializers/wechat_redis_store.rb +30 -0
- data/lib/wechat/responder.rb +9 -3
- data/lib/wechat/ticket/corp_jsapi_ticket.rb +2 -2
- data/lib/wechat/ticket/jsapi_base.rb +17 -8
- data/lib/wechat/ticket/public_jsapi_ticket.rb +2 -2
- data/lib/wechat/token/access_token_base.rb +17 -8
- data/lib/wechat/token/corp_access_token.rb +2 -2
- data/lib/wechat/token/public_access_token.rb +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fb8761bd99cd1efecca27649bf0b95a0bb24bc1
|
4
|
+
data.tar.gz: 036cf5d32c2a355dbf81f0eb505b646e3397ec51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf28762a242a5c33f8fcabe556464a6d50cd567d3a90fc5ff9475fac7d91e957226f93c9b759a088b20e862cd03854648f617d430df07a5b49626b6eec3f0967
|
7
|
+
data.tar.gz: 526982f68c795107123939e94dfb05201274666ddf5e2b574161b9e5034155c84eaaa0fad85df0013f8fbd12998fdde9c760f099cfe432cea08e660a137d4da4
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v0.7.4 (released at 1/23/2016)
|
4
|
+
|
5
|
+
* Add Redis store token/ticket support, close #76, #60
|
6
|
+
* Rails 5 support without deprecate warning or other not necessory call. #82
|
7
|
+
|
3
8
|
## v0.7.3 (released at 1/19/2016)
|
4
9
|
|
5
10
|
* Allow transfer_customer_service to specific account.
|
data/README-CN.md
CHANGED
@@ -55,6 +55,13 @@ rake db:migrate
|
|
55
55
|
|
56
56
|
运行后会自动启用回调消息会话(session)记录,wechat gem会在Rails项目中生成两个文件,用户可以在*wechat_session*表中添加更多字段或者声明一些关联关系。使用已有的**hash_store**直接保存也是可以的,但对于PostgreSQL用户,使用[hstore](http://guides.rubyonrails.org/active_record_postgresql.html#hstore)或者json格式可能更佳,当然,最佳方案仍然是添加新字段记录数据。
|
57
57
|
|
58
|
+
启用Redis存贮token和ticket:
|
59
|
+
|
60
|
+
```console
|
61
|
+
rails g wechat:redis_store
|
62
|
+
```
|
63
|
+
|
64
|
+
Redis存贮相比默认的文件存贮,可以允许Rails应用运行在多台服务器中,如果只有一台服务器,仍然推荐使用默认的文件存贮,另外命令行不会读取Redis存贮的Token或者Ticket。
|
58
65
|
|
59
66
|
## 配置
|
60
67
|
|
data/README.md
CHANGED
@@ -61,6 +61,14 @@ rake db:migrate
|
|
61
61
|
|
62
62
|
Enable session record will generate two files in Rails folder, User can add more column fields in *wechat_session* table and add declare to link to users table, it's also possible to store data directly in **hash_store**. if you are using PostgreSQL, using [hstore](http://guides.rubyonrails.org/active_record_postgresql.html#hstore)/json maybe better, but the best way is still add dedicate column to record the data, the Rails way.
|
63
63
|
|
64
|
+
Using Redis to store wechat token and ticket:
|
65
|
+
|
66
|
+
```console
|
67
|
+
rails g wechat:redis_store
|
68
|
+
```
|
69
|
+
|
70
|
+
Redis store support Rails application running in multi-server, no need to enable it if your Rails application running in one server only, the wechat command won't read the token/ticket store in Redis.
|
71
|
+
|
64
72
|
## Configuration
|
65
73
|
|
66
74
|
#### Configure for command line
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Wechat
|
2
|
+
module Generators
|
3
|
+
class RedisStoreGenerator < Rails::Generators::Base
|
4
|
+
desc 'Using redis as token/ticket store'
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
|
7
|
+
def copy_wechat_redis_initializer
|
8
|
+
template 'config/initializers/wechat_redis_store.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_redis_gem
|
12
|
+
gem 'redis'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Wechat
|
2
|
+
def self.redis
|
3
|
+
# You can reuse existing redis connection and remove this method if require
|
4
|
+
@redis ||= Redis.new # more options see https://github.com/redis/redis-rb#getting-started
|
5
|
+
end
|
6
|
+
|
7
|
+
module Token
|
8
|
+
class AccessTokenBase
|
9
|
+
def read_token
|
10
|
+
JSON.parse(Wechat.redis.get('my_app_wechat_token'))
|
11
|
+
end
|
12
|
+
|
13
|
+
def write_token(token_hash)
|
14
|
+
Wechat.redis.set 'my_app_wechat_token', token_hash.to_json
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Ticket
|
20
|
+
class JsapiBase
|
21
|
+
def read_ticket
|
22
|
+
JSON.parse(Wechat.redis.get('my_app_wechat_ticket'))
|
23
|
+
end
|
24
|
+
|
25
|
+
def write_ticket(ticket_hash)
|
26
|
+
Wechat.redis.set 'my_app_wechat_ticket', ticket_hash.to_json
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/wechat/responder.rb
CHANGED
@@ -10,7 +10,7 @@ module Wechat
|
|
10
10
|
# Rails 5 remove before_filter and skip_before_filter
|
11
11
|
if respond_to?(:skip_before_action)
|
12
12
|
# Rails 5 API mode won't define verify_authenticity_token
|
13
|
-
skip_before_action :verify_authenticity_token
|
13
|
+
skip_before_action :verify_authenticity_token if respond_to?(:verify_authenticity_token)
|
14
14
|
before_action :verify_signature, only: [:show, :create]
|
15
15
|
else
|
16
16
|
skip_before_filter :verify_authenticity_token
|
@@ -209,8 +209,14 @@ module Wechat
|
|
209
209
|
data = Hash.from_xml(content)
|
210
210
|
end
|
211
211
|
|
212
|
-
|
213
|
-
|
212
|
+
if Rails::VERSION::MAJOR >= 5
|
213
|
+
HashWithIndifferentAccess.new(data.fetch('xml', {})).tap do |msg|
|
214
|
+
msg[:Event].downcase! if msg[:Event]
|
215
|
+
end
|
216
|
+
else
|
217
|
+
HashWithIndifferentAccess.new_from_hash_copying_default(data.fetch('xml', {})).tap do |msg|
|
218
|
+
msg[:Event].downcase! if msg[:Event]
|
219
|
+
end
|
214
220
|
end
|
215
221
|
end
|
216
222
|
|
@@ -5,8 +5,8 @@ module Wechat
|
|
5
5
|
class CorpJsapiTicket < JsapiBase
|
6
6
|
def refresh
|
7
7
|
data = client.get('get_jsapi_ticket', params: { access_token: access_token.token })
|
8
|
-
|
9
|
-
|
8
|
+
write_ticket_to_store(data)
|
9
|
+
read_ticket_from_store
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -14,7 +14,7 @@ module Wechat
|
|
14
14
|
|
15
15
|
def ticket
|
16
16
|
# Possible two worker running, one worker refresh ticket, other unaware, so must read every time
|
17
|
-
|
17
|
+
read_ticket_from_store
|
18
18
|
refresh if remain_life_seconds < @random_generator.rand(30..3 * 60)
|
19
19
|
access_ticket
|
20
20
|
end
|
@@ -43,17 +43,26 @@ module Wechat
|
|
43
43
|
|
44
44
|
protected
|
45
45
|
|
46
|
-
def
|
47
|
-
td =
|
46
|
+
def read_ticket_from_store
|
47
|
+
td = read_ticket
|
48
|
+
@ticket_life_in_seconds = td.fetch('ticket_expires_in').to_i
|
48
49
|
@got_ticket_at = td.fetch('got_ticket_at').to_i
|
49
|
-
@
|
50
|
-
|
51
|
-
rescue JSON::ParserError, Errno::ENOENT, KeyError
|
50
|
+
@access_ticket = td.fetch('ticket') # return access_ticket same time
|
51
|
+
rescue JSON::ParserError, Errno::ENOENT, KeyError, TypeError
|
52
52
|
refresh
|
53
53
|
end
|
54
54
|
|
55
|
-
def
|
56
|
-
ticket_hash
|
55
|
+
def write_ticket_to_store(ticket_hash)
|
56
|
+
ticket_hash['got_ticket_at'.freeze] = Time.now.to_i
|
57
|
+
ticket_hash['ticket_expires_in'.freeze] = ticket_hash.delete('expires_in')
|
58
|
+
write_ticket(ticket_hash)
|
59
|
+
end
|
60
|
+
|
61
|
+
def read_ticket
|
62
|
+
JSON.parse(File.read(jsapi_ticket_file))
|
63
|
+
end
|
64
|
+
|
65
|
+
def write_ticket(ticket_hash)
|
57
66
|
File.write(jsapi_ticket_file, ticket_hash.to_json)
|
58
67
|
end
|
59
68
|
|
@@ -5,8 +5,8 @@ module Wechat
|
|
5
5
|
class PublicJsapiTicket < JsapiBase
|
6
6
|
def refresh
|
7
7
|
data = client.get('ticket/getticket', params: { access_token: access_token.token, type: 'jsapi' })
|
8
|
-
|
9
|
-
|
8
|
+
write_ticket_to_store(data)
|
9
|
+
read_ticket_from_store
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -13,24 +13,33 @@ module Wechat
|
|
13
13
|
|
14
14
|
def token
|
15
15
|
# Possible two worker running, one worker refresh token, other unaware, so must read every time
|
16
|
-
|
16
|
+
read_token_from_store
|
17
17
|
refresh if remain_life_seconds < @random_generator.rand(30..3 * 60)
|
18
18
|
access_token
|
19
19
|
end
|
20
20
|
|
21
21
|
protected
|
22
22
|
|
23
|
-
def
|
24
|
-
td =
|
23
|
+
def read_token_from_store
|
24
|
+
td = read_token
|
25
|
+
@token_life_in_seconds = td.fetch('token_expires_in').to_i
|
25
26
|
@got_token_at = td.fetch('got_token_at').to_i
|
26
|
-
@
|
27
|
-
|
28
|
-
rescue JSON::ParserError, Errno::ENOENT, KeyError
|
27
|
+
@access_token = td.fetch('access_token') # return access_token same time
|
28
|
+
rescue JSON::ParserError, Errno::ENOENT, KeyError, TypeError
|
29
29
|
refresh
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
33
|
-
token_hash
|
32
|
+
def write_token_to_store(token_hash)
|
33
|
+
token_hash['got_token_at'.freeze] = Time.now.to_i
|
34
|
+
token_hash['token_expires_in'.freeze] = token_hash.delete('expires_in')
|
35
|
+
write_token(token_hash)
|
36
|
+
end
|
37
|
+
|
38
|
+
def read_token
|
39
|
+
JSON.parse(File.read(token_file))
|
40
|
+
end
|
41
|
+
|
42
|
+
def write_token(token_hash)
|
34
43
|
File.write(token_file, token_hash.to_json)
|
35
44
|
end
|
36
45
|
|
@@ -5,8 +5,8 @@ module Wechat
|
|
5
5
|
class CorpAccessToken < AccessTokenBase
|
6
6
|
def refresh
|
7
7
|
data = client.get('gettoken', params: { corpid: appid, corpsecret: secret })
|
8
|
-
|
9
|
-
|
8
|
+
write_token_to_store(data)
|
9
|
+
read_token_from_store
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -5,8 +5,8 @@ module Wechat
|
|
5
5
|
class PublicAccessToken < AccessTokenBase
|
6
6
|
def refresh
|
7
7
|
data = client.get('token', params: { grant_type: 'client_credential', appid: appid, secret: secret })
|
8
|
-
|
9
|
-
|
8
|
+
write_token_to_store(data)
|
9
|
+
read_token_from_store
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wechat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Skinnyworm
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-01-
|
12
|
+
date: 2016-01-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -136,9 +136,11 @@ files:
|
|
136
136
|
- bin/wechat
|
137
137
|
- lib/action_controller/wechat_responder.rb
|
138
138
|
- lib/generators/wechat/install_generator.rb
|
139
|
+
- lib/generators/wechat/redis_store_generator.rb
|
139
140
|
- lib/generators/wechat/session_generator.rb
|
140
141
|
- lib/generators/wechat/templates/app/controllers/wechats_controller.rb
|
141
142
|
- lib/generators/wechat/templates/app/models/wechat_session.rb
|
143
|
+
- lib/generators/wechat/templates/config/initializers/wechat_redis_store.rb
|
142
144
|
- lib/generators/wechat/templates/config/wechat.yml
|
143
145
|
- lib/generators/wechat/templates/db/migration.rb
|
144
146
|
- lib/wechat.rb
|