Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def connect(location, timeout)
- # capture debug output in instance variable, use SSL if necessary
- uri = URI.parse(location.to_s)
- http = Net::HTTP.new(uri.host, uri.port)
- http.set_debug_output @trace
- if uri.scheme == 'https' then
- http.use_ssl = true
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- end
- # open connection, yielding HTTP object and path to block
- begin
- response = Timeout::timeout(timeout) do
- http.start do |http|
- yield http, uri.request_uri
- end
- end
- rescue Timeout::Error
- #@dump_trace = true
- @trace << "\r\n*** Timeout::Error -- #{timeout}s ***"
- timeout *= 2
- retry
- rescue IOError
- #@dump_trace = true
- @trace << "\r\n*** IOError: #{$!} ***\r\n"
- retry
- end
- response
- end #connect()
- #---------------------------
- # Perform an HTTP POST
- # returns response
- #---------------------------
- def post(location, form_variables, timeout=@initial_timeout)
- post_data = form_variables.map do |key, value|
- CGI::escape(key.to_s) + '=' + CGI::escape(value.to_s)
- end.join('&')
- # Connect, POST, return response. Make sure to set Content-Type
- # explicitly.
- @headers['Content-Type'] = 'application/x-www-form-urlencoded'
- connect(location, timeout) do |http, path|
- http.post(path, post_data, @headers)
- end
- end # post()
- def authenticate(config)
- raise ArgumentError, 'No endpoint params specified in config', caller if config['endpoint_params'].nil?
- raise ArgumentError, 'No endpoint URI specified in config', caller if config['endpoint_uri'].nil?
- auth_params = config['endpoint_params']
- location = config['endpoint_uri']
- @headers['Content-Type'] = 'application/x-www-form-urlencoded'
- response = connect(location, 5) do |http, path|
- http.post(path, auth_params, @headers)
- end
- end
Add Comment
Please, Sign In to add comment