Class: XiWechatCorp::Callback::Response

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

Constant Summary collapse

TEMPLATE =
'<xml><Encrypt>%s</Encrypt><MsgSignature>%s</MsgSignature><TimeStamp>%d</TimeStamp><Nonce>%d</Nonce></xml>'
NONCE_UPPER =
1 << 32

Instance Method Summary collapse

Constructor Details

#initialize(cryptor, signer, xml = nil) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
# File 'lib/xi_wechat_corp/callback/response.rb', line 9

def initialize(cryptor, signer, xml = nil)
  @cryptor = cryptor
  @signer = signer
  assign(xml)
end

Instance Method Details

#assign(xml) ⇒ Object



15
16
17
# File 'lib/xi_wechat_corp/callback/response.rb', line 15

def assign(xml)
  @encrypt = xml ? @cryptor.encrypt(xml) : nil
end

#to_sObject Also known as: to_xml



19
20
21
22
23
24
25
# File 'lib/xi_wechat_corp/callback/response.rb', line 19

def to_s
  return '' if @encrypt.nil?
  timestamp = Time.now.to_i
  nonce = SecureRandom.random_number(NONCE_UPPER)
  signature = @signer.sign(@encrypt, timestamp, nonce)
  sprintf(TEMPLATE, @encrypt, signature, timestamp, nonce)
end