weixin_rails_middleware 1.0.3 → 1.0.4

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: 381fcd2eb890939d2b4cd3c6738aa2ef2ce38982
4
- data.tar.gz: 1eaf214d92befdd0e3646eba489264526fabdf90
3
+ metadata.gz: f864df00b4c607bd3d9c96217d8a448ca8d4b9ff
4
+ data.tar.gz: cf2e9c7be77b62d6e7bdb87c70eaad6351e2b00d
5
5
  SHA512:
6
- metadata.gz: 80698d8806763968e4c432b143c3229a67fd6740ff7b8b53596fe763d06cd668e5e34847151d5df6c04d485d460ed24eec3ddd8cf89ce7e326e81fb33a41cb50
7
- data.tar.gz: 3b08680f1058874ec2f6e39798ad1cc05b5075abc998d6423398ef06afe098e40057068cafdfa9c657284793f589a16096779ed29ce2426bb4aad4645bf25d20
6
+ metadata.gz: ae8b8f0bd47219fe7876db2d728334d7c269320ab3b2a71a77ce1290f46baf3046ab765d2f3226ce1ce807e1ea8f5929536d688fac6fb8fd75cbfe451300a5a0
7
+ data.tar.gz: a674a9bfa8f46348b62aaa51cc15613544449f82ac40a5c8c725f3bf2e42b431e03f042c51d3a0ff4553e6a12efc0ecdf3180dd675f6e37f933af53a79776950
@@ -1,8 +1,9 @@
1
1
  module WeixinRailsMiddleware
2
- class WeixinController < ApplicationController
3
- include WeixinMessageHelper
2
+ class WeixinController < ActionController::Base
3
+ include ReplyWeixinMessageHelper
4
+ include ConfigurationHelpers
4
5
 
5
- skip_before_filter :verify_authenticity_token
6
+ skip_before_action :verify_authenticity_token
6
7
  before_action :check_weixin_params, only: [:index, :reply]
7
8
  before_action :set_weixin_public_account, :set_weixin_message, only: :reply
8
9
 
@@ -16,22 +17,20 @@ module WeixinRailsMiddleware
16
17
  protected
17
18
 
18
19
  def check_weixin_params
19
- if check_weixin_token_valid?
20
- if !is_hexdigest?
21
- render text: "Forbidden", status: 403
22
- end
20
+ if check_weixin_token_valid? && !is_hexdigest?
21
+ render text: "Forbidden", status: 403
23
22
  end
24
23
  end
25
24
 
26
25
  # check the token from Weixin Service is exist in local store.
27
26
  def check_weixin_token_valid?
28
- if WeixinRailsMiddleware.config.token_string.blank?
27
+ if token_string.blank?
29
28
  if token_model_instance.blank?
30
29
  render text: "Forbidden", status: 403
31
30
  return false
32
31
  end
33
32
  else
34
- if current_weixin_token != WeixinRailsMiddleware.config.token_string
33
+ if current_weixin_token != token_string
35
34
  render text: "Forbidden", status: 403
36
35
  return false
37
36
  end
@@ -42,26 +41,16 @@ module WeixinRailsMiddleware
42
41
  def is_hexdigest?
43
42
  signature = params[:signature] || ''
44
43
  timestamp = params[:timestamp] || ''
45
- nonce = params[:nonce] || ''
44
+ nonce = params[:nonce] || ''
46
45
  current_signature = Digest::SHA1.hexdigest([current_weixin_token, timestamp, nonce].sort.join)
47
46
  return true if current_signature == signature
48
47
  false
49
48
  end
50
49
 
51
- def current_weixin_token
52
- @weixin_token = params[:weixin_token]
53
- end
54
-
55
- def token_model_instance
56
- token_model = WeixinRailsMiddleware.config.token_model_class
57
- token_column = WeixinRailsMiddleware.config.token_column
58
- token_model_instance = token_model.where("#{token_column}" => current_weixin_token).first
59
- token_model_instance
60
- end
61
-
50
+ ## Callback
62
51
  # e.g. will generate +@weixin_public_account+
63
52
  def set_weixin_public_account
64
- return nil if WeixinRailsMiddleware.config.token_string.present?
53
+ return nil if token_string.present?
65
54
  @weixin_public_account ||= token_model_instance
66
55
  end
67
56
 
@@ -70,14 +59,18 @@ module WeixinRailsMiddleware
70
59
  @weixin_message ||= current_weixin_message
71
60
  end
72
61
 
