Sha256: 09388288bf4418dd0e05445a40a21e29206f7a0b6923c2bfa26a95c3d8fb9c14

Contents?: true

Size: 787 Bytes

Versions: 1

Compression:

Stored size: 787 Bytes

Contents

require "net/http"
require "longurl/url"
require "longurl/exceptions"

module LongURL
  module Direct
    # Will follow redirections given url <tt>orig</tt>.
    # === Exceptions
    # * LongURL::NetworkError in case of a network error (timeout, socket error, ...)
    # * LongURL::InvalidURL in case of a bad url (nil, empty, not http scheme ...)
    def self.follow_redirections(orig)
      uri = LongURL::URL.check(orig)
      Net::HTTP.start(uri.host, uri.port) do |http|
        answer = http.get(uri.path.empty? ? '/' : uri.path)
        dest = answer['Location']
        (dest && dest[0, 7] == 'http://' && follow_redirections(dest)) || orig
      end
    rescue Timeout::Error, Errno::ENETUNREACH, Errno::ETIMEDOUT, SocketError
      raise LongURL::NetworkError
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
longurl-0.1.0 lib/longurl/direct.rb