Module: URI

Defined in:
lib/epitools/core_ext/uri.rb

Instance Method Summary collapse

Instance Method Details

#get(headers = {}, redirect_limit = 10) ⇒ Object

Get this URI using Net::HTTP



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/epitools/core_ext/uri.rb', line 64

def get(headers={}, redirect_limit=10)
  raise "Sorry, URI can't get from #{scheme.inspect} URIs yet" unless scheme =~ /^https?$/
  raise 'Too many HTTP redirections' if redirect_limit == 0

  # headers['User-Agent'] ||= USER_AGENT

  # response = Net::HTTP.start(host, port) do |http|
  #   # the_path = path.empty? ? "/" : path
  #   req = Net::HTTP::Get.new(self, headers)
  #   http.request(req)
  # end

  response = Net::HTTP.get_response(self)

  case response
  when Net::HTTPSuccess
    response
  when Net::HTTPRedirection
    # puts "redirect: #{response['location']}"
    URI(response['location']).get(headers, redirect_limit-1)
  else
    response.error!
  end
end