Class: SftpWrapper::Curl

Inherits:
Object
  • Object
show all
Defined in:
lib/sftp_wrapper/curl.rb

Overview

The wrapper for curl.

Constant Summary collapse

CURL_ERRORS =

lookup table of curl errors.

{
  # URL malformed. The syntax was not correct.
  3 => SftpWrapper::Errors::ConnectionError,
  # Couldn't resolve proxy. The given proxy host could not be resolved.
  5 => SftpWrapper::Errors::ConnectionError,
  # Couldn't resolve host. The given remote host was not resolved.
  6 => SftpWrapper::Errors::ConnectionError,
  # Failed to connect to host.
  7 => SftpWrapper::Errors::ConnectionError,
  # The user name, password, or similar was not accepted and curl failed to log in.
  67 => SftpWrapper::Errors::AuthenticationFailure,
  # The resource referenced in the URL does not exist.
  78 => SftpWrapper::Errors::ResourceNotExist,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, username, password, options = {}) ⇒ Curl

Initialize SFTP wrapper.

Parameters:

  • host (String)

    host address of SFTP server

  • port (Integer)

    port number of SFTP server

  • username (String)

    user name of SFTP server

  • password (String)

    password of SFTP server

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

    curl options.

Options Hash (options):

  • :curl_path (String)

    path of ‘curl` command.

  • :curl_args (Array<String>)

    command line arguments of ‘curl`.



38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#curl_argsObject (readonly)

Returns the value of attribute curl_args.



10
11
12
# File 'lib/sftp_wrapper/curl.rb', line 10

def curl_args
  @curl_args
end

#curl_pathObject (readonly)

Returns the value of attribute curl_path.



10
11
12
# File 'lib/sftp_wrapper/curl.rb', line 10

def curl_path
  @curl_path
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/sftp_wrapper/curl.rb', line 10

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/sftp_wrapper/curl.rb', line 10

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/sftp_wrapper/curl.rb', line 10

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/sftp_wrapper/curl.rb', line 10

def username
  @username
end

Instance Method Details

#download(source, destination) ⇒ Object

Get remote file.

Parameters:

  • source (String)

    source file path

  • destination (String)

    destination path

Raises:



55
56
57
58
59
60
61
# 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

#upload(source, destination) ⇒ Object

Put local file.

Parameters:

  • source (String)

    source file path

  • destination (String)

    destination path

Raises:



71
72
73
74
75
76
77
# 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