Module: MiGA::Common::Net
- Included in:
- MiGA
- Defined in:
- lib/miga/common/net.rb
Overview
General web-access functions shared throughout MiGA.
Instance Method Summary collapse
-
#download_file_ftp(connection, file, target) ⇒ Object
Download a file via FTP using the
connection(returned by.remote_connection) with remote namefileinto localtarget. -
#known_hosts(name) ⇒ Object
Returns the URL of the host
name(Symbol). -
#main_server ⇒ Object
Returns the address of the main MiGA server.
-
#remote_connection(host) ⇒ Object
Connect to an FTP
host(String) or a known host name (Symbol, see.known_hosts).
Instance Method Details
#download_file_ftp(connection, file, target) ⇒ Object
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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) ⇒ Object
Returns the URL of the host name (Symbol)
15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 ⇒ Object
Returns the address of the main MiGA server
30 31 32 |
# File 'lib/miga/common/net.rb', line 30 def main_server 'gatech.microbial-genomes.org' end |
#remote_connection(host) ⇒ Object
Connect to an FTP host (String) or a known host name (Symbol, see .known_hosts)
37 38 39 40 41 42 43 44 45 46 47 |
# 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 |