Sha256: 5b14dcf1c73f256880913e2890a3e6400121e117da629f3014869f1426b962bd

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'uri'
require 'pathname'
require 'chrysalis/repository'
require 'chrysalis/ar'


module Chrysalis
module VCS
module HTTP

  # Implements support for downloading dependencies from a web server.
  #
  # Repositories of this type are identified by <em>http://</em> URLs.
  #
  # Example:
  #  - http://www.example.com/dist/libfoo-1.2.8.tar.gz
  
  class Repository < Chrysalis::Repository
    
    def self.retrieves?(url, params = {})
      uri = URI::parse(url)
      return (uri.scheme == 'http' || uri.scheme == 'https')
    end
    
    
    def initialize(url, params = {})
      @url = url
      @params = params
    end
    
    def retrieve(to = '.')
      path = Pathname.new(to).join(download_to)
      raise IOError, "Refusing to overwrite existing path. (path: #{path})" if path.exist?
    
      unless @params[:noop]
        puts ""
        puts "Downloading #{@url} ..."

        location = URI::parse(@url)
        Net::HTTP.start(location.host, location.port) do |http|
          ::File.open(path, 'w') do |f|
            http.get(location.path) do |stream|
              f.write stream
            end
          end
        end
      end
      
      extract_path = Archive.extract(path.to_s, to, @params)
      WorkingCopy.new(:url => @url,
                      :path => extract_path)
    end
    
    private
      def download_to
        uri = URI::parse(@url)
        file = uri.path.split('/')[-1]
        
        raise ParameterError, "Invalid URL. (URL: #{@url})" if file.nil?
        return file
      end
    
  end
  
end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chrysalis-0.1.0 lib/chrysalis/vcs/http/repository.rb