class SocialAvatarProxy::RemoteFileResolver

Public Class Methods

new(url, limit = 5) click to toggle source
# File lib/social_avatar_proxy/remote_file_resolver.rb, line 10
def initialize(url, limit = 5)
  # timeout if we have no redirects left
  raise TooManyRedirectsError if limit <= 0
  # store the limit and URL
  @url, @redirect_limit = url, limit
end

Public Instance Methods

resolve() click to toggle source
# File lib/social_avatar_proxy/remote_file_resolver.rb, line 17
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 Instance Methods

perform_request() click to toggle source
# File lib/social_avatar_proxy/remote_file_resolver.rb, line 41
def perform_request
  http = Net::HTTP.new(uri.host, uri.port)
  # enable SSL without verification if on HTTPS
  if uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  # create the request
  request = Net::HTTP::Get.new(uri.request_uri)
  # run the request
  http.request(request)
end
uri() click to toggle source
# File lib/social_avatar_proxy/remote_file_resolver.rb, line 37
def uri
  URI.parse(@url)
end