Sha256: ee6c89d18bae589992068308ea2bf869b96bd0b577b25ce04df25e5469c5dd08

Contents?: true

Size: 1006 Bytes

Versions: 5

Compression:

Stored size: 1006 Bytes

Contents

require 'net/http'

module Anemone
  class HTTP < Net::HTTP
    # Maximum number of redirects to follow on each get_response
    REDIRECTION_LIMIT = 5
    
    #
    # Retrieve an HTTP response for *url*, following redirects.
    # Returns the response object, response code, and final URI location.
    # 
    def self.get(url)      
      response = get_response(url)
      code = Integer(response.code)
      loc = url
      
      limit = REDIRECTION_LIMIT
      while response.is_a?(Net::HTTPRedirection) and limit > 0
          loc = URI(response['location'])
          loc = url.merge(loc) if loc.relative?
          response = get_response(loc)
          limit -= 1
      end

      return response, code, loc
    end
    
    #
    # Get an HTTPResponse for *url*, sending the appropriate User-Agent string
    #
    def self.get_response(url)
      Net::HTTP.start(url.host, url.port) do |http|
        return http.get(url.path, {'User-Agent' => Anemone::USER_AGENT })
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
anemone-0.0.5 lib/anemone/http.rb
anemone-0.0.6 lib/anemone/http.rb
anemone-0.1.0 lib/anemone/http.rb
anemone-0.0.3 lib/anemone/http.rb
anemone-0.0.4 lib/anemone/http.rb