class SftpWrapper::Curl
The wrapper for curl.
Constants
- CURL_ERRORS
lookup table of curl errors.
Attributes
Public Class Methods
Initialize SFTP wrapper.
@param host [String] host address of SFTP server @param port [Integer] port number of SFTP server @param username [String] user name of SFTP server @param password [String] password of SFTP server @param options [Hash] curl options. @option options [String] :curl_path path of `curl` command. @option options [Array<String>] :curl_args command line arguments of `curl`.
# File lib/sftp_wrapper/curl.rb, line 38 def initialize(host, port, username, password, options = {}) @host = host @port = port @username = username @password = password @curl_path = options[:curl_path] || 'curl' @curl_args = options[:curl_args] || [] end
Public Instance Methods
Get remote file.
@param source [String] source file path @param destination [String] destination path @raise [SftpWrapper::Errors::ConnectionError] @raise [SftpWrapper::Errors::AuthenticationFailure] @raise [SftpWrapper::Errors::CommandError]
# File lib/sftp_wrapper/curl.rb, line 55 def download(source, destination) userinfo = [username, password].map(&ERB::Util.method(:url_encode)).join(':') uri = URI::Generic.build(scheme: 'sftp', userinfo: userinfo, host: host, port: port, path: source) cmd = %W[#{curl_path} -o #{destination}] + curl_args + [uri.to_s] execute(*cmd) end
Put local file.
@param source [String] source file path @param destination [String] destination path @raise [SftpWrapper::Errors::ConnectionError] @raise [SftpWrapper::Errors::AuthenticationFailure] @raise [SftpWrapper::Errors::CommandError]
# File lib/sftp_wrapper/curl.rb, line 71 def upload(source, destination) userinfo = [username, password].map(&ERB::Util.method(:url_encode)).join(':') uri = URI::Generic.build(scheme: 'sftp', userinfo: userinfo, host: host, port: port, path: destination) cmd = %W[#{curl_path} -T #{source}] + curl_args + [uri.to_s] execute(*cmd) end
Private Instance Methods
# File lib/sftp_wrapper/curl.rb, line 81 def execute(*cmd) _, stderr, status = Open3.capture3(*cmd) return if status.success? exception_class = CURL_ERRORS[status.exitstatus] || SftpWrapper::Errors::CommandError raise exception_class, "exit status #{status.exitstatus}: #{stderr}" end