Sha256: 8190f57d2281284805f5727239b5ff9d96fd1825c43f6d4972a833c0ca17afc6

Contents?: true

Size: 707 Bytes

Versions: 6

Compression:

Stored size: 707 Bytes

Contents

require 'net/http/persistent'

module LinkThumbnailer

  class Fetcher

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

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

      if uri.is_a?(URI::HTTP)
        http = Net::HTTP::Persistent.new('linkthumbnailer')
        http.headers['User-Agent'] = 'linkthumbnailer'
        resp = http.request(uri)
        case resp
          when Net::HTTPSuccess; resp.body
          when Net::HTTPRedirection; fetch(resp['location'], redirect_count + 1)
          else resp.error!
        end
      end
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
link_thumbnailer-1.0.2 lib/link_thumbnailer/fetcher.rb
link_thumbnailer-1.0.0 lib/link_thumbnailer/fetcher.rb
link_thumbnailer-0.0.6 lib/link_thumbnailer/fetcher.rb
link_thumbnailer-0.0.5 lib/link_thumbnailer/fetcher.rb
link_thumbnailer-0.0.4 lib/link_thumbnailer/fetcher.rb
link_thumbnailer-0.0.3 lib/link_thumbnailer/fetcher.rb