Module: HTTP

Defined in:
lib/HTTP/VERSION.rb,
lib/HTTP/verbs.rb,
lib/HTTP/request.rb

Overview

HTTP/VERSION.rb HTTP::VERSION

Defined Under Namespace

Modules: VERBS

Constant Summary collapse

VERSION =
'0.18.0'

Class Method Summary collapse

Class Method Details

.request(uri, request_object, headers = {}, options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/HTTP/request.rb', line 14

def request(uri, request_object, headers = {}, options = {}, &block)
  uri = uri.is_a?(URI) ? uri : URI.parse(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  no_redirect = options.delete(:no_redirect)
  options[:use_ssl] ||= uri.use_ssl?
  options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
  http.options = options
  request_object.headers = headers
  request_object.basic_auth(uri.user, uri.password) if uri.user
  response = http.request(request_object)
  if response.code =~ /^3/
    if block_given? && no_redirect
      yield response
    elsif no_redirect
      return response
    end
    redirect_uri = URI.parse(response.header['location'])
    if redirect_uri.scheme
      response = get(response.header['location'], {}, {}, options, &block)
    else
      new_location = "http://#{uri.host}:#{uri.port}#{response.header['location']}"
      response = get(new_location, {}, {}, options, &block)
    end
  end
  if block_given?
    yield response
  else
    response
  end
end