Class: Cloudsync::Backend::Base

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

Direct Known Subclasses

CloudFiles, S3, Sftp

Constant Summary collapse

OBJECT_LIMIT =
250
CONTAINER_LIMIT =
250
BUCKET_LIMIT =
CONTAINER_LIMIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
# File 'lib/cloudsync/backend/base.rb', line 12

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.



10
11
12
# File 'lib/cloudsync/backend/base.rb', line 10

def name
  @name
end

#storeObject

Returns the value of attribute store.



10
11
12
# File 'lib/cloudsync/backend/base.rb', line 10

def store
  @store
end

#sync_managerObject

Returns the value of attribute sync_manager.



10
11
12
# File 'lib/cloudsync/backend/base.rb', line 10

def sync_manager
  @sync_manager
end

#upload_prefixObject

Returns the value of attribute upload_prefix.



10
11
12
# File 'lib/cloudsync/backend/base.rb', line 10

def upload_prefix
  @upload_prefix
end

Instance Method Details

#copy(file, to_backend) ⇒ Object

copy



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cloudsync/backend/base.rb', line 21

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:



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

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

#download(file) ⇒ Object

download

Raises:



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

def download(file)
  raise NotImplementedError
end

#files_to_sync(upload_prefix = "") ⇒ Object

Raises:



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

def files_to_sync(upload_prefix="")
  raise NotImplementedError
end

#get_file_from_store(file) ⇒ Object

get_file_from_store

Raises:



81
82
83
# File 'lib/cloudsync/backend/base.rb', line 81

def get_file_from_store(file)
  raise NotImplementedError
end

#needs_update?(file) ⇒ Boolean

needs_update?

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cloudsync/backend/base.rb', line 40

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:



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

def put(file, local_filepath)
  raise NotImplementedError
end

#to_sObject



35
36
37
# File 'lib/cloudsync/backend/base.rb', line 35

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