Class: CnpOnline::Communications

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

Class Method Summary collapse

Class Method Details

.http_post(post_data, config_hash) ⇒ Object

For http or https post with or without a proxy



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/Communications.rb', line 36

def Communications.http_post(post_data,config_hash)
  
  proxy_addr = config_hash['proxy_addr']
  proxy_port = config_hash['proxy_port']
  _commManager = CommManager.instance(config_hash)
  _requestTarget = _commManager.findUrl

  #cnp_url = config_hash['url']
  
  # setup https or http post
  url = URI.parse(_requestTarget.targetUrl)

  response_xml = nil
  https = Net::HTTP.new(url.host, url.port, proxy_addr, proxy_port)
  if(url.scheme == 'https') 
    https.use_ssl = url.scheme=='https'
    https.verify_mode = OpenSSL::SSL::VERIFY_PEER
    https.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
  end
  https.start { |http|
    response = http.request_post(url.path, post_data.to_s, {'Content-Type'=>'text/xml; charset=UTF-8','Connection'=>'close'})
    response_xml = response
    CommManager.instance(Configuration.new.config).reportResult(_requestTarget, _commManager.REQUEST_RESULT_RESPONSE_RECEIVED, response.code)
  }
  
  # validate response, only an HTTP 200 will work, redirects are not followed
  case response_xml
  when Net::HTTPOK
    return response_xml.body
  else
    raise("Error with http http_post_request, code:" + response_xml.header.code)
  end
end