wechat 0.12.3 → 0.13.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -34,12 +34,16 @@ Use `gem install`
34
34
 
35
35
  ```
36
36
  gem install "wechat"
37
+ # If your ruby version < 2.6
38
+ # gem install wechat -v 0.12.2
37
39
  ```
38
40
 
39
41
  Or add it to your app's `Gemfile`:
40
42
 
41
43
  ```
42
44
  gem 'wechat'
45
+ # If your rails version < 6.0
46
+ # gem 'wechat', '~> 0.12.2'
43
47
  ```
44
48
 
45
49
  Run the following command to install it:
@@ -391,8 +395,9 @@ Wechat Public Account commands:
391
395
  wechat group_delete [GROUP_ID] # 删除分组
392
396
  wechat group_update [GROUP_ID, NEW_GROUP_NAME] # 修改分组名
393
397
  wechat groups # 查询所有分组
394
- wechat material_get [MEDIA_ID, PATH] # 永久媒体下载
398
+ wechat material_get [MEDIA_ID, PATH] # 永久媒体下载
395
399
  wechat material_add [MEDIA_TYPE, PATH] # 永久媒体上传
400
+ wechat material_add_news [MPNEWS_YAML_PATH] # 永久图文素材上传
396
401
  wechat material_count # 获取永久素材总数
397
402
  wechat material_delete [MEDIA_ID] # 删除永久素材
398
403
  wechat material_list [TYPE, OFFSET, COUNT] # 获取永久素材列表
data/bin/wechat CHANGED
@@ -6,13 +6,10 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
6
6
  require 'thor'
7
7
  require 'wechat'
8
8
  require 'json'
9
- require 'active_support' # To support Rails 4.2.1, see #17936
10
- require 'active_support/dependencies/autoload'
11
9
  require 'active_support/core_ext'
12
10
  require 'active_support/json'
13
11
  require 'fileutils'
14
12
  require 'yaml'
15
- require 'wechat/api_loader'
16
13
  require 'cgi'
17
14
 
18
15
  class App < Thor
@@ -411,6 +408,12 @@ class App < Thor
411
408
  puts wechat_api.material_add(type, path)
412
409
  end
413
410
 
411
+ desc 'material_add_news [MPNEWS_YAML_PATH]', '永久图文素材上传'
412
+ def material_add_news(mpnews_yaml_path)
413
+ new = YAML.load(File.read(mpnews_yaml_path))
414
+ puts wechat_api.material_add_news(Wechat::Message.new(MsgType: 'mpnews').mpnews(new['articles']))
415
+ end
416
+
414
417
  desc 'material_delete [MEDIA_ID]', '删除永久素材'
415
418
  def material_delete(media_id)
416
419
  puts wechat_api.material_delete(media_id)
@@ -64,15 +64,4 @@ module ActionController
64
64
  end
65
65
  end
66
66
  end
67
-
68
- if defined? Base
69
- class << Base
70
- include WechatResponder
71
- end
72
- end
73
- if defined? API
74
- class << API
75
- include WechatResponder
76
- end
77
- end
78
67
  end
@@ -31,7 +31,7 @@ module Wechat
31
31
  private
32
32
 
33
33
  def migration_version
34
- "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if Rails.version >= '5.0.0'
34
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
35
35
  end
36
36
  end
37
37
  end
@@ -31,7 +31,7 @@ module Wechat
31
31
  private
32
32
 
33
33
  def migration_version
34
- "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if Rails.version >= '5.0.0'
34
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
35
35
  end
36
36
  end
37
37
  end
data/lib/wechat.rb CHANGED
@@ -1,20 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'zeitwerk'
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.ignore("#{__dir__}/generators/**/*.rb")
6
+ loader.setup
7
+
3
8
  require 'base64'
4
9
  require 'openssl/cipher'
5
- require 'wechat/api_loader'
6
- require 'wechat/api'
7
- require 'wechat/mp_api'
8
- require 'wechat/corp_api'
9
- require 'wechat/helpers'
10
- require 'action_controller/wechat_responder'
11
10
 
12
11
  module Wechat
13
- autoload :Message, 'wechat/message'
14
- autoload :Responder, 'wechat/responder'
15
- autoload :Cipher, 'wechat/cipher'
16
- autoload :ControllerApi, 'wechat/controller_api'
17
-
18
12
  class AccessTokenExpiredError < StandardError; end
19
13
 
20
14
  class InvalidCredentialError < StandardError; end
