### Home Classes Methods

### In Files

- lib/facets/net/http.rb

### Methods

- ::download
- ::download_and_save

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Random
- Random::ArrayExtensions
- Random::HashExtensions
- Random::IntegerExtensions
- Random::NumericExtensions
- Random::RangeExtensions
- Random::StringExtensions
- Random::StringExtensions::Self
- FileUtils
- FileUtils::DryRun
- FileUtils::NoWrite
- FileUtils::Verbose
- Digest
- Digest::Class
- Digest::Instance
- Multiton
- Multiton::Inclusive
- Multiton::MetaMethods
- URI
- URI::Hash
- URI::Kernel
- Argvector
- Argvector
- ERB
- ERB::OpenTemplate
- Enumerable
- Enumerable::Arguments
- GetoptLong
- GetoptLong::DSL
- Memoizable
- Memoizable::Copy
- Numeric
- Numeric::Multipliers
- Object
- Object::ArgVector
- Timer
- Timer::Dummy
- Array
- BasicObject
- CGI
- Cloneable
- Continuation
- Date
- DateTime
- Equitable
- File
- Find
- Hash
- Instantiable
- Integer
- Interval
- Kernel
- Math
- Module
- Net
- NilClass
- NullClass
- OpEsc
- OpenStruct
- Pathname
- Platform
- Range
- RbConfig
- Set
- Shellwords
- String
- StringScanner
- Time
- Tuple
- YAML
- Zlib

No matching classes.

# Net

### Public Class Methods

download(url, limit=10) click to toggle source

CREDIT: Daniel Huckstep

    # File lib/facets/net/http.rb, line 6
    def self.download(url, limit=10)
      raise ArgumentError, 'HTTP redirect too deep' if limit == 0
      #url = URI.parse(url)
      #req = Net::HTTP::Get.new(url.path)
      #req['User-Agent'] = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030422 Ubuntu/8.10 (intrepid) Firefox/3.0.7"
      #resp = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
      resp = Net::HTTP.get_response(URI.parse(url))
      case resp
      when Net::HTTPSuccess     then resp
      when Net::HTTPRedirection then download(resp['location'], limit - 1)
      else resp.error!
      end
    end

download_and_save(url, path=nil) click to toggle source

CREDIT: Daniel Huckstep

    # File lib/facets/net/http.rb, line 21
    def self.download_and_save(url, path=nil)
      if path.nil?
        path = File.expand_path(url.split('/').last)
      else
        path = File.expand_path(path)
      end
      raise ArgumentError.new('Save path is a directory') if File.directory?(path)
      resp = download(url)
      open(path,'w') { |file| file.write(resp.body) } if resp.is_a?(Net::HTTPSuccess)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.