Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy-to-use FTP library for Ruby?

Tags:

ruby

ftp

Is there a high-level Ruby library to interact with an FTP server?

Instead of Net::HTTP I can use HTTParty, Curb, Rest Client, or Typhoeus, which makes everything easier, but I can't find any similar solutions to replace/enhance Net::FTP.

More specifically, I'm looking for:

  • minimal lines to connect to a server. For example, login must be explicitly specified with Net::FTP
  • the ability to iterate through all entries in one folder, or using glob, or just recursively.
  • the ability to get all possible information, such as the type of entry, size, mtime without manually parsing returned lines.
like image 494
tig Avatar asked Feb 16 '11 13:02

tig


2 Answers

Ruby's built-in OpenURI will handle FTP.

From OpenURI's docs:

OpenURI is an easy-to-use wrapper for net/http, net/https and net/ftp.

This will seem to hang while it retrieves the Ruby source, but should return after a minute or two.

require 'open-uri'
open('ftp://ftp.ruby-lang.org//pub/ruby/ruby-1.9.2-p136.tar.bz2') do |fi|
  File.open('ruby-1.9.2-p136.tar.bz2', 'wb') do |fo|
    fo.puts fi.read
  end
end

Or Net::FTP is easy to use with a lot more functionality:

require 'net/ftp'

Net::FTP.open('ftp.ruby-lang.org') do |ftp|    
  ftp.login
  ftp.chdir('/pub/ruby')
  puts ftp.list('ruby-1.9.2*')
  puts ftp.nlst()

  ruby_file = 'ruby-1.9.2-p136.tar.bz2'
  ftp.getbinaryfile(ruby_file, ruby_file, 1024)
end
like image 160
the Tin Man Avatar answered Nov 05 '22 13:11

the Tin Man


Have you tried EventMachine? https://github.com/schleyfox/em-ftp-client

like image 4
Paulo Casaretto Avatar answered Nov 05 '22 14:11

Paulo Casaretto