Sha256: 862e16d96cd06603d3a850b8d67bbbc38248fecad4ad93d62c1087d451b1aec4

Contents?: true

Size: 763 Bytes

Versions: 4

Compression:

Stored size: 763 Bytes

Contents

require 'net/http'

# The Http module defines a 
# 
#   Http.get(url)
#
# method.
module Http
  extend self

  # the default expiration time for get requests.
  attr :max_age
  
  def get(url, max_age = self.max_age)
    App.logger.benchmark("[GET] #{url}", :minimum => 20) do 
      App.cached(url, max_age) do get_(url) end
    end
  end

  private
  
  def get_(uri_str, limit = 10)
    raise 'too many redirections' if limit == 0

    response = Net::HTTP.get_response(URI(uri_str))

    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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
radiospiel-app-0.2.2 lib/extensions/http.rb
radiospiel-app-0.2.1 lib/extensions/http.rb
radiospiel-app-0.2.0 lib/extensions/http.rb
radiospiel-app-0.1.1 lib/extensions/http.rb