module Http

The Http module defines a

Http.get(url)

method.

Public Instance Methods

get(url, max_age = MaxAge.for(url)) click to toggle source
# File lib/extensions/http.rb, line 25
def get(url, max_age = MaxAge.for(url))
  App.logger.benchmark("[GET] #{url}", :minimum => 20) do 
    App.cached(url, max_age) do 
      App.logger.debug "[GET] #{url}"
      get_(url) 
    end
  end
end

Private Instance Methods

get_(uri_str, limit = 10) click to toggle source
# File lib/extensions/http.rb, line 36
def get_(uri_str, limit = 10)
  raise 'too many redirections' if limit == 0

  uri = URI.parse(uri_str)
  
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  case response
  when Net::HTTPSuccess then
    response.body
  when Net::HTTPRedirection then
    location = response['location']
    App.logger.debug "redirected to #{location}"
    get_(location, limit - 1)
  else  
    response.value
  end
end