Sha256: 9fece8a5a8d7f76db2a8de66d57cafdbfb38a9c029ac7ce3ffeda839fac3dac7

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

require 'net/http/persistent'

module LinkThumbnailer

  class Fetcher

    attr_accessor :url

    def fetch(url, redirect_count = 0)
      if redirect_count > LinkThumbnailer.configuration.redirect_limit
        raise ArgumentError, "too many redirects (#{redirect_count})"
      end

      self.url = url.is_a?(URI) ? url : URI(url)

      if self.url.is_a?(URI::HTTP)
        http                        = Net::HTTP::Persistent.new('linkthumbnailer')
        http.headers['User-Agent']  = LinkThumbnailer.configuration.user_agent
        http.verify_mode            = OpenSSL::SSL::VERIFY_NONE unless LinkThumbnailer.configuration.verify_ssl
        http.open_timeout           = LinkThumbnailer.configuration.http_timeout
        resp                        = http.request(self.url)
        case resp
        when Net::HTTPSuccess     then resp.body
        when Net::HTTPRedirection
          location = resp['location'].start_with?('http') ? resp['location'] : "#{self.url.scheme}://#{self.url.host}#{resp['location']}"
          fetch(location, redirect_count + 1)
        else resp.error!
        end
      end
    end

  end

end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
link_thumbnailer-1.1.1 lib/link_thumbnailer/fetcher.rb
link_thumbnailer-1.1.0 lib/link_thumbnailer/fetcher.rb
rcarvalho-link_thumbnailer-1.0.9.1 lib/link_thumbnailer/fetcher.rb
link_thumbnailer-1.0.9 lib/link_thumbnailer/fetcher.rb