lib/uri/ftp.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  # $Id: ftp.rb,v 1.1 2002/01/10 08:00:51 akira Exp $
   3  #
   4  # Copyright (c) 2001 akira yamada <akira@ruby-lang.org>
   5  # You can redistribute it and/or modify it under the same term as Ruby.
   6  #
   7  
   8  require 'uri/generic'
   9  
  10  module URI
  11  
  12  =begin
  13  
  14  == URI::FTP
  15  
  16  === Super Class
  17  
  18  ((<URI::Generic>))
  19  
  20  =end
  21  
  22    # RFC1738 section 3.2.
  23    class FTP < Generic
  24      DEFAULT_PORT = 21
  25  
  26      COMPONENT = [
  27        :scheme, 
  28        :userinfo, :host, :port,
  29        :path, :typecode
  30      ].freeze
  31  
  32      TYPECODE = ['a', 'i', 'd'].freeze
  33      TYPECODE_PREFIX = ';type='.freeze
  34  
  35  =begin
  36  
  37  === Class Methods
  38  
  39  --- URI::FTP::build
  40      Create a new URI::FTP object from components of URI::FTP with
  41      check.  It is scheme, userinfo, host, port, path and typecode. It
  42      provided by an Array or a Hash. typecode is "a", "i" or "d".
  43  
  44  --- URI::FTP::new
  45      Create a new URI::FTP object from ``generic'' components with no
  46      check.
  47  
  48  =end
  49  
  50      def self.new2(user, password, host, port, path, 
  51                    typecode = nil, arg_check = true)
  52        typecode = nil if typecode.size == 0
  53        if typecode && !TYPECODE.include?(typecode)
  54          raise ArgumentError,
  55            "bad typecode is specified: #{typecode}"
  56        end
  57  
  58        # do escape
  59  
  60        self.new('ftp',
  61                 [user, password], 
  62                 host, port, nil, 
  63                 typecode ? path + TYPECODE_PREFIX + typecode : path, 
  64                 nil, nil, nil, arg_check)
  65      end
  66  
  67      def self.build(args)
  68        tmp = Util::make_components_hash(self, args)
  69  
  70        if tmp[:typecode]
  71          if tmp[:typecode].size == 1
  72            tmp[:typecode] = TYPECODE_PREFIX + tmp[:typecode] 
  73          end
  74          tmp[:path] << tmp[:typecode]
  75        end
  76  
  77        return super(tmp)
  78      end
  79  
  80      def initialize(*arg)
  81        super(*arg)
  82        @typecode = nil
  83        tmp = @path.index(TYPECODE_PREFIX)
  84        if tmp
  85          typecode = @path[tmp + TYPECODE_PREFIX.size..-1]
  86          self.set_path(@path[0..tmp - 1])
  87          
  88          if arg[-1]
  89            self.typecode = typecode
  90          else
  91            self.set_typecode(typecode)
  92          end
  93        end
  94      end
  95      attr_reader :typecode
  96  
  97      #
  98      # methods for typecode
  99      #
 100  
 101      def check_typecode(v)
 102        if TYPECODE.include?(v)
 103          return true
 104        else
 105          raise InvalidComponentError,
 106            "bad typecode(expected #{TYPECODE.join(', ')}): #{v}"
 107        end
 108      end
 109      private :check_typecode
 110  
 111      def set_typecode(v)
 112        @typecode = v
 113      end
 114      protected :set_typecode
 115  
 116      def typecode=(typecode)
 117        check_typecode(typecode)
 118        set_typecode(typecode)
 119      end
 120  
 121  =begin
 122  =end
 123      def merge(oth)
 124        tmp = super(oth)
 125        if self != tmp
 126          tmp.set_typecode(oth.typecode)
 127        end
 128  
 129        return tmp
 130      end
 131  
 132  =begin
 133  =end
 134      def to_str
 135        save_path = nil
 136        if @typecode
 137          save_path = @path
 138          @path = @path + TYPECODE_PREFIX + @typecode
 139        end
 140        str = super
 141        if @typecode
 142          @path = save_path
 143        end
 144  
 145        return str
 146      end
 147    end # FTP
 148    @@schemes['FTP'] = FTP
 149  end # URI