lib/io_streams/paths/sftp.rb in iostreams-1.0.0.beta vs lib/io_streams/paths/sftp.rb in iostreams-1.0.0.beta2
- old
+ new
@@ -1,16 +1,16 @@
module IOStreams
module Paths
class SFTP < IOStreams::Path
include SemanticLogger::Loggable if defined?(SemanticLogger)
- attr_reader :hostname, :username, :file_name, :create_path, :options
+ attr_reader :hostname, :username, :create_path, :options, :url
# Stream to a remote file over sftp.
#
- # file_name: [String]
- # Name of file to write to.
+ # url: [String]
+ # "sftp://<host_name>/<file_name>"
#
# username: [String]
# Name of user to login with.
#
# password: [String]
@@ -37,11 +37,10 @@
uri = URI.parse(url)
raise(ArgumentError, "Invalid URL. Required Format: 'sftp://<host_name>/<file_name>'") unless uri.scheme == 'sftp'
@hostname = uri.hostname
- @file_name = uri.path
@mkdir = false
@username = username || uri.user
@create_path = create_path
logger ||= self.logger if defined?(SemanticLogger)
@@ -49,13 +48,17 @@
options[:logger] = logger
options[:port] = port || uri.port || 22
options[:max_pkt_size] = max_pkt_size
options[:password] = password || uri.password
@options = options
- super(file_name)
+ super(uri.path)
end
+ def to_s
+ url
+ end
+
def mkdir
@mkdir = true
self
end
@@ -71,11 +74,11 @@
# Note:
# - raises Net::SFTP::StatusException when the file could not be read.
def reader(&block)
result = nil
Net::SFTP.start(hostname, username, options) do |sftp|
- result = sftp.file.open(file_name, 'rb', &block)
+ result = sftp.file.open(path, 'rb', &block)
end
result
end
# Write to a file on a remote sftp server.
@@ -87,11 +90,11 @@
# output.write('Hello World')
# end
def writer(&block)
result = nil
Net::SFTP.start(hostname, username, options) do |sftp|
- sftp.session.exec!("mkdir -p '#{::File.dirname(file_name)}'") if create_path
- result = sftp.file.open(file_name, 'wb', &block)
+ sftp.session.exec!("mkdir -p '#{::File.dirname(path)}'") if create_path
+ result = sftp.file.open(path, 'wb', &block)
end
result
end
end
end