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 http:// 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