Class: HTTPWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/http_wrapper/util.rb,
lib/http_wrapper/errors.rb,
lib/http_wrapper/request.rb,
lib/http_wrapper/version.rb,
lib/http_wrapper/constants.rb,
lib/http_wrapper/http_wrapper.rb

Defined Under Namespace

Modules: Util Classes: Request

Constant Summary collapse

Error =
Class.new StandardError
TooManyRedirectsError =
Class.new Error
UnknownKeyError =
Class.new Error
VERSION =
'4.0.0'
USER_AGENT =
"HTTPWrapper/#{HTTPWrapper::VERSION}; Ruby/#{RUBY_VERSION}"
CONTENT_TYPE_HEADER_NAME =
'content-type'
USER_AGENT_HEADER_NAME =
'user-agent'
'cookie'
AJAX_HEADER_NAME =
'x-requested-with'
DEFAULT_CONTENT_TYPE =
'text/html; charset=UTF-8'
JSON_CONTENT_TYPE =
'application/json; charset=UTF-8'
POST_CONTENT_TYPE =
'application/x-www-form-urlencoded'
MULTIPART_CONTENT_TYPE =
'multipart/form-data'
AJAX_HEADER =
{ AJAX_HEADER_NAME => 'XMLHttpRequest' }.freeze
JSON_HEADER =
{ CONTENT_TYPE_HEADER_NAME => JSON_CONTENT_TYPE }.freeze
AJAX_JSON_HEADER =
AJAX_HEADER.dup.merge!(JSON_HEADER).freeze
KNOWN_OPTIONS_KEYS =
%i[timeout verify_cert logger max_redirects user_agent].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HTTPWrapper

Returns a new instance of HTTPWrapper.



10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/http_wrapper/http_wrapper.rb', line 8

def logger
  @logger
end

#max_redirectsObject

Returns the value of attribute max_redirects.



8
9
10
# File 'lib/http_wrapper/http_wrapper.rb', line 8

def max_redirects
  @max_redirects
end

#timeoutObject

Returns the value of attribute timeout.



8
9
10
# File 'lib/http_wrapper/http_wrapper.rb', line 8

def timeout
  @timeout
end

#user_agentObject

Returns the value of attribute user_agent.



8
9
10
# File 'lib/http_wrapper/http_wrapper.rb', line 8

def user_agent
  @user_agent
end

#verify_certObject

Returns the value of attribute verify_cert.



8
9
10
# File 'lib/http_wrapper/http_wrapper.rb', line 8

def verify_cert
  @verify_cert
end

Instance Method Details

#execute(request, uri) ⇒ Object



43
44
45
46
# 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 = {}) ⇒ Object



38
39
40
41
# File 'lib/http_wrapper/http_wrapper.rb', line 38

def post_and_get_cookie(url, params = {})
  response = post url, params
  response['set-cookie']
end