Class: PinganApi::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pingan_api/base.rb

Direct Known Subclasses

AssistanceBusiness

Instance Method Summary collapse

Instance Method Details

#send_request(body) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pingan_api/base.rb', line 12

def send_request(body)
  #File.open("tmp/#{Time.now.to_i}.xml", "w") { |file| file.write body }
  uri = URI(PinganApi.config.server)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  pem = File.read(PinganApi.config.cert)
  http.cert = OpenSSL::X509::Certificate.new(pem)
  http.key = OpenSSL::PKey::RSA.new(pem)

  request = Net::HTTP::Post.new("/", initheader = {'Content-Type' =>'application/xml;charset=GBK'})
  request.body = body.encode('GBK')

  return send_request_and_log(http, request)
end

#send_request_and_log(http, request) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pingan_api/base.rb', line 30

def send_request_and_log(http, request)
  #log = PinganApi::Models::Log.new
  # TODO,save request
  #log.write_request(request)
  #begin
    # TODO, save response
    #File.open("tmp/#{Time.now.to_i}_request.xml", "w") { |file| file.write request.body }
    res = http.request(request)
    #log.write_response(res)
    #File.open("tmp/#{Time.now.to_i}_response.xml", "w") { |file| file.write res.body.force_encoding("GBK") }

    return res.body.force_encoding("GBK")
  #rescue => e
    #log.write_error(e.to_s)
  #  return nil
  #end
end

#send_request_with_data_and_template(data, template_path) ⇒ Object



6
7
8
9
10
# File 'lib/pingan_api/base.rb', line 6

def send_request_with_data_and_template(data, template_path)
  erb = ERB.new(File.read(template_path))
  body = erb.result(data.get_binding)
  return send_request(body)
end