class AdsCommon::Http

Constants

HTTP_OPEN_TIMEOUT
HTTP_READ_TIMEOUT

HTTP read and open timeouts in seconds.

Public Class Methods

get(url, config, headers = nil) click to toggle source

Performs a get on a URL, using all of the connection options in the client library, returning the response body as a string.

# File lib/ads_common/http.rb, line 42
def self.get(url, config, headers = nil)
  return get_response(url, config, headers).body
end
get_response(url, config, headers = nil) click to toggle source

Performs a get on a URL, using all of the connection options in the client library, returning a HTTPI::Response.

# File lib/ads_common/http.rb, line 34
def self.get_response(url, config, headers = nil)
  request = prepare_request(url, config, headers)
  response = HTTPI.get(request)
  return response
end
get_stream(url, config, headers = nil, &block) click to toggle source

Performs a get on a URL, using all of the connection options in the client library, sending the response piecemeal to the given block.

# File lib/ads_common/http.rb, line 48
def self.get_stream(url, config, headers = nil, &block)
  request = prepare_request(url, config, headers)
  request.on_body(&block)
  HTTPI.get(request)
  return nil
end
post(url, data, config, headers = nil) click to toggle source

Performs a post on a URL, using all of the connection options in the client library, returning the response body as a string.

# File lib/ads_common/http.rb, line 64
def self.post(url, data, config, headers = nil)
  return post_response(url, data, config, headers).body
end
post_response(url, data, config, headers = nil) click to toggle source

Performs a post on a URL, using all of the connection options in the client library, returning a HTTPI::Response.

# File lib/ads_common/http.rb, line 57
def self.post_response(url, data, config, headers = nil)
  request = prepare_request(url, config, headers, data)
  return HTTPI.post(request)
end
post_stream(url, data, config, headers = nil, &block) click to toggle source

Performs a post on a URL, using all of the connection options in the client library, sending the response piecemeal to the given block.

# File lib/ads_common/http.rb, line 70
def self.post_stream(url, data, config, headers = nil, &block)
  request = prepare_request(url, config, headers, data)
  request.on_body(&block)
  HTTPI.post(request)
  return nil
end
put(url, data, config, headers = nil) click to toggle source

Performs a put on a URL, using all of the connection options in the client library, returning the response body as a string.

# File lib/ads_common/http.rb, line 86
def self.put(url, data, config, headers = nil)
  return put_response(url, data, config, headers).body
end
put_response(url, data, config, headers = nil) click to toggle source

Performs a put on a URL, using all of the connection options in the client library, returning a HTTPI::Response.

# File lib/ads_common/http.rb, line 79
def self.put_response(url, data, config, headers = nil)
  request = prepare_request(url, config, headers, data)
  return HTTPI.put(request)
end

Private Class Methods

configure_httpi(config, httpi) click to toggle source

Configures HTTPI request according to the config provided.

# File lib/ads_common/http.rb, line 104
def self.configure_httpi(config, httpi)
  adapter = config.read('connection.adapter')
  HTTPI.adapter = adapter if adapter
  proxy = config.read('connection.proxy')
  httpi.proxy = proxy if proxy
  enable_gzip = config.read('connection.enable_gzip', false)
  httpi.gzip if enable_gzip
  logger = config.read('library.logger')
  if logger
    HTTPI.logger = logger
    HTTPI.log_level = :debug
  end
  httpi.read_timeout =
      config.read('connection.read_timeout', HTTP_READ_TIMEOUT)
  httpi.open_timeout =
      config.read('connection.open_timeout', HTTP_OPEN_TIMEOUT)
  strict_ssl =
      config.read('connection.strict_ssl_verification', true)
  httpi.auth.ssl.verify_mode = strict_ssl ? :peer : :none
  if logger && !strict_ssl
    logger.warn('HTTPS peer validation is disabled. This is NOT ' +
        'secure and NOT recommended.')
  end
end
prepare_request(url, config, headers = nil, data = nil) click to toggle source

Returns a suitably configured request object for a given URL and options. Defaulting to stricter :peer validation.

# File lib/ads_common/http.rb, line 94
def self.prepare_request(url, config, headers = nil, data = nil)
  request = HTTPI::Request.new(url)
  request.follow_redirect = true
  request.headers = headers if headers
  request.body = data if data
  configure_httpi(config, request)
  return request
end