module MiGA::Common::Net

General web-access functions shared throughout MiGA.

Public Instance Methods

download_file_ftp(connection, file, target) { |transferred| ... } click to toggle source

Download a file via FTP using the connection (returned by .remote_connection) with remote name file into local target.

Alternatively, connection can simply be the host (String) or a recognized Symbol (see .remote_connection), in which case the function opens the connection automatically

Reports progress to the function block with two arguments: the currently transferred size and the total file size

# File lib/miga/common/net.rb, line 59
def download_file_ftp(connection, file, target)
  # Open connection unless passed
  close_conn = false
  if connection.is_a?(String) || connection.is_a?(Symbol)
    connection = remote_connection(connection)
    close_conn = true
  end

  # Prepare download
  FileUtils.mkdir_p(File.dirname(target))
  filesize = connection.size(file)
  transferred = 0

  # Get in chunks of 1KiB
  connection.getbinaryfile(file, target, 1024) do |data|
    yield(transferred += data.size, filesize) if block_given?
  end

  # Close connection if automatically opened
  connection.close if close_conn
end
known_hosts(name) click to toggle source

Returns the URL of the host name (Symbol)

# File lib/miga/common/net.rb, line 15
def known_hosts(name)
  case name.to_sym
  when :miga_online_ftp
    "ftp://#{main_server}//" # <- // to simplify chdir in connection
  when :miga_db
    "ftp://#{main_server}/db"
  when :miga_dist
    "ftp://#{main_server}/dist"
  else
    raise "Unrecognized server name: #{host}"
  end
end
main_server() click to toggle source

Returns the address of the main MiGA server

# File lib/miga/common/net.rb, line 30
def main_server
  'gatech.microbial-genomes.org'
end
remote_connection(host) click to toggle source

Connect to an FTP host (String) or a known host name (Symbol, see .known_hosts)

# File lib/miga/common/net.rb, line 37
def remote_connection(host)
  host = known_hosts(host) if host.is_a?(Symbol)
  uri = URI.parse(host)
  raise 'Only FTP hosts are currently supported' unless uri.scheme == 'ftp'

  ftp = Net::FTP.new(uri.host)
  ftp.passive = true
  ftp.login
  ftp.chdir(uri.path)
  ftp
end