"kwatch" <kwatch / lycos.jp> wrote in message
news:cf674456.0206242322.638840b5 / posting.google.com...
>
> I want to do ...
> .1) connect http server and get a html file

I like PHP's "URL fopen wrapper" feature.
Here is my Ruby draft implementation:
---------------------------------------------

class RFile
  require 'net/ftp'
  require 'net/http'
  require 'uri'
  require 'tempfile'

  def initialize(uri,net)
    @uri = uri
    @net = net
  end

  def RFile.open(fileName,aMode="r",aPerm=nil,&block)
    uri = URI.parse(fileName)
    case uri.scheme
    when nil
      aFile = aPerm ? File.new(fileName ,aMode,aPerm) :
File.new(fileName,aMode)
      if block_given?
        yield aFile
        aFile.close
        aFile = nil
      end
      aFile
    when 'http'
      Net::HTTP.version_1_1
      new(uri,Net::HTTP.new(uri.host,uri.port))
    when 'ftp'
      user,pass = uri.userinfo.split(':') if uri.userinfo
      new(uri,Net::FTP.new(uri.host,user,pass))
    end
  end

  def read
    case @uri.scheme
    when 'http'
      @net.get(@uri.path)[1]
    when 'ftp'
      dir,file = File.split(@uri.path)
      @net.chdir(dir[1..-1]) if dir!='/'
      data = ''
      @net.retrbinary("RETR #{file}", 1024){|d| data += d}
      data
    end
  end

  def each(aSepString=$/, &block)
    read.split(aSepString).each(&block)
  end

  def write(data)
    case @uri.scheme
    when 'ftp'
      dir,file = File.split(@uri.path)
      @net.chdir(dir[1..-1]) if dir!='/'
      f = Tempfile.new("tmp")
      f.write(data)
      f.close
      @net.putbinaryfile( f.path, file, 1024 )
      f.close(true)
      data.length
    end
  end

  def close
    @net.close if @uri.schme=='ftp'
  end

end
-------------------------------------------------

usage:

RFile:open("/home/path/file.txt", "r").each {|x| puts x}
RFile:open("http://www.example.com/index.htm", "r").each {|x| puts x}
RFile:open("ftp://user:password / example.com/file", "r").read
RFile:open("ftp://user:password / example.com/file", "w").write("test")


Park Heesob.