From: Mark Hahn Date: 2002-01-07T15:24:56+09:00 Subject: RE: net_require This is a multi-part message in MIME format. ------=_NextPart_000_0006_01C19700.F68D3CD0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit > the following code could be used to try requiring a file Do you have a test case for this? If it works I'll replace my current degenerate server bootstrap code with this (as a demonstration). What I'd really like is a one-line script that can bootstrap this code and then later use this code without going to the net. -----Original Message----- From: Jack Dempsey [mailto:dabigdemp@hotmail.com] Sent: Sunday, January 06, 2002 9:38 PM To: ruby-talk ML Subject: [ruby-talk:30440] net_require To Mark and others interested: the following code could be used to try requiring a file and if its not able to do so, go to a repository site and download it...that's all it does currently, but with security issues being discussed along the way, i think it might prove useful to those who'd care to use it: #!/usr/bin/ruby require 'net/http' def net_require(file) begin require file rescue LoadError net_download(file) #could now require the file here end end def net_download(file) site = 'www.ruby-lang.org' #file << '.rb' if !(file =~ /.rb/) begin h = Net::HTTP.new(site,80) resp, data = h.get(file) filename = file.split('/').pop puts filename File.open(filename,'w') do |line| line.puts data end rescue => err puts "Error: #{err}" exit end end if $0 == __FILE__ net_require '/en/index.html' end ------=_NextPart_000_0006_01C19700.F68D3CD0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
>=20 the following code could be used to try requiring = a file=20
 
Do you=20 have a test case for this?  If it works I'll replace my current = degenerate=20 server bootstrap code with this (as a = demonstration).
 
What=20 I'd really like is a one-line script that can bootstrap this code and = then later=20 use this code without going to the net.
-----Original Message-----
From: Jack Dempsey=20 [mailto:dabigdemp@hotmail.com]
Sent: Sunday, January 06, = 2002 9:38=20 PM
To: ruby-talk ML
Subject: [ruby-talk:30440]=20 net_require

To Mark and others interested: the = following code=20 could be used to try requiring a file and if its not able to do so, go = to a=20 repository site and download it...that's all it does currently, but = with=20 security issues being discussed along the way, i think it might prove = useful=20 to those who'd care to use it:
 
#!/usr/bin/ruby
 
require 'net/http'
 
def net_require(file)
   = begin
    require file
   rescue=20 LoadError
    = net_download(file)
   =20 #could now require the file here
   = end
end
 
def net_download(file)
  site = =3D=20 'www.ruby-lang.org'
  #file << '.rb' if !(file =3D~ = /.rb/)=20
  begin
    h =3D=20 Net::HTTP.new(site,80)
    resp, data =3D=20 h.get(file)
    filename =3D=20 file.split('/').pop
    puts = filename
   =20 File.open(filename,'w') do |line|
      = line.puts=20 data
    end
  rescue =3D>=20 err
    puts "Error: #{err}"
   =20 exit
  end  
end
 
if $0 =3D=3D __FILE__
  = net_require=20 '/en/index.html'
end
------=_NextPart_000_0006_01C19700.F68D3CD0--