Sha256: ea9287e1ba500041c7458b6edf78c59deef1b8bad2dde0c102a3d97f0041cc02

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

require 'net/http'

module Ogo
  module Utils
    class RedirectFollower
      REDIRECT_DEFAULT_LIMIT = 5

      class TooManyRedirects < StandardError; end
      class EmptyURLError < ArgumentError; end

      attr_accessor :url, :body, :charset, :redirect_limit, :response, :headers

      def initialize(url, options = {})
        raise EmptyURLError if url.to_s.empty?
        @redirect_limit = options[:limit] || REDIRECT_DEFAULT_LIMIT
        @headers = options[:headers] || {}
        @url = url.start_with?('http') ? url : "http://#{url}"
      end

      def resolve
        raise TooManyRedirects if redirect_limit < 0

        uri = Addressable::URI.parse(url).normalize

        http = Net::HTTP.new(uri.host, uri.inferred_port)
        if uri.scheme == 'https'
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        end

        self.response = http.request_get(uri.request_uri, headers)

        if response.kind_of?(Net::HTTPRedirection)
          self.url = redirect_url
          self.redirect_limit -= 1
          resolve
        end

        charset = nil
        if content_type = response['content-type']
          if content_type =~ /charset=(.+)/i
            charset = $1
          end
        end
        self.charset = charset

        self.body = response.body
        self
      end

      def redirect_url
        if response['location'].nil?
          response.body.match(/<a href=\"([^>]+)\">/i)[1]
        else
          response['location']
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ogo-0.1.1 lib/ogo/utils/redirect_follower.rb
ogo-0.0.2 lib/ogo/utils/redirect_follower.rb