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
29
# 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
  http.read_timeout = 10

  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=' + PinganApi.config.encoding})
  request.body = body.encode(PinganApi.config.encoding)

  return send_request_and_log(http, request)
end

#send_request_and_log(http, request) ⇒ Object



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

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

    return res.body.force_encoding(PinganApi.config.encoding)
  #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