Class: HTTPWrapper

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

Constant Summary collapse

HTTPWrapperError =
Class.new StandardError
TooManyRedirectsError =
Class.new HTTPWrapperError
UnknownParameterError =
Class.new HTTPWrapperError
VERSION =
'2.0.0'.freeze
KNOWN_OPTIONS_KEYS =
[:timeout, :ca_file, :validate_ssl_cert].freeze
KNOWN_PARAMS_KEYS =
[:headers, :params, :cookie, :auth, :body, :method].freeze
HEADER_CONTENT_TYPE =
'Content-Type'.freeze
DEFAULT_CONTENT_TYPE =
'text/xml; charset=UTF-8'.freeze
JSON_CONTENT_TYPE =
'application/json; charset=UTF-8'.freeze
POST_CONTENT_TYPE =
'application/x-www-form-urlencoded; charset=UTF-8'.freeze
HEADER_USER_AGENT =
'User-Agent'.freeze
'Cookie'.freeze
HEADER_AJAX =
'X-Requested-With'.freeze
HEADER_AJAX_VALUE =
'XMLHttpRequest'.freeze

Instance Attribute Summary collapse

Class Method 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
19
20
# File 'lib/http_wrapper/http_wrapper.rb', line 10

def initialize(options = {})
  unknown_options = options.keys - KNOWN_OPTIONS_KEYS

  if unknown_options.length > 0
    raise UnknownParameterError.new "Unknown options: #{unknown_options.join(', ')}"
  end

  @timeout           = options.fetch(:timeout) { 10000 }
  @ca_file           = options.fetch(:ca_file) { nil }
  @validate_ssl_cert = options.fetch(:validate_ssl_cert) { false }
end

Instance Attribute Details

#ca_fileObject

Returns the value of attribute ca_file.



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

def ca_file
  @ca_file
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#validate_ssl_certObject

Returns the value of attribute validate_ssl_cert.



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

def validate_ssl_cert
  @validate_ssl_cert
end

Class Method Details

.default_user_agentObject



22
23
24
# File 'lib/http_wrapper/http_wrapper.rb', line 22

def self.default_user_agent
  "HTTPWrapper/#{VERSION}; Ruby/#{RUBY_VERSION}"
end

Instance Method Details

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



26
27
28
29
# File 'lib/http_wrapper/http_wrapper.rb', line 26

def get(url, params = {})
  params = validate_parameters_and_init_headers params
  locate_response url, params
end

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



54
55
56
57
58
# File 'lib/http_wrapper/http_wrapper.rb', line 54

def get_ajax(url, params = {})
  params = validate_parameters_and_init_headers params
  params[:headers][HEADER_AJAX] = HEADER_AJAX_VALUE
  locate_response url, params
end

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



80
81
82
83
84
# File 'lib/http_wrapper/http_wrapper.rb', line 80

def get_ajax_json(url, params = {})
  params = validate_parameters_and_init_headers params
  params[:headers][HEADER_CONTENT_TYPE] = JSON_CONTENT_TYPE
  get_ajax url, params
end

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



67
68
69
70
71
# File 'lib/http_wrapper/http_wrapper.rb', line 67

def get_json(url, params = {})
  params = validate_parameters_and_init_headers params
  params[:headers][HEADER_CONTENT_TYPE] = JSON_CONTENT_TYPE
  locate_response url, params
end

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



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

def get_soap(url, params = {})
  params = validate_parameters_and_init_headers params
  params[:headers]['SOAPAction'] ||= ''
  params[:headers][HEADER_CONTENT_TYPE] ||= DEFAULT_CONTENT_TYPE
  locate_response url, params
end

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



97
98
99
100
# File 'lib/http_wrapper/http_wrapper.rb', line 97

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