class HTTPWrapper

Constants

AJAX_HEADER
AJAX_HEADER_NAME
AJAX_JSON_HEADER
CONTENT_TYPE_HEADER_NAME
DEFAULT_CONTENT_TYPE
Error
JSON_CONTENT_TYPE
JSON_HEADER
KNOWN_OPTIONS_KEYS
MULTIPART_CONTENT_TYPE
POST_CONTENT_TYPE
TooManyRedirectsError
USER_AGENT
USER_AGENT_HEADER_NAME
UnknownKeyError
VERSION

Attributes

logger[RW]
max_redirects[RW]
timeout[RW]
user_agent[RW]
verify_cert[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/http_wrapper/http_wrapper.rb, line 10
def initialize(options = {})
  Util.validate_hash_keys options, KNOWN_OPTIONS_KEYS

  @timeout       = options.fetch(:timeout) { 10 }
  @verify_cert   = options.fetch(:verify_cert) { true }
  @logger        = options.fetch(:logger) { nil }
  @max_redirects = options.fetch(:max_redirects) { 10 }
  @user_agent    = options.fetch(:user_agent) { USER_AGENT }
end

Public Instance Methods

execute(request, uri) click to toggle source
# File lib/http_wrapper/http_wrapper.rb, line 43
def execute(request, uri)
  connection = create_connection uri
  connection.request request
end
post_and_get_cookie(url, params = {}) click to toggle source
# File lib/http_wrapper/http_wrapper.rb, line 38
def post_and_get_cookie(url, params = {})
  response = post url, params
  response['set-cookie']
end

Private Instance Methods

create_connection(uri) click to toggle source
# File lib/http_wrapper/http_wrapper.rb, line 67
def create_connection(uri)
  connection = Net::HTTP.new uri.host, uri.port
  connection.read_timeout = @timeout
  connection.open_timeout = @timeout

  if uri.is_a? URI::HTTPS
    connection.use_ssl = true
    connection.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @verify_cert
  end

  connection.set_debug_output(@logger)
  connection
end
get_response(request, redirects_limit = @max_redirects) click to toggle source
# File lib/http_wrapper/http_wrapper.rb, line 50
def get_response(request, redirects_limit = @max_redirects)
  raise TooManyRedirectsError, 'Too many redirects!' if redirects_limit == 0

  response = execute request.create, request.uri

  if response.is_a? Net::HTTPRedirection
    request.uri = response['location']
    response = get_response request, redirects_limit - 1
  end

  response
end
headers_specific_for(request_type) click to toggle source
# File lib/http_wrapper/http_wrapper.rb, line 63
def headers_specific_for(request_type)
  self.class.const_get "#{request_type.upcase}_HEADER"
end