@@ -55,3 +49,21 @@ module Wechat
55
49
  end
56
50
 
57
51
  ActionView::Base.include Wechat::Helpers if defined? ActionView::Base
52
+ require 'action_controller/wechat_responder' # To make wechat_api and wechat_responder available
53
+
54
+ module ActionController
55
+ if defined? Base
56
+ ActiveSupport.on_load(:action_controller_base) do
57
+ class << Base
58
+ include WechatResponder
59
+ end
60
+ end
61
+ end
62
+ if defined? API
63
+ ActiveSupport.on_load(:action_controller_api) do
64
+ class << API
65
+ include WechatResponder
66
+ end
67
+ end
68
+ end
69
+ end
data/lib/wechat/api.rb CHANGED
@@ -1,11 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'wechat/api_base'
4
- require 'wechat/http_client'
5
- require 'wechat/token/public_access_token'
6
- require 'wechat/ticket/public_jsapi_ticket'
7
- require 'wechat/concern/common'
8
-
9
3
  module Wechat
10
4
  class Api < ApiBase
11
5
  def initialize(appid, secret, token_file, timeout, skip_verify_ssl, jsapi_ticket_file)
@@ -148,8 +148,15 @@ module Wechat
148
148
  post 'material/batchget_material', JSON.generate(type: type, offset: offset, count: count)
149
149
  end
150
150
 
151
- def material_add(type, file)
152
- post_file 'material/add_material', file, params: { type: type }
151
+ def material_add(type, file, opts = {})
152
+ params = { type: type }
153
+ params.merge!(description: opts.slice(:title, :introduction).to_json) if type == 'video'
154
+
155
+ post_file 'material/add_material', file, params: params
156
+ end
157
+
158
+ def material_add_news(mpnews_message)
159
+ post 'material/add_news', mpnews_message.to_json
153
160
  end
154
161
 
155
162
  def material_delete(media_id)
@@ -58,7 +58,7 @@ module Wechat
58
58
  cookies.signed_or_encrypted[:we_access_token] = { value: access_info['access_token'], expires: self.class.oauth2_cookie_duration.from_now }
59
59
  yield access_info['openid'], access_info
60
60
  else
61
- Rails::VERSION::MAJOR >= 6 ? (redirect_to generate_oauth2_url(oauth2_params), allow_other_host: true) : (redirect_to generate_oauth2_url(oauth2_params))
61
+ redirect_to generate_oauth2_url(oauth2_params), allow_other_host: true
62
62
  end
63
63
  end
64
64
 
@@ -73,7 +73,7 @@ module Wechat
73
73
  cookies.signed_or_encrypted[:we_deviceid] = { value: userinfo['DeviceId'], expires: self.class.oauth2_cookie_duration.from_now }
74
74
  yield userinfo['UserId'], userinfo
75
75
  else
76
- Rails::VERSION::MAJOR >= 6 ? (redirect_to generate_oauth2_url(oauth2_params), allow_other_host: true) : (redirect_to generate_oauth2_url(oauth2_params))
76
+ redirect_to generate_oauth2_url(oauth2_params), allow_other_host: true
77
77
  end
78
78
  end
79
79
 
@@ -1,10 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'wechat/api_base'
4
- require 'wechat/http_client'
5
- require 'wechat/token/corp_access_token'
6
- require 'wechat/ticket/corp_jsapi_ticket'
7
-
8
3
  module Wechat
9
4
  class CorpApi < ApiBase
10
5
  attr_reader :agentid
@@ -34,7 +34,8 @@ module Wechat
34
34
  timestamp: "#{js_hash[:timestamp]}",
35
35
  nonceStr: "#{js_hash[:noncestr]}",
36
36
  signature: "#{js_hash[:signature]}",
37
- jsApiList: ['#{config_options[:api].join("','")}']
37
+ jsApiList: ['#{config_options[:api]&.join("','")}'],
38
+ openTagList: ['#{config_options[:open_tags]&.join("','")}']
38
39
  });
39
40
  WECHAT_CONFIG_JS
40
41
  javascript_tag config_js, type: 'application/javascript'
data/lib/wechat/mp_api.rb CHANGED
@@ -1,13 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'wechat/api_base'
4
- require 'wechat/http_client'
5
- require 'wechat/token/public_access_token'
6
- require 'wechat/ticket/public_jsapi_ticket'
7
- require 'wechat/qcloud/token'
8
- require 'wechat/concern/common'
9
- require 'wechat/concern/qcloud'
10
-
11
3
  module Wechat
