Sha256: 600bfd434cc997b996f2e323a2ba79d1e52de76a8c6160dc1f08dc7a66b85228
Contents?: true
Size: 1.17 KB
Versions: 3
Compression:
Stored size: 1.17 KB
Contents
require "net/https" require "net/http" require "uri" require "timeout" module SocialAvatarProxy class TooManyRedirects < StandardError; end class TimeoutError < Timeout::Error; end class RemoteFileResolver def initialize(url, limit = 5) # timeout if we have no redirects left raise TooManyRedirects if limit <= 0 # store the limit and URL @url, @redirect_limit = url, limit end def resolve response = Timeout::timeout(30) do perform_request end # if this is a redirect if response.kind_of?(Net::HTTPRedirection) # follow the redirect resolver = self.class.new(response["location"], @redirect_limit - 1) resolver.resolve # if this is not a redirect else # return the response response end rescue Timeout::Error => e raise TimeoutError, e.message, e.backtrace end private def uri URI.parse(@url) end def perform_request http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == "https" request = Net::HTTP::Get.new(uri.request_uri) http.request(request) end end end
Version data entries
3 entries across 3 versions & 1 rubygems