Guest User

Untitled

a guest
Feb 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. def connect(location, timeout)
  2.  
  3. # capture debug output in instance variable, use SSL if necessary
  4. uri = URI.parse(location.to_s)
  5. http = Net::HTTP.new(uri.host, uri.port)
  6. http.set_debug_output @trace
  7. if uri.scheme == 'https' then
  8. http.use_ssl = true
  9. http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  10. end
  11.  
  12. # open connection, yielding HTTP object and path to block
  13. begin
  14. response = Timeout::timeout(timeout) do
  15. http.start do |http|
  16. yield http, uri.request_uri
  17. end
  18. end
  19. rescue Timeout::Error
  20. #@dump_trace = true
  21. @trace << "\r\n*** Timeout::Error -- #{timeout}s ***"
  22. timeout *= 2
  23. retry
  24. rescue IOError
  25. #@dump_trace = true
  26. @trace << "\r\n*** IOError: #{$!} ***\r\n"
  27. retry
  28. end
  29. response
  30. end #connect()
  31.  
  32. #---------------------------
  33. # Perform an HTTP POST
  34. # returns response
  35. #---------------------------
  36. def post(location, form_variables, timeout=@initial_timeout)
  37. post_data = form_variables.map do |key, value|
  38. CGI::escape(key.to_s) + '=' + CGI::escape(value.to_s)
  39. end.join('&')
  40.  
  41. # Connect, POST, return response. Make sure to set Content-Type
  42. # explicitly.
  43. @headers['Content-Type'] = 'application/x-www-form-urlencoded'
  44. connect(location, timeout) do |http, path|
  45. http.post(path, post_data, @headers)
  46. end
  47. end # post()
  48.  
  49. def authenticate(config)
  50.  
  51. raise ArgumentError, 'No endpoint params specified in config', caller if config['endpoint_params'].nil?
  52. raise ArgumentError, 'No endpoint URI specified in config', caller if config['endpoint_uri'].nil?
  53.  
  54. auth_params = config['endpoint_params']
  55. location = config['endpoint_uri']
  56. @headers['Content-Type'] = 'application/x-www-form-urlencoded'
  57.  
  58. response = connect(location, 5) do |http, path|
  59. http.post(path, auth_params, @headers)
  60. end
  61.  
  62. end
Add Comment
Please, Sign In to add comment