Class: XiWechatCorp::Callback::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/xi_wechat_corp/callback/request.rb

Constant Summary collapse

SIGN_PARAMS =
%w(timestamp nonce echostr Encrypt)

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#paramsObject (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_idObject



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

#challengeObject



48
49
50
# File 'lib/xi_wechat_corp/callback/request.rb', line 48

def challenge
  @cryptor.decrypt(@params['echostr'])
end

#challenge?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/xi_wechat_corp/callback/request.rb', line 44

def challenge?
  key?('echostr')
end

#corp_idObject



63
64
65
# File 'lib/xi_wechat_corp/callback/request.rb', line 63

def corp_id
  @params['ToUserName']
end

#decryptObject



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

Returns:

  • (Boolean)


52
53
54
# File 'lib/xi_wechat_corp/callback/request.rb', line 52

def decryptable?
  key?('Encrypt')
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/xi_wechat_corp/callback/request.rb', line 31

def key?(key)
  @params.key?(key.to_s)
end

#verifyObject



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