Module: Dbox

Defined in:
lib/dbox.rb,
lib/dbox/db.rb,
lib/dbox/api.rb,
lib/dbox/utils.rb,
lib/dbox/syncer.rb,
lib/dbox/database.rb,
lib/dbox/loggable.rb

Defined Under Namespace

Modules: Loggable, Utils Classes: API, BadPath, ConfigurationError, CorruptDatabase, DB, Database, DatabaseError, MissingDatabase, RemoteAlreadyExists, RemoteMissing, RequestDenied, ServerError, Syncer

Constant Summary collapse

NUM_TRIES =
3
TIME_BETWEEN_TRIES =

in seconds

3

Class Method Summary collapse

Class Method Details

.authorizeObject



22
23
24
# File 'lib/dbox.rb', line 22

def self.authorize
  Dbox::API.authorize
end

.clone(remote_path, local_path) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/dbox.rb', line 34

def self.clone(remote_path, local_path)
  log.debug "Cloning (remote: #{remote_path}, local: #{local_path})"
  remote_path = clean_remote_path(remote_path)
  local_path = clean_local_path(local_path)
  migrate_dbfile(local_path)
  Dbox::Syncer.clone(remote_path, local_path)
end

.clone_or_pull(remote_path, local_path) ⇒ Object



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

def self.clone_or_pull(remote_path, local_path)
  if exists?(local_path)
    pull(local_path)
  else
    clone(remote_path, local_path)
  end
end

.create(remote_path, local_path) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/dbox.rb', line 26

def self.create(remote_path, local_path)
  log.debug "Creating (remote: #{remote_path}, local: #{local_path})"
  remote_path = clean_remote_path(remote_path)
  local_path = clean_local_path(local_path)
  migrate_dbfile(local_path)
  Dbox::Syncer.create(remote_path, local_path)
end

.delete(remote_path, local_path = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/dbox.rb', line 86

def self.delete(remote_path, local_path = nil)
  log.debug "Deleting (remote: #{remote_path})"
  remote_path = clean_remote_path(remote_path)
  Dbox::Syncer.api.delete_dir(remote_path)
  if local_path
    local_path = clean_local_path(local_path)
    log.debug "Deleting (local_path: #{local_path})"
    FileUtils.rm_rf(local_path) if File.exists?(local_path)
  end
end

.exists?(local_path) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/dbox.rb', line 80

def self.exists?(local_path)
  local_path = clean_local_path(local_path)
  migrate_dbfile(local_path)
  Dbox::Database.exists?(local_path)
end

.logObject



18
19
20
# File 'lib/dbox/loggable.rb', line 18

def self.log
  @logger ||= setup_logger
end

.metadata(remote_path) ⇒ Object



97
98
99
100
101
# File 'lib/dbox.rb', line 97

def self.(remote_path)
  log.debug "Getting metadata for #{remote_path}"
  remote_path = clean_remote_path(remote_path)
  Dbox::Syncer.api.(remote_path)
end

.move(new_remote_path, local_path) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/dbox.rb', line 72

def self.move(new_remote_path, local_path)
  log.debug "Moving (new remote: #{new_remote_path}, local: #{local_path})"
  new_remote_path = clean_remote_path(new_remote_path)
  local_path = clean_local_path(local_path)
  migrate_dbfile(local_path)
  Dbox::Syncer.move(new_remote_path, local_path)
end

.pull(local_path) ⇒ Object



42
43
44
45
46
47
# File 'lib/dbox.rb', line 42

def self.pull(local_path)
  log.debug "Pulling (local: #{local_path})"
  local_path = clean_local_path(local_path)
  migrate_dbfile(local_path)
  Dbox::Syncer.pull(local_path)
end

.push(local_path) ⇒ Object



57
58
59
60
61
62
# File 'lib/dbox.rb', line 57

def self.push(local_path)
  log.debug "Pushing (local: #{local_path})"
  local_path = clean_local_path(local_path)
  migrate_dbfile(local_path)
  Dbox::Syncer.push(local_path)
end

.setup_loggerObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dbox/loggable.rb', line 22

def self.setup_logger
  if defined?(LOGGER)
    LOGGER
  elsif defined?(Rails.logger)
    Rails.logger
  else
    l = Logger.new(STDOUT)
    l.level = (ENV["DEBUG"] && ENV["DEBUG"] != "false") ? Logger::DEBUG : Logger::INFO
    l.formatter = proc {|severity, datetime, progname, msg| "[#{severity}] #{msg}\n" }
    l
  end
end

.sync(local_path) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/dbox.rb', line 64

def self.sync(local_path)
  log.debug "Syncing (local: #{local_path})"
  res = {}
  res[:pull] = pull(local_path)
  res[:push] = push(local_path)
  res
end