Class: Fife::Storage::Sftp

Inherits:
Object
  • Object
show all
Defined in:
lib/fife/storage/sftp.rb

Defined Under Namespace

Classes: UninitializedAttributes

Constant Summary collapse

REQUIRED_ATTRIBUTES =
[:host, :user, :naming, :ssh_options, :remote_dir]

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Sftp

Returns a new instance of Sftp.



34
35
36
37
38
39
# File 'lib/fife/storage/sftp.rb', line 34

def initialize(&block)
  instance_eval &block
  @naming ||= -> io {io.name}
  uninitialized_attributes = REQUIRED_ATTRIBUTES.select{|attr| send(attr).nil?}
  raise UninitializedAttributes, uninitialized_attributes unless uninitialized_attributes.empty?
end

Instance Method Details

#remote_dir(*value) ⇒ Object



20
21
22
23
# File 'lib/fife/storage/sftp.rb', line 20

def remote_dir(*value)
  return @remote_dir if value.empty?
  @remote_dir = Pathname(value[0])
end

#store(io) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/fife/storage/sftp.rb', line 41

def store(io)
  remote_path = remote_dir.join(@naming.call(io))
  remote_dir = remote_path.dirname
  Net::SFTP.start(host, user, ssh_options) do |sftp|
    sftp.mkdir_p!(remote_dir)
    sftp.file.open(remote_path, 'w') { |f| IO.copy_stream(io.tap(&:rewind), f) }
  end
end