Class: JwAlipay::Response

Inherits:
Object
  • Object
show all
Includes:
Sign
Defined in:
lib/jw_alipay/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sign

#_md5_sign, #_query_string, #_sign_string, #_sort_params, #sign, #verify_sign

Constructor Details

#initialize(res, options = {}) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jw_alipay/response.rb', line 7

def initialize(res, options = {})
  fields = Hash[res.split('&').map{ |f| f.split('=') }].symbolize_keys
  sign, sort = fields[:sign], options[:sort].nil? ? true : options[:sort]
  if sign and verify_sign(fields, sort)
    data = fields[options[:data_name]]
    @fields = fields
    @data = data ? parse_xml_data(data, options[:data_root]) : nil
  elsif sign
    @error = {:msg => :sign_error, :detail => 'sign error'}
  else
    err = fields[options[:error_name]]
    @error = err ? parse_xml_data(err, options[:error_root]) : nil
  end
end

Instance Attribute Details

#data ⇒ Object (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/jw_alipay/response.rb', line 5

def data
  @data
end

#error ⇒ Object (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/jw_alipay/response.rb', line 5

def error
  @error
end

#fields ⇒ Object (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/jw_alipay/response.rb', line 5

def fields
  @fields
end

Instance Method Details

#parse_xml_data(xml, root = nil) ⇒ Object



22
23
24
25
# File 'lib/jw_alipay/response.rb', line 22

def parse_xml_data(xml, root = nil)
  res = Hash.from_xml(CGI.unescape(xml)).symbolize_keys
  (root ? res[root] : res).symbolize_keys
end