Class: XFTP::Session::FTP Private

Inherits:
Base
  • Object
show all
Defined in:
lib/xftp/session/ftp.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

FTP session adapter

Instance Attribute Summary

Attributes inherited from Base

#credentials, #settings, #uri

Instance Method Summary collapse

Methods inherited from Base

#start

Methods included from Helpers::Logging

#log

Methods included from DSL::BlockEvaluator

#evaluate

Constructor Details

#initialize(uri, settings = {}) ⇒ FTP

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates an FTP session adapter instance

Parameters:

  • uri (URI)

    the remote uri

  • settings (Hash) (defaults to: {})

    the adapter connection settings



15
16
17
18
19
20
21
22
23
24
# File 'lib/xftp/session/ftp.rb', line 15

def initialize(uri, settings = {})
  super

  @ftp = Net::FTP.new
  @port = settings.delete(:port) || uri.port || Net::FTP::FTP_PORT
  @credentials[:login] ||= 'anonymous'

  options = XFTP.config.ftp.deep_merge(@settings)
  options.each { |key, val| @ftp.public_send("#{key}=", val) }
end

Instance Method Details

#chdir(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Changes the current (remote) working directory

Parameters:

  • path (String)

    the relative (remote) path



28
29
30
31
# File 'lib/xftp/session/ftp.rb', line 28

def chdir(path)
  ensure_relative_path! :chdir, path
  @ftp.chdir path
end

#directory?(path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

a directory on the remote host

Returns:

  • (Boolean)

    ‘true` if the argument refers to



58
59
60
61
62
63
64
65
# File 'lib/xftp/session/ftp.rb', line 58

def directory?(path)
  ensure_relative_path! :directory?, path
  chdir path
  chdir '..'
  true
rescue
  false
end

#download(from, to: File.basename(from), block_size: Net::FTP::DEFAULT_BLOCKSIZE) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initiates a download from remote to local, synchronously

Parameters:

  • from (String)

    the source remote file name

  • to (String) (defaults to: File.basename(from))

    the target local file name

  • block_size (Integer) (defaults to: Net::FTP::DEFAULT_BLOCKSIZE)

    the size of file chunk

See Also:

  • Net::FTP#get


112
113
114
115
# File 'lib/xftp/session/ftp.rb', line 112

def download(from, to: File.basename(from), block_size: Net::FTP::DEFAULT_BLOCKSIZE)
  log "downloading file from #{from} to #{to}..."
  @ftp.get(from, to, block_size)
end

#each_fileObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the block once for each entry in the current directory on the remote server and yields a filename to the block



83
84
85
# File 'lib/xftp/session/ftp.rb', line 83

def each_file
  files.each { |filename| yield filename }
end

#each_ioObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the block once for each entry in the current directory on the remote server and yields a filename and ‘StringIO` object to the block



89
90
91
92
93
94
# File 'lib/xftp/session/ftp.rb', line 89

def each_io
  each_file do |filename|
    io = get filename
    yield filename, io
  end
end

#entries(pattern = nil) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an array of entries (including directories) in the remote directory.

Parameters:

  • pattern (String) (defaults to: nil)

    the wildcard search pattern

Returns:

  • (Array<String>)

    an array of entries (including directories) in the remote directory



125
126
127
# File 'lib/xftp/session/ftp.rb', line 125

def entries(pattern = nil)
  @ftp.nlst pattern
end

#exists?(dirname) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns ‘true` if the argument refers to a directory on the remote host.

Returns:

  • (Boolean)

    ‘true` if the argument refers to a directory on the remote host



49
50
51
# File 'lib/xftp/session/ftp.rb', line 49

def exists?(dirname)
  entries.include? dirname
end

#file?(path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

a file on the remote host

Returns:

  • (Boolean)

    ‘true` if the argument refers to



69
70
71
# File 'lib/xftp/session/ftp.rb', line 69

def file?(path)
  !directory?(path)
end

#filesArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an array of filenames in the remote directory.

Returns:

  • (Array<String>)

    an array of filenames in the remote directory



118
119
120
# File 'lib/xftp/session/ftp.rb', line 118

def files
  entries.select { |entry| file? entry }
end

#get(filename) ⇒ StringIO

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Downloads file into IO object

Returns:

  • (StringIO)

    the remote file data



98
99
100
# File 'lib/xftp/session/ftp.rb', line 98

def get(filename)
  @ftp.getbinaryfile(filename, nil)
end

#glob(pattern, &callback) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
# File 'lib/xftp/session/ftp.rb', line 103

def glob(pattern, &callback)
  Operations::FTP::Glob.new(@ftp).call(pattern, &callback)
end

#mkdir(dirname) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a remote directory

Parameters:

  • dirname (String)

    the name of new directory relative to the current (remote) working directory



36
37
38
39
# File 'lib/xftp/session/ftp.rb', line 36

def mkdir(dirname)
  ensure_relative_path! :mkdir, dirname
  @ftp.mkdir dirname
end

#move(from, to:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Renames (moves) a file on the server

Parameters:

  • from (String)

    the path to move from

  • to (String)

    the new path to move to



76
77
78
79
# File 'lib/xftp/session/ftp.rb', line 76

def move(from, to:)
  log "moving from #{from} to #{to}..."
  @ftp.rename(from, to)
end

#rmdir(dirname) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes the remote directory

Parameters:

  • dirname (String)

    the name of directory to be removed



43
44
45
46
# File 'lib/xftp/session/ftp.rb', line 43

def rmdir(dirname)
  ensure_relative_path! :rmdir, dirname
  @ftp.rmdir dirname
end