Class: Cloudsync::Backend::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudsync/backend/base.rb

Direct Known Subclasses

CloudFiles, S3, Sftp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
# File 'lib/cloudsync/backend/base.rb', line 8

def initialize(opts = {})
  @sync_manager     = opts[:sync_manager]
  @name             = opts[:name]
  @backend_type     = opts[:backend] || self.class.to_s.split("::").last
  @download_prefix  = opts[:download_prefix] || ""
  @upload_prefix    = opts[:upload_prefix] || ""
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/cloudsync/backend/base.rb', line 6

def name
  @name
end

#storeObject

Returns the value of attribute store.



6
7
8
# File 'lib/cloudsync/backend/base.rb', line 6

def store
  @store
end

#sync_managerObject

Returns the value of attribute sync_manager.



6
7
8
# File 'lib/cloudsync/backend/base.rb', line 6

def sync_manager
  @sync_manager
end

#upload_prefixObject

Returns the value of attribute upload_prefix.



6
7
8
# File 'lib/cloudsync/backend/base.rb', line 6

def upload_prefix
  @upload_prefix
end

Instance Method Details

#copy(file, to_backend) ⇒ Object

copy



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cloudsync/backend/base.rb', line 17

def copy(file, to_backend)
  start_copy = Time.now
  $LOGGER.info("Copying file #{file} from #{self} to #{to_backend}")
  tempfile = download(file)
  if tempfile
    to_backend.put(file, tempfile.path)

    $LOGGER.debug("Finished copying #{file} from #{self} to #{to_backend} (#{Time.now - start_copy}s)")
    tempfile.unlink
  else
    $LOGGER.info("Failed to download #{file}")
  end
end

#delete(file, delete_bucket_if_empty = true) ⇒ Object

delete

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/cloudsync/backend/base.rb', line 68

def delete(file, delete_bucket_if_empty=true)
  raise NotImplementedError
end

#download(file) ⇒ Object

download

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/cloudsync/backend/base.rb', line 58

def download(file)
  raise NotImplementedError
end

#files_to_sync(upload_prefix = {}) ⇒ Object

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/cloudsync/backend/base.rb', line 72

def files_to_sync(upload_prefix={})
  raise NotImplementedError
end

#get_file_from_store(file) ⇒ Object

get_file_from_store

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/cloudsync/backend/base.rb', line 77

def get_file_from_store(file)
  raise NotImplementedError
end

#needs_update?(file) ⇒ Boolean

needs_update?

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloudsync/backend/base.rb', line 36

def needs_update?(file)
  $LOGGER.debug("Checking if file needs update: #{file}")

  local_backend_file = get_file_from_store(file)

  if local_backend_file.nil?
    $LOGGER.debug("File doesn't exist at #{self} (#{file})")
    return true
  end

  if file.e_tag == local_backend_file.e_tag
    $LOGGER.debug("Etags match for #{file}")
    return false
  else
    $LOGGER.debug(["Etags don't match for #{file}.",
                  "#{file.backend}: #{file.e_tag}",
                  "#{self}: #{local_backend_file.e_tag}"].join(" "))
    return true
  end
end

#put(file, local_filepath) ⇒ Object

put

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/cloudsync/backend/base.rb', line 63

def put(file, local_filepath)
  raise NotImplementedError
end

#to_sObject



31
32
33
# File 'lib/cloudsync/backend/base.rb', line 31

def to_s
  "#{@name}[:#{@backend_type}/#{@upload_prefix}]"
end