Class: DRbQS::Transfer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/drbqs/utility/transfer/transfer_client.rb,
lib/drbqs/utility/transfer/transfer_client_connect.rb

Defined Under Namespace

Classes: Base, Local, SFTP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_directory, same_host, host, user) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 8

def initialize(server_directory, same_host, host, user)
  unless Pathname.new(server_directory).absolute?
    raise ArgumentError, "Directory of server must be absolute."
  end
  @directory = server_directory
  @same_host = same_host
  @local = DRbQS::Transfer::Client::Local.new(@directory)
  if host && user
    @sftp = DRbQS::Transfer::Client::SFTP.new(user, host, @directory)
  else
    @sftp = nil
  end
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



6
7
8
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 6

def directory
  @directory
end

#localObject (readonly)

Returns the value of attribute local.



6
7
8
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 6

def local
  @local
end

#sftpObject (readonly)

Returns the value of attribute sftp.



6
7
8
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 6

def sftp
  @sftp
end

Class Method Details

.getObject



63
64
65
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 63

def get
  @transfer
end

.set(transfer) ⇒ Object



67
68
69
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 67

def set(transfer)
  @transfer = transfer
end

.transfer_to_server(files) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 71

def transfer_to_server(files)
  if files && @transfer
    begin
      @transfer.transfer(files)
    rescue Exception => err
      err_new = err.class.new("#{err.to_s} (#{err.class}); Can not send file: #{files.join(", ")}")
      err_new.set_backtrace(err.backtrace)
      raise err_new
    end
  else
    raise "Server does not set transfer settings. Can not send file: #{files.join(", ")}"
  end
end

Instance Method Details

#download(files, readonly = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 39

def download(files, readonly = nil)
  download_files = nil
  if @same_host
    begin
      if readonly
        download_files = files
      else
        download_files = @local.download(files)
      end
    rescue
    end
  end
  if !download_files
    unless @sftp
      raise "SFTP is not prepared."
    end
    download_files = @sftp.download(files)
  end
  download_files
end

#transfer(files) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/drbqs/utility/transfer/transfer_client.rb', line 22

def transfer(files)
  transfered = false
  if @same_host
    begin
      @local.transfer(files)
      transfered = true
    rescue
    end
  end
  if !transfered
    unless @sftp
      raise "Can not transfer files."
    end
    @sftp.transfer(files)
  end
end