73
- # take the weixin params
74
- def current_weixin_params
75
- request.body.read
62
+ def current_weixin_token
63
+ @weixin_token = params[:weixin_token]
64
+ end
65
+
66
+ def token_model_instance
67
+ token_model_instance = token_model_class.where("#{token_column}" => current_weixin_token).first
68
+ token_model_instance
76
69
  end
77
70
 
78
71
  # return a message class with current_weixin_params
79
72
  def current_weixin_message
80
- Message.factory(current_weixin_params)
73
+ Message.factory(request.body.read)
81
74
  end
82
75
 
83
76
  end
@@ -2,19 +2,21 @@
2
2
  WeixinRailsMiddleware.configure do |config|
3
3
 
4
4
  ## NOTE:
5
- ## if you config all them, it will use `token_string` default
5
+ ## If you config all them, it will use `token_string` default
6
6
  ##
7
- # Th FIRST configure
8
- # if you config `token_model`, it will use it to find_by_weixin_token
9
- # you must config a column name, `weixin_token` default
10
- # config.token_model = "" # ActiveRecord subclass or other ORM subclass
7
+ # Th first configure is fit for your weixin public_account is saved in database.
8
+ # +token_model+ and +token_column+ must in the same table.
9
+ # +token_model+ The class name that to save your public_account
10
+ # +token_column+ You can config a column name Optional, but you must have a column `weixin_token` default.
11
+ # config.token_model = ""
11
12
  # config.token_column = "weixin_token"
12
13
 
13
- # OR the SECOND configure
14
- # if you config `token_string`, so it will directly use it
14
+ # Or the other configure is fit for only one weixin public_account
15
+ # If you config `token_string`, so it will directly use it
15
16
  # config.token_string = "token string"
16
17
 
17
- # router
18
+ # Router
19
+ # Default is "/", and recommend you use default directly.
18
20
  # config.engine_path = "/"
19
21
 
20
22
  end
@@ -10,7 +10,6 @@ WeixinRailsMiddleware::WeixinController.class_eval do
10
10
 
11
11
  def response_text_message(options={})
12
12
  reply_text_message("Your Message: #{@weixin_message.Content}")
13
-
14
13
  end
15
14
 
16
15
  def response_location_message(options={})
@@ -9,14 +9,36 @@ module WeixinRailsMiddleware
9
9
  @token_column = DEFAULT_TOKEN_COLUMN_NAME
10
10
  end
11
11
 
12
+ end
13
+
14
+ module ConfigurationHelpers
15
+ extend ActiveSupport::Concern
16
+
17
+ def engine_path
18
+ @engine_path ||= WeixinRailsMiddleware.config.engine_path
19
+ end
20
+
21
+ def token_string
22
+ @token_string ||= WeixinRailsMiddleware.config.token_string.to_s
23
+ end
24
+
25
+ def token_column
26
+ @token_column ||= WeixinRailsMiddleware.config.token_column
27
+ end
28
+
29
+ def token_model
30
+ @token_model ||= WeixinRailsMiddleware.config.token_model
31
+ end
32
+
33
+ def is_default_engine_path?
34
+ engine_path == DEFAULT_ENGINE_PATH # "/"
35
+ end
36
+
12
37
  def token_model_class
13
- raise "You need to config `token_model` in config/initializers/weixin_rails_middleware.rb" if token_model.blank?
14
- token_model_c = token_model.constantize
15
- unless token_model_c.table_exists?
16
- raise "You don't have #{token_model_c.table_name} table"
38
+ if token_model.blank?
39
+ raise "You need to config `token_model` in 'config/initializers/weixin_rails_middleware.rb'"
17
40
  end
18
- token_model_c
41
+ @token_model_c ||= token_model.constantize
19
42
  end
20
-
21
43
  end
22
44
  end
@@ -1,5 +1,14 @@
1
1
  module WeixinRailsMiddleware
2
2
  class Engine < ::Rails::Engine
3
+ include ConfigurationHelpers
4
+
3
5
  isolate_namespace WeixinRailsMiddleware
6
+
7
+ config.after_initialize do
8
+ if token_model_class.present?
9
+ token_model_class.send(:include, WeixinServerUrl::InstanceMethods)
10
+ end
11
+ end
12
+
4
13
  end
5
- end
14
+ end
@@ -1,5 +1,5 @@
1
1
  module WeixinRailsMiddleware
2
- module WeixinMessageHelper
2
+ module ReplyWeixinMessageHelper
3
3
 
4
4
  # e.g.
5
5
  # reply_text_message(@weixin_message.ToUserName, @weixin_message.FromUserName, "Your Message: #{@weixin_message.Content}")
