wechat 0.7.20 → 0.8.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: a1fcfa80de68c3711415ea5b2a09e2f00a2e89e8
4
- data.tar.gz: fdfe9d0212a7536c011d8e3f04579dfca8547052
3
+ metadata.gz: e557486566741fde96f95013826e225fb60a8655
4
+ data.tar.gz: bbcfb11df1063caf51f64ca68ab23c4ce41b0360
5
5
  SHA512:
6
- metadata.gz: 98d4bc51b5e7c838a0979e4b51819cfe0fce80f96b6090ea45ddd87e4df844bbf2f8c7b3572918dbc5a275cfbef399f5184741fffed4c0184abc23e672eb8bd1
7
- data.tar.gz: c004f7e6e4912667f34a356e0f24cc8f0f0e002080e062996d166bdb706c532cb37a7e8f1c7d69a6708a3fe7d78f117817b62ef25332917dcba7798ce95a9142
6
+ metadata.gz: d6b1b39c6d6e7cd385842041b8125534db5a3f8007e2e0e7e4e79700c6691f27edeb00e34bb79e3077b22a611f06ecb7f37212cb8e5fc0fac4d746d9efd5e984
7
+ data.tar.gz: e6149d522aeefda20229604a835604a6d106f8f3a90194a5c498cb411cdeb1619e07752e84752951f3679a0e0530e5182fd687d7e5c02fa10362124fbd402d8e
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.8.0 (released at 10/24/2016)
4
+
5
+ * Complete support multi-wechat public account. by @xiewenwei #150
6
+ * Support loading configure value from Figaro if application.yml exist.
7
+
3
8
  ## v0.7.20 (released at 8/29/2016)
4
9
 
5
10
  * Apply opt and config together when loading controller_wechat, to simplify wechat_responder params. by @bzhang443 #147
