Class: AnPostReturn::SFTP::Client
- Inherits:
-
Object
- Object
- AnPostReturn::SFTP::Client
- Defined in:
- lib/an_post_return/sftp/client.rb
Instance Attribute Summary collapse
-
#connected ⇒ Object
(also: #connected?)
readonly
SFTP connection configuration.
-
#host ⇒ Object
readonly
SFTP connection configuration.
-
#password ⇒ Object
readonly
SFTP connection configuration.
-
#proxy_config ⇒ Object
readonly
SFTP connection configuration.
-
#username ⇒ Object
readonly
SFTP connection configuration.
Instance Method Summary collapse
-
#connect ⇒ Boolean
Establish SFTP connection.
-
#disconnect ⇒ void
Close SFTP connection.
-
#initialize(host, username, password, proxy_config = nil) ⇒ Client
constructor
Initialize a new SFTP client.
-
#list_files(remote_path, glob_pattern = nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>
List files in remote directory.
-
#read_file(remote_path) {|Tempfile| ... } ⇒ Tempfile, Object
Download and read a file from SFTP server.
Constructor Details
#initialize(host, username, password, proxy_config = nil) ⇒ Client
Initialize a new SFTP client
26 27 28 29 30 31 32 |
# File 'lib/an_post_return/sftp/client.rb', line 26 def initialize(host, username, password, proxy_config = nil) @host = host @username = username @password = password @proxy_config = proxy_config @connected = false end |
Instance Attribute Details
#connected ⇒ Object (readonly) Also known as: connected?
SFTP connection configuration
12 13 14 |
# File 'lib/an_post_return/sftp/client.rb', line 12 def connected @connected end |
#host ⇒ Object (readonly)
SFTP connection configuration
12 13 14 |
# File 'lib/an_post_return/sftp/client.rb', line 12 def host @host end |
#password ⇒ Object (readonly)
SFTP connection configuration
12 13 14 |
# File 'lib/an_post_return/sftp/client.rb', line 12 def password @password end |
#proxy_config ⇒ Object (readonly)
SFTP connection configuration
12 13 14 |
# File 'lib/an_post_return/sftp/client.rb', line 12 def proxy_config @proxy_config end |
#username ⇒ Object (readonly)
SFTP connection configuration
12 13 14 |
# File 'lib/an_post_return/sftp/client.rb', line 12 def username @username end |
Instance Method Details
#connect ⇒ Boolean
Establish SFTP connection
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/an_post_return/sftp/client.rb', line 38 def connect return true if @connected @ssh_session = start_ssh_session @sftp_client = Net::SFTP::Session.new(@ssh_session) @sftp_client.connect! @connected = true true rescue Net::SSH::Exception => e raise ConnectionError, "Failed to connect to #{host}: #{e.}" end |
#disconnect ⇒ void
This method returns an undefined value.
Close SFTP connection
54 55 56 57 58 59 60 |
# File 'lib/an_post_return/sftp/client.rb', line 54 def disconnect return unless @connected @sftp_client.close_channel @ssh_session.close @connected = false end |
#list_files(remote_path, glob_pattern = nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>
List files in remote directory
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/an_post_return/sftp/client.rb', line 95 def list_files(remote_path, glob_pattern = nil) ensure_connected entries = [] begin if glob_pattern @sftp_client.dir.glob(remote_path, glob_pattern) { |entry| entries << entry } else @sftp_client.dir.foreach(remote_path) { |entry| entries << entry } end entries rescue Net::SFTP::StatusException => e raise FileError, "Failed to list files in #{remote_path}: #{e.}" end end |
#read_file(remote_path) {|Tempfile| ... } ⇒ Tempfile, Object
Download and read a file from SFTP server
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/an_post_return/sftp/client.rb', line 68 def read_file(remote_path) ensure_connected temp_file = Tempfile.new(["sftp", File.extname(remote_path)]) begin @sftp_client.download!(remote_path, temp_file.path) block_given? ? yield(temp_file) : temp_file rescue Net::SFTP::StatusException => e if e..include?("no such file") raise FileNotFoundError else raise FileError, "Failed to download #{remote_path}: #{e.}" end ensure unless block_given? temp_file.close temp_file.unlink end end end |