@@ -1,5 +1,5 @@
1
1
  module WeixinRailsMiddleware
2
- module UniqueToken
2
+ module UniqueTokenHelper
3
3
  def self.generate(options = {})
4
4
  # SecureRandom: hex, base64, random_bytes, urlsafe_base64, random_number, uuid
5
5
  generator_method_type = options.delete(:generator).try(:to_sym) || :hex
@@ -14,4 +14,4 @@ module WeixinRailsMiddleware
14
14
  end
15
15
  end
16
16
 
17
- WeiXinUniqueToken = WeixinRailsMiddleware::UniqueToken
17
+ WeiXinUniqueToken = WeixinRailsMiddleware::UniqueTokenHelper
@@ -0,0 +1,69 @@
1
+ # TODO: added weixin_server_url for User
2
+ # if config +token_model+, e.g. user = User.first; user.weixin_server_url
3
+ # if config +token_string+, e.g. weixin_server_url
4
+ # a public url looks like: weixin_server_url
5
+ module WeixinRailsMiddleware
6
+ module WeixinServerUrl
7
+
8
+ module Base
9
+
10
+ private
11
+
12
+ def weixin_server_url_with_public_account(public_account)
13
+ base_host_url + current_token(public_account)
14
+ end
15
+
16
+ def weixin_server_url_without_public_account
17
+ base_host_url + token_string
18
+ end
19
+
20
+ def current_token(public_account)
21
+ if public_account.blank?
22
+ token_string
23
+ else
24
+ public_account.try(token_column)
25
+ end
26
+ end
27
+
28
+ def base_host_url
29
+ host_url = request.base_url
30
+ if is_default_engine_path? # "/"
31
+ "#{host_url}/weixin/"
32
+ else
33
+ "#{host_url}/#{engine_path}/weixin/"
34
+ end
35
+ end
36
+ end
37
+
38
+ module ViewHelpers
39
+ extend ActiveSupport::Concern
40
+ include ConfigurationHelpers
41
+ include Base
42
+ included do
43
+ def weixin_server_url(public_account=nil)
44
+ raise "You need pass a public_account instance." if token_string.blank? && public_account.blank?
45
+ if public_account.present?
46
+ weixin_server_url_with_public_account(public_account)
47
+ else
48
+ weixin_server_url_without_public_account(public_account)
49
+ end
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ module InstanceMethods
56
+ extend ActiveSupport::Concern
57
+ include ConfigurationHelpers
58
+ include Base
59
+ included do
60
+ def weixin_server_url
61
+ # base_host_url + self.send(token_column)
62
+ warn "Hi, this method is Pending, please use view helper: +weixin_server_url+ :)"
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ ActionView::Base.send :include, WeixinRailsMiddleware::WeixinServerUrl::ViewHelpers
@@ -10,11 +10,12 @@ module ActionView
10
10
  @options = {value: generate_weixin_token }.merge!(@options)
11
11
  super
12
12
  end
13
+
13
14
  private
14
15
 
15
- def generate_weixin_token
16
- @options.fetch("value"){value_before_type_cast(object)} || WeiXinUniqueToken.generate
17
- end
16
+ def generate_weixin_token
17
+ @options.fetch("value"){value_before_type_cast(object)} || WeiXinUniqueToken.generate
18
+ end
18
19
  end
19
20
  end # end of Tags
20
21
 
@@ -27,7 +27,7 @@ module WeixinRailsMiddleware
27
27
  @source.MsgId.to_i
28
28
  end
29
29
 
30
- def Message.factory(xml)
30
+ def self.factory(xml)
31
31
  hash = MultiXml.parse(xml)['xml']
32
32
  case hash['MsgType']
33
33
  when 'text'
@@ -9,10 +9,10 @@ module WeixinRailsMiddleware
9
9
  xml_name :xml
10
10
  #xml_convention :camelcase
11
11
 
12
- xml_accessor :ToUserName, :cdata => true
12
+ xml_accessor :ToUserName, :cdata => true
13
13
  xml_accessor :FromUserName, :cdata => true
14
- xml_reader :CreateTime, :as => Integer
15
- xml_reader :MsgType, :cdata => true
14
+ xml_reader :CreateTime, :as => Integer
15
+ xml_reader :MsgType, :cdata => true
16
16
 
17
17
  def initialize
18
18
  @CreateTime = Time.now.to_i
@@ -34,8 +34,8 @@ module WeixinRailsMiddleware
34
34
  class TextReplyMessage < ReplyMessage
35
35
  xml_accessor :Content, :cdata => true
36
36
  def initialize