12
4
  class MpApi < ApiBase
13
5
  def initialize(appid, secret, token_file, timeout, skip_verify_ssl, jsapi_ticket_file, qcloud_env, qcloud_token_file, qcloud_token_lifespan)
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'English'
4
4
  require 'rexml/document'
5
- require 'wechat/signature'
6
5
 
7
6
  module Wechat
8
7
  module Responder
@@ -11,24 +10,9 @@ module Wechat
11
10
  include Cipher
12
11
 
13
12
  included do
14
- # Rails 5 remove before_filter and skip_before_filter
15
- if respond_to?(:skip_before_action)
16
- if respond_to?(:verify_authenticity_token)
17
- skip_before_action :verify_authenticity_token
18
- else
19
- # Rails 5 API mode won't define verify_authenticity_token
20
- # https://github.com/rails/rails/blob/v5.0.0.beta3/actionpack/lib/abstract_controller/callbacks.rb#L66
21
- # https://github.com/rails/rails/blob/v5.0.0.beta3/activesupport/lib/active_support/callbacks.rb#L640
22
- skip_before_action :verify_authenticity_token, raise: false
23
- end
24
-
25
- before_action :config_account, only: %i[show create]
26
- before_action :verify_signature, only: %i[show create]
27
- else
28
- skip_before_filter :verify_authenticity_token
29
- before_filter :config_account, only: %i[show create]
30
- before_filter :verify_signature, only: %i[show create]
31
- end
13
+ skip_before_action :verify_authenticity_token, raise: false
14
+ before_action :config_account, only: %i[show create]
15
+ before_action :verify_signature, only: %i[show create]
32
16
  end
33
17
 
34
18
  module ClassMethods
@@ -180,15 +164,9 @@ module Wechat
180
164
  def show
181
165
  if @we_corpid.present?
182
166
  echostr, _corp_id = unpack(decrypt(Base64.decode64(params[:echostr]), @we_encoding_aes_key))
183
- if Rails::VERSION::MAJOR >= 4
184
- render plain: echostr
185
- else
186
- render text: echostr
187
- end
188
- elsif Rails::VERSION::MAJOR >= 4
189
- render plain: params[:echostr]
167
+ render plain: echostr
190
168
  else
191
- render text: params[:echostr]
169
+ render plain: params[:echostr]
192
170
  end
193
171
  end
194
172
 
@@ -197,11 +175,7 @@ module Wechat
197
175
  response_msg = run_responder(request_msg)
198
176
 
199
177
  if response_msg.respond_to? :to_xml
200
- if Rails::VERSION::MAJOR >= 4
201
- render plain: process_response(response_msg)
202
- else
203
- render text: process_response(response_msg)
204
- end
178
+ render plain: process_response(response_msg)
205
179
  else
206
180
  head :ok, content_type: 'text/html'
207
181
  end
@@ -248,15 +222,9 @@ module Wechat
248
222
  end
249
223
 
250
224
  data_hash = data.fetch('xml', {})
251
- if Rails::VERSION::MAJOR >= 5
252
- data_hash = data_hash.to_unsafe_hash if data_hash.instance_of?(ActionController::Parameters)
253
- HashWithIndifferentAccess.new(data_hash).tap do |msg|
254
- msg[:Event]&.downcase!
255
- end
256
- else
257
- HashWithIndifferentAccess.new_from_hash_copying_default(data_hash).tap do |msg|
258
- msg[:Event]&.downcase!
259
- end
225
+ data_hash = data_hash.to_unsafe_hash if data_hash.instance_of?(ActionController::Parameters)
226
+ HashWithIndifferentAccess.new(data_hash).tap do |msg|
227
+ msg[:Event]&.downcase!
260
228
  end
261
229
  end
262
230
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'wechat/ticket/jsapi_base'
4
-
5
3
  module Wechat
6
4
  module Ticket
7
5
  class CorpJsapiTicket < JsapiBase
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'wechat/ticket/jsapi_base'
4
-
5
3
  module Wechat
6
4
  module Ticket
7
5
  class PublicJsapiTicket < JsapiBase
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'wechat/token/access_token_base'
4
-
5
3
  module Wechat
6
4
  module Token
7
5
  class CorpAccessToken < AccessTokenBase
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'wechat/token/access_token_base'
4
-
5
3
  module Wechat
6
4
  module Token
