Class: LitleOnline::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



35
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
# File 'lib/Communications.rb', line 35

def Communications.http_post(post_data,config_hash)
  
  proxy_addr = config_hash['proxy_addr']
  proxy_port = config_hash['proxy_port']
  litle_url = config_hash['url']
  
  # setup https or http post
  url = URI.parse(litle_url)
  
  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','Connection'=>'close'})
    response_xml = response
  }
  
  # 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