Class: APDM::Saxo::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/apdm/saxo/remote.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(saxo, photo) ⇒ Remote

Returns a new instance of Remote.



9
10
11
12
# File 'lib/apdm/saxo/remote.rb', line 9

def initialize(saxo, photo)
  self.saxo = saxo
  self.photo = photo
end

Instance Attribute Details

#local_sizeObject

Returns the value of attribute local_size.



8
9
10
# File 'lib/apdm/saxo/remote.rb', line 8

def local_size
  @local_size
end

#photoObject

Returns the value of attribute photo.



8
9
10
# File 'lib/apdm/saxo/remote.rb', line 8

def photo
  @photo
end

#remote_sizeObject

Returns the value of attribute remote_size.



8
9
10
# File 'lib/apdm/saxo/remote.rb', line 8

def remote_size
  @remote_size
end

#saxoObject

Returns the value of attribute saxo.



8
9
10
# File 'lib/apdm/saxo/remote.rb', line 8

def saxo
  @saxo
end

Instance Method Details

#cleanup(ftp) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/apdm/saxo/remote.rb', line 49

def cleanup(ftp)
  if transfer_succeeded?
    File.delete(source)
  else
    ftp.delete(destination)
  end
end

#destinationObject



22
23
24
# File 'lib/apdm/saxo/remote.rb', line 22

def destination
  @destination ||= "#{saxo.remote_dir}/#{photo.name}"
end

#loggerObject



14
15
16
# File 'lib/apdm/saxo/remote.rb', line 14

def logger
  APDM::Saxo.logger
end

#sourceObject



18
19
20
# File 'lib/apdm/saxo/remote.rb', line 18

def source
  @source ||= photo.file
end

#transferObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/apdm/saxo/remote.rb', line 26

def transfer
  logger.info("Begin transfer of #{source} to #{destination}")
  Timeout.timeout(30) do
    Net::FTP.open(saxo.server, saxo.username, saxo.password) do |ftp|
      Timeout.timeout(20) do
        upload(ftp)
      end
      Timeout.timeout(10) do
        cleanup(ftp)
      end
    end
  end
  return true if transfer_succeeded?

  raise SaxoTransferError("Upload failed [#{local_size}/#{remote_size}]. Source: #{source} -- destination: #{destination}")
end

#transfer_succeeded?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/apdm/saxo/remote.rb', line 57

def transfer_succeeded?
  local_size == remote_size
end

#upload(ftp) ⇒ Object



43
44
45
46
47
# File 'lib/apdm/saxo/remote.rb', line 43

def upload(ftp)
  ftp.passive = true
  ftp.putbinaryfile(source, destination)
  self.remote_size = ftp.size(destination)
end