Class: HTTPWrapper

Inherits:
Object
  • Object
show all
Defined in:
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: CONTENT_TYPE, HEADER Classes: Request

Constant Summary collapse

HTTPWrapperError =
Class.new StandardError
TooManyRedirectsError =
Class.new HTTPWrapperError
UnknownParameterError =
Class.new HTTPWrapperError
VERSION =
'2.1.0'.freeze
USER_AGENT =
"HTTPWrapper/#{HTTPWrapper::VERSION}; Ruby/#{RUBY_VERSION}".freeze
AJAX_HEADER =
{ HEADER::AJAX => 'XMLHttpRequest'.freeze }.freeze
JSON_HEADER =
{ HEADER::CONTENT_TYPE => CONTENT_TYPE::JSON }.freeze
AJAX_JSON_HEADER =
AJAX_HEADER.dup.merge!(JSON_HEADER).freeze
KNOWN_OPTIONS_KEYS =
[: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.



8
9
10
11
12
13
14
15
16
# File 'lib/http_wrapper/http_wrapper.rb', line 8

def initialize(options = {})
  validate_options options

  @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.



6
7
8
# File 'lib/http_wrapper/http_wrapper.rb', line 6

def logger
  @logger
end

#max_redirectsObject

Returns the value of attribute max_redirects.



6
7
8
# File 'lib/http_wrapper/http_wrapper.rb', line 6

def max_redirects
  @max_redirects
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/http_wrapper/http_wrapper.rb', line 6

def timeout
  @timeout
end

#user_agentObject

Returns the value of attribute user_agent.



6
7
8
# File 'lib/http_wrapper/http_wrapper.rb', line 6

def user_agent
  @user_agent
end

#verify_certObject

Returns the value of attribute verify_cert.



6
7
8
# File 'lib/http_wrapper/http_wrapper.rb', line 6

def verify_cert
  @verify_cert
end

Instance Method Details

#execute(request, uri) ⇒ Object



40
41
42
43
# File 'lib/http_wrapper/http_wrapper.rb', line 40

def execute(request, uri)
  connection = create_connection uri
  connection.request request
end

#post_and_get_cookie(url, params = {}) ⇒ Object



35
36
37
38
# File 'lib/http_wrapper/http_wrapper.rb', line 35

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