7
5
  class PublicAccessToken < AccessTokenBase
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.12.3
4
+ version: 0.13.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skinnyworm
@@ -35,7 +35,7 @@ cert_chain:
35
35
  spvOK5/LPXWX6ZGc2SR8SH/s7ftYH2EkeM1VUbtemow08NdgCwJ4IG+fRQ9dcrJ+
36
36
  L9TbpLHvVrCe1w8duMqNeUmqj+M1iC/5Zst2vIe14QcOTuAh
37
37
  -----END CERTIFICATE-----
38
- date: 2021-03-15 00:00:00.000000000 Z
38
+ date: 2021-06-18 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: activesupport
@@ -43,7 +43,7 @@ dependencies:
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: '3.2'
46
+ version: '6.0'
47
47
  - - "<"
48
48
  - !ruby/object:Gem::Version
49
49
  version: '7'
@@ -53,7 +53,7 @@ dependencies:
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: '3.2'
56
+ version: '6.0'
57
57
  - - "<"
58
58
  - !ruby/object:Gem::Version
59
59
  version: '7'
@@ -66,7 +66,7 @@ dependencies:
66
66
  version: 1.0.4
67
67
  - - "<"
68
68
  - !ruby/object:Gem::Version
69
- version: '5'
69
+ version: '6'
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: 1.0.4
77
77
  - - "<"
78
78
  - !ruby/object:Gem::Version
79
- version: '5'
79
+ version: '6'
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: nokogiri
82
82
  requirement: !ruby/object:Gem::Requirement
@@ -119,48 +119,62 @@ dependencies:
119
119
  - - ">="
120
120
  - !ruby/object:Gem::Version
121
121
  version: '0'
122
+ - !ruby/object:Gem::Dependency
123
+ name: zeitwerk
124
+ requirement: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: '2.4'
129
+ type: :runtime
130
+ prerelease: false
131
+ version_requirements: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - "~>"
134
+ - !ruby/object:Gem::Version
135
+ version: '2.4'
122
136
  - !ruby/object:Gem::Dependency
123
137
  name: rubocop
124
138
  requirement: !ruby/object:Gem::Requirement
125
139
  requirements:
126
140
  - - "~>"
127
141
  - !ruby/object:Gem::Version
128
- version: '1.0'
142
+ version: '1.9'
129
143
  type: :development
130
144
  prerelease: false
131
145
  version_requirements: !ruby/object:Gem::Requirement
132
146
  requirements:
133
147
  - - "~>"
134
148
  - !ruby/object:Gem::Version
135
- version: '1.0'
149
+ version: '1.9'
136
150
  - !ruby/object:Gem::Dependency
137
151
  name: rails
138
152
  requirement: !ruby/object:Gem::Requirement
139
153
  requirements:
140
154
  - - ">="
141
155
  - !ruby/object:Gem::Version
142
- version: '5.1'
156
+ version: '6.0'
143
157
  type: :development
144
158
  prerelease: false
145
159
  version_requirements: !ruby/object:Gem::Requirement
146
160
  requirements:
147
161
  - - ">="
148
162
  - !ruby/object:Gem::Version
149
- version: '5.1'
163
+ version: '6.0'
150
164
  - !ruby/object:Gem::Dependency
151
165
  name: rspec-rails
152
166
  requirement: !ruby/object:Gem::Requirement
153
167
  requirements:
154
168
  - - "~>"
155
169
  - !ruby/object:Gem::Version
156
- version: '4.0'
170
+ version: '5.0'
157
171
  type: :development
158
172
  prerelease: false
159
173
  version_requirements: !ruby/object:Gem::Requirement
160
174
  requirements:
161
175
  - - "~>"
162
176
  - !ruby/object:Gem::Version
163
- version: '4.0'
177
+ version: '5.0'
164
178
  - !ruby/object:Gem::Dependency
165
179
  name: sqlite3
166
180
  requirement: !ruby/object:Gem::Requirement
@@ -237,14 +251,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
237
251
  requirements:
238
252
  - - ">="
239
253
  - !ruby/object:Gem::Version
240
- version: '2.4'
254
+ version: '2.6'
241
255
  required_rubygems_version: !ruby/object:Gem::Requirement
242
256
  requirements:
243
257
  - - ">="
244
258
  - !ruby/object:Gem::Version
245
259
  version: '0'
246
260
  requirements: []
247
- rubygems_version: 3.2.14
261
+ rubygems_version: 3.2.20
248
262
  signing_key:
249
263
  specification_version: 4
250
264
  summary: DSL for wechat message handling and API