Class: URI::HTTP
Overview
:nodoc:
Instance Method Summary collapse
-
#read(options = nil, &block) ⇒ Object
See URI::Generic#read.
Instance Method Details
#read(options = nil, &block) ⇒ Object
See URI::Generic#read
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/buildr/core/transports.rb', line 278 def read( = nil, &block) ||= {} connect do |http| trace "Requesting #{self}" headers = { 'If-Modified-Since' => CGI.rfc1123_date([:modified].utc) } if [: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. trace 'Not modified since last download' return nil when Net::HTTPRedirection # Try to download from the new URI, handle relative redirects. trace "Redirected to #{response['Location']}" return (self + URI.parse(response['location'])).read(, &block) when Net::HTTPOK info "Downloading #{self}" result = nil [:progress], path.split('/').last, response.content_length do |progress| if block response.read_body do |chunk| block.call chunk progress << chunk end else result = '' response.read_body do |chunk| result << chunk progress << chunk end end end return result when Net::HTTPNotFound raise NotFoundError, "Looking for #{self} and all I got was a 404!" else raise RuntimeError, "Failed to download #{self}: #{response.}" end end end end |