Class: Jorp::RemoteAdaptor

Inherits:
Object
  • Object
show all
Defined in:
lib/jorp/remote_adaptor.rb

Instance Method Summary collapse

Constructor Details

#initialize(hostname, path, username) ⇒ RemoteAdaptor

Returns a new instance of RemoteAdaptor.



14
15
16
17
18
19
20
# File 'lib/jorp/remote_adaptor.rb', line 14

def initialize(hostname, path, username)
  @hostname = hostname
  @path = path
  @username = username
  @proxy_directory = Dir.mktmpdir('jorp')
  @proxy_path = File.join(@proxy_directory, File.basename(@path))
end

Instance Method Details

#connectObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jorp/remote_adaptor.rb', line 40

def connect
  connection = Net::SSH.start(@hostname, @username)
  download(connection)
  Thread.new do
    begin
      Listen.to(@proxy_directory, :filter => /^#{Regexp.escape(File.basename(@proxy_path))}$/) do
        upload(connection)
      end
    rescue Exception => e
      puts "An error occurred: " + e.inspect
    end
  end
end

#disconnectObject



54
55
56
# File 'lib/jorp/remote_adaptor.rb', line 54

def disconnect
  FileUtils.remove_entry_secure(@proxy_directory)
end

#download(connection) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/jorp/remote_adaptor.rb', line 31

def download(connection)
  begin
    copier = Net::SCP.new(connection)
    copier.download!(@path, @proxy_path)
  rescue Exception => e
    puts "ERROR: " + e
  end
end

#proxy_pathObject



58
59
60
# File 'lib/jorp/remote_adaptor.rb', line 58

def proxy_path
  @proxy_path
end

#upload(connection) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/jorp/remote_adaptor.rb', line 22

def upload(connection)
  begin
    copier = Net::SCP.new(connection)
    copier.upload!(@proxy_path, @path)
  rescue Exception => e
    puts "ERROR: " + e
  end
end