Class: XiWechatCorp::Callback::Request
- Inherits:
-
Object
- Object
- XiWechatCorp::Callback::Request
- Defined in:
- lib/xi_wechat_corp/callback/request.rb
Constant Summary collapse
- SIGN_PARAMS =
%w(timestamp nonce echostr Encrypt)
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #agent_id ⇒ Object
- #assign(query_params, xml = nil) ⇒ Object
- #build_response(xml = nil) ⇒ Object
- #challenge ⇒ Object
- #challenge? ⇒ Boolean
- #corp_id ⇒ Object
- #decrypt ⇒ Object
- #decryptable? ⇒ Boolean
-
#initialize(cryptor, signer, query_params = {}, xml = nil) ⇒ Request
constructor
A new instance of Request.
- #key?(key) ⇒ Boolean
- #verify ⇒ Object
- #verify! ⇒ Object
Constructor Details
#initialize(cryptor, signer, query_params = {}, xml = nil) ⇒ Request
Returns a new instance of Request.
11 12 13 14 15 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 11 def initialize(cryptor, signer, query_params = {}, xml = nil) @cryptor = cryptor @signer = signer assign(query_params, xml) end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 9 def params @params end |
Instance Method Details
#[](key) ⇒ Object
27 28 29 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 27 def [](key) @params[key.to_s] end |
#agent_id ⇒ Object
67 68 69 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 67 def agent_id @params['AgentID'] end |
#assign(query_params, xml = nil) ⇒ Object
21 22 23 24 25 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 21 def assign(query_params, xml = nil) xml = xml.to_s body_params = xml.size > 0 ? parse_xml(xml) : {} @params = body_params.merge(query_params) end |
#build_response(xml = nil) ⇒ Object
17 18 19 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 17 def build_response(xml = nil) Response.new(@cryptor, @signer, xml) end |
#challenge ⇒ Object
48 49 50 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 48 def challenge @cryptor.decrypt(@params['echostr']) end |
#challenge? ⇒ Boolean
44 45 46 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 44 def challenge? key?('echostr') end |
#corp_id ⇒ Object
63 64 65 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 63 def corp_id @params['ToUserName'] end |
#decrypt ⇒ Object
56 57 58 59 60 61 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 56 def decrypt if decryptable? @params.merge!(parse_xml(@cryptor.decrypt(@params['Encrypt']))) end @params end |
#decryptable? ⇒ Boolean
52 53 54 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 52 def decryptable? key?('Encrypt') end |
#key?(key) ⇒ Boolean
31 32 33 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 31 def key?(key) @params.key?(key.to_s) end |
#verify ⇒ Object
35 36 37 38 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 35 def verify signature = @params['msg_signature'] signature && signature == @signer.sign(*@params.values_at(*SIGN_PARAMS)) end |
#verify! ⇒ Object
40 41 42 |
# File 'lib/xi_wechat_corp/callback/request.rb', line 40 def verify! raise SignatureVerificationError unless verify end |