37
- super
38
- @MsgType = 'text'
37
+ super
38
+ @MsgType = 'text'
39
39
  end
40
40
  end
41
41
 
@@ -43,7 +43,7 @@ module WeixinRailsMiddleware
43
43
  include ROXML
44
44
  xml_accessor :Title, :cdata => true
45
45
  xml_accessor :Description, :cdata => true
46
- xml_accessor :MusicUrl, :cdata => true
46
+ xml_accessor :MusicUrl, :cdata => true
47
47
  xml_accessor :HQMusicUrl, :cdata => true
48
48
  end
49
49
 
@@ -74,7 +74,7 @@ module WeixinRailsMiddleware
74
74
  xml_accessor :Title, :cdata => true
75
75
  xml_accessor :Description, :cdata => true
76
76
  xml_accessor :PicUrl, :cdata => true
77
- xml_accessor :Url, :cdata => true
77
+ xml_accessor :Url, :cdata => true
78
78
  end
79
79
 
80
80
  # <xml>
@@ -180,4 +180,4 @@ module WeixinRailsMiddleware
180
180
  end
181
181
  end
182
182
 
183
- end
183
+ end
@@ -1,3 +1,3 @@
1
1
  module WeixinRailsMiddleware
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4".freeze
3
3
  end
@@ -1,11 +1,11 @@
1
- require "weixin_rails_middleware/engine"
2
1
  require "weixin_rails_middleware/configuration"
3
- require "weixin_rails_middleware/message"
4
- require "weixin_rails_middleware/reply_message"
5
- require "weixin_rails_middleware/weixin_message_helper"
6
- require "weixin_rails_middleware/weixin_token_form_helper"
7
- require "weixin_rails_middleware/unique_token_helper"
8
- require "weixin_rails_middleware/weixin_server_url"
2
+ require "weixin_rails_middleware/engine"
3
+ require "weixin_rails_middleware/models/message"
4
+ require "weixin_rails_middleware/models/reply_message"
5
+ require "weixin_rails_middleware/helpers/reply_weixin_message_helper"
6
+ require "weixin_rails_middleware/helpers/weixin_token_form_helper"
7
+ require "weixin_rails_middleware/helpers/unique_token_helper"
8
+ require "weixin_rails_middleware/helpers/weixin_server_url"
9
9
 
10
10
  module WeixinRailsMiddleware
11
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weixin_rails_middleware
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - lanrion
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-19 00:00:00.000000000 Z
11
+ date: 2014-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -103,7 +103,6 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - MIT-LICENSE
105
105
  - Rakefile
106
- - app/controllers/weixin_rails_middleware/application_controller.rb
107
106
  - app/controllers/weixin_rails_middleware/weixin_controller.rb
108
107
  - config/routes.rb
109
108
  - lib/generators/templates/initializer.rb
@@ -113,13 +112,13 @@ files:
113
112
  - lib/weixin_rails_middleware.rb
114
113
  - lib/weixin_rails_middleware/configuration.rb
115
114
  - lib/weixin_rails_middleware/engine.rb
116
- - lib/weixin_rails_middleware/message.rb
117
- - lib/weixin_rails_middleware/reply_message.rb
118
- - lib/weixin_rails_middleware/unique_token_helper.rb
115
+ - lib/weixin_rails_middleware/helpers/reply_weixin_message_helper.rb
116
+ - lib/weixin_rails_middleware/helpers/unique_token_helper.rb
117
+ - lib/weixin_rails_middleware/helpers/weixin_server_url.rb
118
+ - lib/weixin_rails_middleware/helpers/weixin_token_form_helper.rb
119
+ - lib/weixin_rails_middleware/models/message.rb
120
+ - lib/weixin_rails_middleware/models/reply_message.rb
119
121
  - lib/weixin_rails_middleware/version.rb
120
- - lib/weixin_rails_middleware/weixin_message_helper.rb
121
- - lib/weixin_rails_middleware/weixin_server_url.rb
122
- - lib/weixin_rails_middleware/weixin_token_form_helper.rb
123
122
  homepage: http://github.com/lanrion/weixin_rails_middleware
124
123
  licenses: []
125
124
  metadata: {}
@@ -1,4 +0,0 @@
1
- module WeixinRailsMiddleware
2
- class ApplicationController < ActionController::Base
3
- end
4
- end
@@ -1,14 +0,0 @@
1
- module WeixinRailsMiddleware
2
- module WeixinServerUrl
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- def weixin_server_url
7
- "http test"
8
- end
9
- end
10
- end
11
- end
12
-
13
-
14
-