Class: URI::HTTP

Inherits:
Object show all
Defined in:
lib/pik/contrib/uri_ext.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#read(options = nil, &block) ⇒ Object

See URI::Generic#read



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/pik/contrib/uri_ext.rb', line 238

def read(options = nil, &block)
  options ||= {}
  connect do |http|
    puts "Requesting #{self}" if verbose
    headers = { 'If-Modified-Since' => CGI.rfc1123_date(options[:modified].utc) } if options[:modified]
    request = Net::HTTP::Get.new(request_uri.empty? ? '/' : request_uri, headers)
    request.basic_auth self.user, self.password if self.user
    http.request request do |response|
      case response
      when Net::HTTPNotModified
        # No modification, nothing to do.
        puts 'Not modified since last download' if verbose
        return nil
      when Net::HTTPRedirection
        # Try to download from the new URI, handle relative redirects.
        puts "Redirected to #{response['Location']}" if verbose
        return (self + URI.parse(response['location'])).read(options, &block)
      when Net::HTTPOK
        puts "Downloading #{self}" if verbose
        result = nil
        with_progress_bar options[:progress], path.split('/').last, response.content_length do |progress|
          if block
            response.read_body do |chunk|
              block.call chunk
              progress << chunk
            end
            return true
          else
            result = ''
            response.read_body do |chunk|
              result << chunk
              progress << chunk
            end
            return result
          end
        end
      when Net::HTTPNotFound
        raise NotFoundError, "Looking for #{self} and all I got was a 404!"
      else
        raise RuntimeError, "Failed to download #{self}: #{response.message}"
      end
    end
  end
end

#verboseObject



233
234
235
# File 'lib/pik/contrib/uri_ext.rb', line 233

def verbose
  false
end