Class: XFTP::Session::SFTP Private

Inherits:
Base
  • Object
show all
Defined in:
lib/xftp/session/sftp.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.

SFTP session adapter

Defined Under Namespace

Classes: ProgressHandler

Constant Summary collapse

RENAME_OPERATION_FLAGS =

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

Default flags for rename operation

0x0004
GLOB_OPERATION_FLAGS =

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

Default flags for glob operation

File::FNM_EXTGLOB

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 = {}) ⇒ SFTP

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 SFTP session adapter instance



44
45
46
47
48
49
# File 'lib/xftp/session/sftp.rb', line 44

def initialize(uri, settings = {})
  super
  @path = Pathname '.'
  @settings[:password] = @credentials[:password]
  @ssh_options = XFTP.config.ssh.deep_merge(@settings)
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



53
54
55
56
# File 'lib/xftp/session/sftp.rb', line 53

def chdir(path)
  ensure_relative_path! :chdir, path
  @path += 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



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

def directory?(path)
  ensure_relative_path! :directory?, path
  @sftp.file.directory? remote_path(path)
end

#download(from, to: File.basename(from), **options) ⇒ 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

See Also:

  • Net::SFTP::Operations::Download


140
141
142
143
144
145
# File 'lib/xftp/session/sftp.rb', line 140

def download(from, to: File.basename(from), **options)
  log "downloading file from #{from} to #{to}..."
  remote = remote_path(from)
  local = (Pathname.pwd + to).to_s
  @sftp.download!(remote, local, options)
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 (asynchronously) yields a filename to the block



104
105
106
107
108
109
# File 'lib/xftp/session/sftp.rb', line 104

def each_file
  @sftp.dir.foreach(@path.to_s) do |entry|
    filename = entry.name
    yield filename unless directory? filename
  end
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 (asynchronously) yields a filename and ‘StringIO` object to the block



113
114
115
116
117
118
# File 'lib/xftp/session/sftp.rb', line 113

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

#entriesArray<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.



153
154
155
# File 'lib/xftp/session/sftp.rb', line 153

def entries
  @sftp.dir.entries(@path.to_s).map(&:name)
end

#exists?(filename) ⇒ 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.

in a current working directory on the remote host



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

def exists?(filename)
  entries.include? filename
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



90
91
92
# File 'lib/xftp/session/sftp.rb', line 90

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.



148
149
150
# File 'lib/xftp/session/sftp.rb', line 148

def files
  entries.reject { |filename| directory? filename }
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



122
123
124
# File 'lib/xftp/session/sftp.rb', line 122

def get(filename)
  @sftp.download!(remote_path(filename), nil, progress: ProgressHandler)
end

#glob(pattern, flags: GLOB_OPERATION_FLAGS) ⇒ 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.

For more info (see Dir#glob), it’s almost of the same nature



131
132
133
# File 'lib/xftp/session/sftp.rb', line 131

def glob(pattern, flags: GLOB_OPERATION_FLAGS)
  @sftp.dir.glob(@path.to_s, pattern, flags) { |entry| yield entry.name }
end

#mkdir(dirname, attrs = {}) ⇒ 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



63
64
65
66
# File 'lib/xftp/session/sftp.rb', line 63

def mkdir(dirname, attrs = {})
  ensure_relative_path! :mkdir, path
  @sftp.mkdir!(remote_path(dirname), attrs)
end

#move(from, to:, flags: RENAME_OPERATION_FLAGS) ⇒ 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



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

def move(from, to:, flags: RENAME_OPERATION_FLAGS)
  log "moving from #{from} to #{to}..."
  @sftp.rename!(remote_path(from), remote_path(to), flags)
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



70
71
72
73
# File 'lib/xftp/session/sftp.rb', line 70

def rmdir(dirname)
  ensure_relative_path! :rmdir, path
  @sftp.rmdir! remote_path(dirname)
end