Class URI::FTP
In: lib/open-uri.rb
lib/uri/ftp.rb
Parent: Object

FTP URI syntax is defined by RFC1738 section 3.2.

Methods

build   check_typecode   new   new2   path   set_typecode   to_s   typecode=  

Included Modules

OpenURI::OpenRead

Constants

DEFAULT_PORT = 21
COMPONENT = [ :scheme, :userinfo, :host, :port, :path, :typecode
TYPECODE = ['a', 'i', 'd'].freeze   Typecode is "a", "i" or "d".
  • "a" indicates a text file (the FTP command was ASCII)
  • "i" indicates a binary file (FTP command IMAGE)
  • "d" indicates the contents of a directory should be displayed
TYPECODE_PREFIX = ';type='.freeze

Attributes

typecode  [R] 

Public Class methods

Description

Creates a new URI::FTP object from components, with syntax checking.

The components accepted are userinfo, host, port, path and typecode.

The components should be provided either as an Array, or as a Hash with keys formed by preceding the component names with a colon.

If an Array is used, the components must be passed in the order [userinfo, host, port, path, typecode]

If the path supplied is absolute, it will be escaped in order to make it absolute in the URI. Examples:

    require 'uri'

    uri = URI::FTP.build(['user:password', 'ftp.example.com', nil,
      '/path/file.> zip', 'i'])
    puts uri.to_s  ->  ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=a

    uri2 = URI::FTP.build({:host => 'ftp.example.com',
      :path => 'ruby/src'})
    puts uri2.to_s  ->  ftp://ftp.example.com/ruby/src

[Source]

     # File lib/uri/ftp.rb, line 78
 78:     def self.build(args)
 79: 
 80:       # Fix the incoming path to be generic URL syntax
 81:       # FTP path  ->  URL path
 82:       # foo/bar       /foo/bar
 83:       # /foo/bar      /%2Ffoo/bar
 84:       #
 85:       if args.kind_of?(Array)
 86:         args[3] = '/' + args[3].sub(/^\//, '%2F')
 87:       else
 88:         args[:path] = '/' + args[:path].sub(/^\//, '%2F')
 89:       end
 90: 
 91:       tmp = Util::make_components_hash(self, args)
 92: 
 93:       if tmp[:typecode]
 94:         if tmp[:typecode].size == 1
 95:           tmp[:typecode] = TYPECODE_PREFIX + tmp[:typecode] 
 96:         end
 97:         tmp[:path] << tmp[:typecode]
 98:       end
 99: 
100:       return super(tmp)
101:     end

Description

Creates a new URI::FTP object from generic URL components with no syntax checking.

Unlike build(), this method does not escape the path component as required by RFC1738; instead it is treated as per RFC2396.

Arguments are scheme, userinfo, host, port, registry, path, opaque, query and fragment, in that order.

[Source]

     # File lib/uri/ftp.rb, line 115
115:     def initialize(*arg)
116:       super(*arg)
117:       @typecode = nil
118:       tmp = @path.index(TYPECODE_PREFIX)
119:       if tmp
120:         typecode = @path[tmp + TYPECODE_PREFIX.size..-1]
121:         self.set_path(@path[0..tmp - 1])
122:         
123:         if arg[-1]
124:           self.typecode = typecode
125:         else
126:           self.set_typecode(typecode)
127:         end
128:       end
129:     end

[Source]

    # File lib/uri/ftp.rb, line 34
34:     def self.new2(user, password, host, port, path, 
35:                   typecode = nil, arg_check = true)
36:       typecode = nil if typecode.size == 0
37:       if typecode && !TYPECODE.include?(typecode)
38:         raise ArgumentError,
39:           "bad typecode is specified: #{typecode}"
40:       end
41: 
42:       # do escape
43: 
44:       self.new('ftp',
45:                [user, password], 
46:                host, port, nil, 
47:                typecode ? path + TYPECODE_PREFIX + typecode : path, 
48:                nil, nil, nil, arg_check)
49:     end

Public Instance methods

Returns the path from an FTP URI.

RFC 1738 specifically states that the path for an FTP URI does not include the / which separates the URI path from the URI host. Example:

    ftp://ftp.example.com/pub/ruby

The above URI indicates that the client should connect to ftp.example.com then cd pub/ruby from the initial login directory.

If you want to cd to an absolute directory, you must include an escaped / (%2F) in the path. Example:

    ftp://ftp.example.com/%2Fpub/ruby

This method will then return "/pub/ruby"

[Source]

     # File lib/uri/ftp.rb, line 179
179:     def path
180:       return @path.sub(/^\//,'').sub(/^%2F/i,'/')
181:     end

[Source]

     # File lib/uri/ftp.rb, line 183
183:     def to_s
184:       save_path = nil
185:       if @typecode
186:         save_path = @path
187:         @path = @path + TYPECODE_PREFIX + @typecode
188:       end
189:       str = super
190:       if @typecode
191:         @path = save_path
192:       end
193: 
194:       return str
195:     end

[Source]

     # File lib/uri/ftp.rb, line 147
147:     def typecode=(typecode)
148:       check_typecode(typecode)
149:       set_typecode(typecode)
150:       typecode
151:     end

Protected Instance methods

[Source]

     # File lib/uri/ftp.rb, line 142
142:     def set_typecode(v)
143:       @typecode = v
144:     end

Private Instance methods

[Source]

     # File lib/uri/ftp.rb, line 132
132:     def check_typecode(v)
133:       if TYPECODE.include?(v)
134:         return true
135:       else
136:         raise InvalidComponentError,
137:           "bad typecode(expected #{TYPECODE.join(', ')}): #{v}"
138:       end
139:     end

[Validate]