Class: Dandelion::Adapter::SFTP

Inherits:
Base
  • Object
show all
Includes:
Utils
Defined in:
lib/dandelion/adapter/sftp.rb

Instance Method Summary collapse

Methods included from Utils

#temp

Methods inherited from Base

adapter, create_adapter, requires_gems

Constructor Details

#initialize(config) ⇒ SFTP

Returns a new instance of SFTP.



12
13
14
15
16
17
18
19
# File 'lib/dandelion/adapter/sftp.rb', line 12

def initialize(config)
  require 'net/sftp'

  @config = config
  @config.defaults(preserve_permissions: true)

  @sftp = sftp_client
end

Instance Method Details

#delete(file) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/dandelion/adapter/sftp.rb', line 49

def delete(file)
  begin
    @sftp.remove!(path(file))
    cleanup(File.dirname(path(file)))
  rescue Net::SFTP::StatusException => e
    raise unless e.code == 2
  end
end

#read(file) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/dandelion/adapter/sftp.rb', line 21

def read(file)
  begin
    @sftp.file.open(path(file), 'r') do |f|
      f.gets
    end
  rescue Net::SFTP::StatusException => e
    raise unless e.code == 2
    nil
  end
end

#to_sObject



58
59
60
# File 'lib/dandelion/adapter/sftp.rb', line 58

def to_s
  "sftp://#{@config['username']}@#{@config['host']}/#{@config['path']}"
end

#write(file, data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dandelion/adapter/sftp.rb', line 32

def write(file, data)
  temp(file, data) do |temp|
    begin
      @sftp.upload!(temp, path(file))
    rescue Net::SFTP::StatusException => e
      raise unless e.code == 2
      mkdir_p(File.dirname(path(file)))
      @sftp.upload!(temp, path(file))
    end
  end

  if @config[:preserve_permissions]
    mode = get_mode(file)
    @sftp.setstat!(path(file), permissions: mode) if mode
  end
end