@@ -1,7 +1,7 @@
1
- WeChat [![Gem Version][version-badge]][rubygems] [![Build Status][travis-badge]][travis] [![Code Climate][codeclimate-badge]][codeclimate] [![Code Coverage][codecoverage-badge]][codecoverage]
1
+ WeChat [![Gem Version](https://badge.fury.io/rb/wechat.svg)](https://rubygems.org/gems/wechat) [![Build Status](https://travis-ci.org/Eric-Guo/wechat.svg)](https://travis-ci.org/Eric-Guo/wechat) [![Code Climate](https://codeclimate.com/github/Eric-Guo/wechat.png)](https://codeclimate.com/github/Eric-Guo/wechat) [![Code Coverage](https://codeclimate.com/github/Eric-Guo/wechat/coverage.png)](https://codeclimate.com/github/Eric-Guo/wechat/coverage)
2
2
  ======
3
3
 
4
- [![Join the chat][gitter-badge]][gitter] [![Issue Stats][issue-badge]][issuestats] [![PR Stats][pr-badge]][issuestats]
4
+ [![Join the chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Eric-Guo/wechat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5
5
 
6
6
  WeChat gem 可以帮助开发者方便地在Rails环境中集成微信[公众平台](https://mp.weixin.qq.com/)和[企业平台](https://qy.weixin.qq.com)提供的服务,包括:
7
7
 
@@ -162,7 +162,28 @@ development:
162
162
 
163
163
  test:
164
164
  <<: *default
165
- ```
165
+
166
+ # Multiple Accounts
167
+ #
168
+ # wx2_development:
169
+ # <<: *default
170
+ # appid: "my_appid"
171
+ # secret: "my_secret"
172
+ # access_token: "tmp/wechat_access_token2"
173
+ # jsapi_ticket: "tmp/wechat_jsapi_ticket2"
174
+ #
175
+ # wx2_test:
176
+ # <<: *default
177
+ # appid: "my_appid"
178
+ # secret: "my_secret"
179
+ #
180
+ # wx2_production:
181
+ # <<: *default
182
+ # appid: "my_appid"
183
+ # secret: "my_secret"
184
+ ```
185
+
186
+ 进一步的多账号支持参见[PR 150](https://github.com/Eric-Guo/wechat/pull/150)。
166
187
 
167
188
  ##### 配置优先级
168
189
 
@@ -180,6 +201,16 @@ Wechat服务器有报道曾出现[RestClient::SSLCertificateNotVerified](http://
180
201
 
181
202
  有些情况下,单个Rails应用可能需要处理来自多个微信公众号的消息,您可以通过在`wechat_responder`和`wechat_api`后配置多个相关参数来支持多账号。
182
203
 
204
+ ```ruby
205
+ class WechatFirstController < ActionController::Base
206
+ wechat_responder account: :new_account
207
+
208
+ on :text, with:"help", respond: "help content"
209
+ end
210
+ ```
211
+
212
+ 或者直接完整配置
213
+
183
214
  ```ruby
184
215
  class WechatFirstController < ActionController::Base
185
216
  wechat_responder appid: "app1", secret: "secret1", token: "token1", access_token: Rails.root.join("tmp/access_token1")
@@ -472,19 +503,19 @@ template:
472
503
  data:
473
504
  first:
474
505
  value: "您好,您已报名成功"
475
- color: "#0A0A0A"
506
+ color: "#0A0A0A"
476
507
  keynote1:
477
508
  value: "XX活动"
478
- color: "#CCCCCC"
509
+ color: "#CCCCCC"
479
510
  keynote2:
480
511
  value: "2014年9月16日"
481
- color: "#CCCCCC"
512
+ color: "#CCCCCC"
482
513
  keynote3:
483
514
  value: "上海徐家汇xxx城"
484
- color: "#CCCCCC"
515
+ color: "#CCCCCC"
485
516
  remark:
486
517
  value: "欢迎再次使用。"
487
- color: "#173177"
518
+ color: "#173177"
488
519
 
489
520
  ```
490
521
 
@@ -619,7 +650,7 @@ class WechatsController < ActionController::Base
619
650
  # 处理地理位置消息
620
651
  on :label_location do |request|
621
652
  request.reply.text("Label: #{request[:Label]} Location_X: #{request[:Location_X]} Location_Y: #{request[:Location_Y]} Scale: #{request[:Scale]}")
622
- end
653
+ end
623
654
 
624
655
  # 处理上报地理位置事件
625
656
  on :location do |request|
@@ -720,17 +751,3 @@ end
720
751
  * 企业号接受菜单消息时,Wechat腾讯服务器无法解析部分域名,请使用IP绑定回调URL,用户的普通消息目前不受影响。
721
752
  * 企业号全量覆盖成员使用的csv通讯录格式,直接将下载的模板导入[是不工作的](http://qydev.weixin.qq.com/qa/index.php?qa=13978),必须使用Excel打开,然后另存为csv格式才会变成合法格式。
722
753
  * 如果使用nginx+unicron部署方案,并且使用了https,必须设置`trusted_domain_fullname`为https,否则会导致JS-SDK签名失效。
723
-
724
- [version-badge]: https://badge.fury.io/rb/wechat.svg
725
- [rubygems]: https://rubygems.org/gems/wechat
726
- [travis-badge]: https://travis-ci.org/Eric-Guo/wechat.svg
727
- [travis]: https://travis-ci.org/Eric-Guo/wechat
728
- [codeclimate-badge]: https://codeclimate.com/github/Eric-Guo/wechat.png
729
- [codeclimate]: https://codeclimate.com/github/Eric-Guo/wechat
730
- [codecoverage-badge]: https://codeclimate.com/github/Eric-Guo/wechat/coverage.png
731
- [codecoverage]: https://codeclimate.com/github/Eric-Guo/wechat/coverage
732
- [gitter-badge]: https://badges.gitter.im/Join%20Chat.svg
733
- [gitter]: https://gitter.im/Eric-Guo/wechat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
734
- [issue-badge]: http://issuestats.com/github/Eric-Guo/wechat/badge/issue
735
- [pr-badge]: http://issuestats.com/github/Eric-Guo/wechat/badge/pr
736
- [issuestats]: http://issuestats.com/github/Eric-Guo/wechat
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
- WeChat [![Gem Version][version-badge]][rubygems] [![Build Status][travis-badge]][travis] [![Code Climate][codeclimate-badge]][codeclimate] [![Code Coverage][codecoverage-badge]][codecoverage]
1
+ WeChat [![Gem Version](https://badge.fury.io/rb/wechat.svg)](https://rubygems.org/gems/wechat) [![Build Status](https://travis-ci.org/Eric-Guo/wechat.svg)](https://travis-ci.org/Eric-Guo/wechat) [![Code Climate](https://codeclimate.com/github/Eric-Guo/wechat.png)](https://codeclimate.com/github/Eric-Guo/wechat) [![Code Coverage](https://codeclimate.com/github/Eric-Guo/wechat/coverage.png)](https://codeclimate.com/github/Eric-Guo/wechat/coverage)
2
2
  ======
3
3
 
4
- [![Join the chat][gitter-badge]][gitter] [![Issue Stats][issue-badge]][issuestats] [![PR Stats][pr-badge]][issuestats]
4
+ [![Join the chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Eric-Guo/wechat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5
5
 
6
6
  [中文文档 Chinese document](/README-CN.md)
7
7
 
@@ -175,7 +175,28 @@ development:
175
175
 
176
176
  test:
177
177
  <<: *default
178
- ```
178
+
179
+ # Multiple Accounts
180
+ #
181
+ # wx2_development:
182
+ # <<: *default
183
+ # appid: "my_appid"
184
+ # secret: "my_secret"
185
+ # access_token: "tmp/wechat_access_token2"
186
+ # jsapi_ticket: "tmp/wechat_jsapi_ticket2"
187
+ #
188
+ # wx2_test:
189
+ # <<: *default
190
+ # appid: "my_appid"
191
+ # secret: "my_secret"
192
+ #
193
+ # wx2_production:
194
+ # <<: *default
195
+ # appid: "my_appid"
196
+ # secret: "my_secret"
197
+ ```
198
+
199
+ Detail multiple accounts can reference [PR 150](https://github.com/Eric-Guo/wechat/pull/150)
179
200
 
180
201
  ##### Configure priority
181
202
 
@@ -187,12 +208,21 @@ Stability various even for Tencent wechat server, so setting a long timeout may
187
208
 
188
209
  ##### Skip the SSL verification
189
210
 
190
- SSL Certification can also be corrupted by some reason in China, [it's reported](http://qydev.weixin.qq.com/qa/index.php?qa=11037) and if it's happen to you, can setting `skip_verify_ssl: true`. (not recommend)
211
+ SSL Certification can also be corrupted by some reason in China, [it's reported](http://qydev.weixin.qq.com/qa/index.php?qa=11037) and if it's happen to you, can setting `skip_verify_ssl: true`. (not recommend)
191
212
 
192
213
  #### Configure individual responder with different appid
193
214
 
194
215
  Sometime, you may want to hosting more than one enterprise/public wechat account in one Rails application, so you can given those configuration info when calling `wechat_responder` or `wechat_api`
195
216
 
217
+ ```ruby
218
+ class WechatFirstController < ActionController::Base
219
+ wechat_responder account: :new_account
220
+
221
+ on :text, with:"help", respond: "help content"
222
+ end
223
+
224
+ Or you can config full options.
225
+
196
226
  ```ruby
197
227
  class WechatFirstController < ActionController::Base
198
228
  wechat_responder appid: "app1", secret: "secret1", token: "token1", access_token: Rails.root.join("tmp/access_token1")
@@ -487,19 +517,19 @@ template:
487
517
  data:
488
518
  first:
489
519
  value: "Hello, you successfully registed"
490
- color: "#0A0A0A"
520
+ color: "#0A0A0A"
491
521
  keynote1:
492
522
  value: "5km Health Running"
493
- color: "#CCCCCC"
523
+ color: "#CCCCCC"
494
524
  keynote2:
495
525
  value: "2014-09-16"
496
- color: "#CCCCCC"
526
+ color: "#CCCCCC"
497
527
  keynote3:
498
528
  value: "Centry Park, Pudong, Shanghai"
499
- color: "#CCCCCC"
529
+ color: "#CCCCCC"
500
530
  remark:
501
531
  value: "Welcome back"
502
- color: "#173177"
532
+ color: "#173177"
503
533
 
504
534
  ```
505
535
 
@@ -733,17 +763,3 @@ end
733
763
  * Sometime, enterprise account can not receive the menu message due to Tencent server can not resolved the DNS, so using IP as a callback URL more stable, but it's never happen for user sent text message.
734
764
  * Enterprise batch replace users using a CSV format file, but if you using the download template directly, it's [not working](http://qydev.weixin.qq.com/qa/index.php?qa=13978), must open the CSV file in excel first, then save as CSV format again, seems Tencent only support Excel save as CSV file format.
735
765
  * If you using unicorn behind nginx and https, you need setting `trusted_domain_fullname` and point to https, other will got http and lead invalid signature in the JS-SDK.
736
-
737
- [version-badge]: https://badge.fury.io/rb/wechat.svg
738
- [rubygems]: https://rubygems.org/gems/wechat
739
- [travis-badge]: https://travis-ci.org/Eric-Guo/wechat.svg
740
- [travis]: https://travis-ci.org/Eric-Guo/wechat
741
- [codeclimate-badge]: https://codeclimate.com/github/Eric-Guo/wechat.png
742
- [codeclimate]: https://codeclimate.com/github/Eric-Guo/wechat
743
- [codecoverage-badge]: https://codeclimate.com/github/Eric-Guo/wechat/coverage.png
744
- [codecoverage]: https://codeclimate.com/github/Eric-Guo/wechat/coverage
745
- [gitter-badge]: https://badges.gitter.im/Join%20Chat.svg
746
- [gitter]: https://gitter.im/Eric-Guo/wechat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
747
- [issue-badge]: http://issuestats.com/github/Eric-Guo/wechat/badge/issue
748
- [pr-badge]: http://issuestats.com/github/Eric-Guo/wechat/badge/pr
749
- [issuestats]: http://issuestats.com/github/Eric-Guo/wechat
data/bin/wechat CHANGED
@@ -17,7 +17,7 @@ require 'cgi'
17
17
 
18
18
  class App < Thor
19
19
  package_name 'Wechat'
20
- option :token_file, aliases: '-t', desc: 'File to store access token'
20
+ class_option :account, aliases: '-a', default: :default, desc: 'Name of Wechat account configuration.'
21
21
 
22
22
  attr_reader :wechat_api_client
23
23
  no_commands do
@@ -26,6 +26,7 @@ class App < Thor
26
26
  end
27
27
  end
28
28
 
29
+ option :token_file, aliases: '-t', desc: 'File to store access token'
29
30
  desc 'callbackip', '获取微信服务器IP地址'
30
31
  def callbackip
31
32
  puts wechat_api.callbackip
@@ -17,28 +17,31 @@ module ActionController
17
17
  private
18
18
 
19
19
  def load_controller_wechat(opts = {})
20
- self.token = opts[:token] || Wechat.config.token
21
- self.appid = opts[:appid] || Wechat.config.appid
22
- self.corpid = opts[:corpid] || Wechat.config.corpid
23
- self.agentid = opts[:agentid] || Wechat.config.agentid
24
- self.encrypt_mode = opts[:encrypt_mode] || Wechat.config.encrypt_mode || corpid.present?
20
+ account = opts[:account].present? ? opts[:account].to_sym : :default
21
+
22
+ self.token = opts[:token] || Wechat.config(account).token
23
+ self.appid = opts[:appid] || Wechat.config(account).appid
24
+ self.corpid = opts[:corpid] || Wechat.config(account).corpid
25
+ self.agentid = opts[:agentid] || Wechat.config(account).agentid
26
+ self.encrypt_mode = opts[:encrypt_mode] || Wechat.config(account).encrypt_mode || corpid.present?
25
27
  self.timeout = opts[:timeout] || 20
26
28
  self.skip_verify_ssl = opts[:skip_verify_ssl]
27
- self.encoding_aes_key = opts[:encoding_aes_key] || Wechat.config.encoding_aes_key
28
- self.trusted_domain_fullname = opts[:trusted_domain_fullname] || Wechat.config.trusted_domain_fullname
29
- Wechat.config.oauth2_cookie_duration ||= 1.hour
30
- self.oauth2_cookie_duration = opts[:oauth2_cookie_duration] || Wechat.config.oauth2_cookie_duration.to_i.seconds
29
+ self.encoding_aes_key = opts[:encoding_aes_key] || Wechat.config(account).encoding_aes_key
30
+ self.trusted_domain_fullname = opts[:trusted_domain_fullname] || Wechat.config(account).trusted_domain_fullname
31
+ Wechat.config(account).oauth2_cookie_duration ||= 1.hour
32
+ self.oauth2_cookie_duration = opts[:oauth2_cookie_duration] || Wechat.config(account).oauth2_cookie_duration.to_i.seconds
33
+
34
+ access_token = opts[:access_token] || Wechat.config(account).access_token
35
+ jsapi_ticket = opts[:jsapi_ticket] || Wechat.config(account).jsapi_ticket
31
36
 
32
- access_token = opts[:access_token] || Wechat.config.access_token
33
- jsapi_ticket = opts[:jsapi_ticket] || Wechat.config.jsapi_ticket
37
+ return self.wechat_api_client = Wechat.api if account == :default && opts.empty?
34
38
 
35
- return self.wechat_api_client = Wechat.api if opts.empty?
36
39
  if corpid.present?
37
- corpsecret = opts[:corpsecret] || Wechat.config.corpsecret
40
+ corpsecret = opts[:corpsecret] || Wechat.config(account).corpsecret
38
41
  Wechat::CorpApi.new(corpid, corpsecret, access_token, \
39
42
  agentid, timeout, skip_verify_ssl, jsapi_ticket)
40
43
  else
41
- secret = opts[:secret] || Wechat.config.secret
44
+ secret = opts[:secret] || Wechat.config(account).secret
42
45
  Wechat::Api.new(appid, secret, access_token, \
43
46
  timeout, skip_verify_ssl, jsapi_ticket)
44
47
  end
@@ -7,11 +7,17 @@ module Wechat
7
7
  module Token
8
8
  class AccessTokenBase
9
9
  def read_token
10
- JSON.parse(Wechat.redis.get('my_app_wechat_token'))
10
+ JSON.parse(Wechat.redis.get(redis_key)) || {}
11
11
  end
12
12
 
13
13
  def write_token(token_hash)
14
- Wechat.redis.set 'my_app_wechat_token', token_hash.to_json
14
+ Wechat.redis.set redis_key, token_hash.to_json
15
+ end
16
+
17
+ private
18
+
19
+ def redis_key
20
+ "my_app_wechat_token_#{self.appid}"
15
21
  end
16
22
  end
17
23
  end
@@ -19,11 +25,17 @@ module Wechat
19
25
  module Ticket
20
26
  class JsapiBase
21
27
  def read_ticket
22
- JSON.parse(Wechat.redis.get('my_app_wechat_ticket'))
28
+ JSON.parse(Wechat.redis.get(redis_key)) || {}
23
29
  end
24
30
 
25
31
  def write_ticket(ticket_hash)
26
- Wechat.redis.set 'my_app_wechat_ticket', ticket_hash.to_json
32
+ Wechat.redis.set redis_key, ticket_hash.to_json
33
+ end
34
+
35
+ private
36
+
37
+ def redis_key
38
+ "my_app_wechat_ticket_#{self.appid}"
27
39
  end
28
40
  end
29
41
  end
@@ -32,4 +32,41 @@ development:
32
32
  trusted_domain_fullname: "http://your_dev.proxy.qqbrowser.cc"
33
33
 
34
34
  test:
35
- <<: *default
35
+ <<: *default
36
+
37
+ # Multiple Accounts
38
+ #
39
+ # wx2_development:
40
+ # <<: *default
41
+ # appid: "my_appid"
42
+ # secret: "my_secret"
43
+ # access_token: "tmp/wechat_access_token2"
44
+ # jsapi_ticket: "tmp/wechat_jsapi_ticket2"
45
+ #
46
+ # wx2_test:
47
+ # <<: *default
48
+ # appid: "my_appid"
49
+ # secret: "my_secret"
50
+ #
51
+ # wx2_production:
52
+ # <<: *default
53
+ # appid: "my_appid"
54
+ # secret: "my_secret"
55
+ #
56
+ # wx3_development:
57
+ # <<: *default
58
+ # appid: "my_appid"
59
+ # secret: "my_secret"
60
+ # access_token: "tmp/wechat_access_token3"
61
+ # jsapi_ticket: "tmp/wechat_jsapi_ticket3"
62
+ #
63
+ # wx3_test:
64
+ # <<: *default
65
+ # appid: "my_appid"
66
+ # secret: "my_secret"
67
+ #
68
+ # wx3_production:
69
+ # <<: *default
70
+ # appid: "my_appid"
71
+ # secret: "my_secret"
72
+ #
@@ -19,12 +19,13 @@ module Wechat
19
19
  end
20
20
  end
21
21
 
22
- def self.config
23
- ApiLoader.config
22
+ def self.config(account = :default)
23
+ ApiLoader.config(account)
24
24
  end
25
25
 
26
- def self.api
27
- @wechat_api ||= ApiLoader.with({})
26
+ def self.api(account = :default)
27
+ @wechat_apis ||= {}
28
+ @wechat_apis[account.to_sym] ||= ApiLoader.with({account: account})
28
29
  end
29
30
  end
30
31
 
@@ -1,7 +1,8 @@
1
1
  module Wechat
2
2
  module ApiLoader
3
3
  def self.with(options)
4
- c = ApiLoader.config
4
+ account = options[:account] || :default
5
+ c = ApiLoader.config(account)
5
6
 
6
7
  token_file = options[:token_file] || c.access_token || '/var/tmp/wechat_access_token'
7
8
  js_token_file = options[:js_token_file] || c.jsapi_ticket || '/var/tmp/wechat_jsapi_ticket'
@@ -19,50 +20,94 @@ HELP
19
20
  end
20
21
  end
21
22
 
22
- @config = nil
23
+ @configs = nil
23
24
 
24
- def self.config
25
- return @config unless @config.nil?
26
- @config ||= loading_config!
27
- @config
25
+ def self.config(account = :default)
26
+ @configs ||= loading_config!
27
+ @configs[account.to_sym] || raise("Wechat configuration for #{account} is missing.")
28
28
  end
29
29
 
30
30
  private_class_method def self.loading_config!
31
- config ||= config_from_file || config_from_environment
31
+ configs = config_from_file || config_from_environment
32
+
33
+ configs.symbolize_keys!
34
+ configs.each do |key, cfg|
35
+ if cfg.is_a?(Hash)
36
+ cfg.symbolize_keys!
37
+ else
38
+ raise "wrong wechat configuration format for #{key}"
39
+ end
40
+ end
32
41
 
33
42
  if defined?(::Rails)
34
- config[:access_token] ||= Rails.root.join('tmp/access_token').to_s
35
- config[:jsapi_ticket] ||= Rails.root.join('tmp/jsapi_ticket').to_s
43
+ configs.each do |_, cfg|
44
+ cfg[:access_token] ||= Rails.root.try(:join, 'tmp/access_token').try(:to_path)
45
+ cfg[:jsapi_ticket] ||= Rails.root.try(:join, 'tmp/jsapi_ticket').try(:to_path)
46
+ end
47
+ end
48
+
49
+ configs.each do |_, cfg|
50
+ cfg[:timeout] ||= 20
51
+ cfg[:have_session_class] = class_exists?('WechatSession')
52
+ end
53
+
54
+ # create config object using raw config data
55
+ cfg_objs = {}
56
+ configs.each do |account, cfg|
57
+ cfg_objs[account] = OpenStruct.new(cfg)
36
58
  end
37
- config[:timeout] ||= 20
38
- config[:have_session_class] = class_exists?('WechatSession')
39
- config.symbolize_keys!
40
- @config = OpenStruct.new(config)
59
+ cfg_objs
41
60
  end
42
61
 
43
62
  private_class_method def self.config_from_file
44
63
  if defined?(::Rails)
45
- config_file = Rails.root.join('config/wechat.yml')
46
- return YAML.load(ERB.new(File.read(config_file)).result)[Rails.env] if File.exist?(config_file)
64
+ config_file = ENV['WECHAT_CONF_FILE'] || Rails.root.join('config/wechat.yml')
65
+ return resovle_config_file(config_file, Rails.env.to_s)
47
66
  else
48
- rails_config_file = File.join(Dir.getwd, 'config/wechat.yml')
67
+ rails_config_file = ENV['WECHAT_CONF_FILE'] || File.join(Dir.getwd, 'config/wechat.yml')
68
+ application_config_file = File.join(Dir.getwd, 'config/application.yml')
49
69
  home_config_file = File.join(Dir.home, '.wechat.yml')
50
70
  if File.exist?(rails_config_file)
51
- rails_env = ENV['RAILS_ENV'] || 'default'
52
- config = YAML.load(ERB.new(File.read(rails_config_file)).result)[rails_env]
53
- if config.present? && (config['appid'] || config['corpid'])
54
- puts "Using rails project config/wechat.yml #{rails_env} setting..."
71
+ if File.exist?(application_config_file) && !defined?(::Figaro)
72
+ require 'figaro'
73
+ Figaro::Application.new(path: application_config_file).load
74
+ end
75
+ rails_env = ENV['RAILS_ENV'] || 'development'
76
+ config = resovle_config_file(rails_config_file, rails_env)
77
+ if config.present? && (default = config[:default]) && (default['appid'] || default['corpid'])
78
+ puts "Using rails project #{ENV['WECHAT_CONF_FILE'] || "config/wechat.yml"} #{rails_env} setting..."
55
79
  return config
56
80
  end
57
81
  end
58
82
  if File.exist?(home_config_file)
59
- return YAML.load ERB.new(File.read(home_config_file)).result
83
+ return resovle_config_file(home_config_file, nil)
84
+ end
85
+ end
86
+ end
87
+
88
+ private_class_method def self.resovle_config_file(config_file, env)
89
+ if File.exist?(config_file)
90
+ raw_data = YAML.load(ERB.new(File.read(config_file)).result)
91
+ configs = {}
92
+ if env
93
+ # Process multiple accounts when env is given
94
+ raw_data.each do |key, value|
95
+ if key == env
96
+ configs[:default] = value
97
+ elsif m = /(.*?)_#{env}$/.match(key)
98
+ configs[m[1].to_sym] = value
99
+ end
100
+ end
101
+ else
102
+ # Treat is as one account when env is omitted
103
+ configs[:default] = raw_data
60
104
  end
105
+ configs
61
106
  end
62
107
  end
63
108
 
64
109
  private_class_method def self.config_from_environment
65
- { appid: ENV['WECHAT_APPID'],
110
+ value = { appid: ENV['WECHAT_APPID'],
66
111
  secret: ENV['WECHAT_SECRET'],
67
112
  corpid: ENV['WECHAT_CORPID'],
68
113
  corpsecret: ENV['WECHAT_CORPSECRET'],
@@ -75,6 +120,7 @@ HELP
75
120
  encoding_aes_key: ENV['WECHAT_ENCODING_AES_KEY'],
76
121
  jsapi_ticket: ENV['WECHAT_JSAPI_TICKET'],
77
122
  trusted_domain_fullname: ENV['WECHAT_TRUSTED_DOMAIN_FULLNAME'] }
123
+ {default: value}
78
124
  end
79
125
 
80
126
  private_class_method def self.class_exists?(class_name)
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.20
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skinnyworm
@@ -31,7 +31,7 @@ cert_chain:
31
31
  R5k6Ma92sW8jupX4cqbSu9rntdVQkNRpoHIrfU0MZT0cKsg/D1zMteylxrO3KMsz
32
32
  SPQRv+nrI1J0zevFqb8010heoR8SDyUA0Mm3+Q==
33
33
  -----END CERTIFICATE-----
34
- date: 2016-08-29 00:00:00.000000000 Z
34
+ date: 2016-10-24 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
@@ -206,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  version: '0'
207
207
  requirements: []
208
208
  rubyforge_project:
209
- rubygems_version: 2.6.4
209
+ rubygems_version: 2.6.7
210
210
  signing_key:
211
211
  specification_version: 4
212
212
  summary: DSL for wechat message handling and API
metadata.gz.sig CHANGED
Binary file