Class: Datapimp::Sync::DropboxFolder

Inherits:
Hashie::Mash
  • Object
show all
Includes:
Logging
Defined in:
lib/datapimp/sync/dropbox_folder.rb

Instance Method Summary collapse

Methods included from Logging

#log, #logger, #logger=

Instance Method Details

#cursorObject

The Dropbox Delta API uses a cursor to keep track of the last state the local filesystem has synced with. We store this in the syncable folder itself



70
71
72
# File 'lib/datapimp/sync/dropbox_folder.rb', line 70

def cursor
  cursor_path.exist? && cursor_path.read
end

#cursor_pathObject



74
75
76
# File 'lib/datapimp/sync/dropbox_folder.rb', line 74

def cursor_path
  local_path.join('.dropbox-cursor')
end

#deltaObject

Provides access to the Dropbox API Delta which will tell us how to modify the state of ‘local_path` to match what exists on Dropbox.



58
59
60
# File 'lib/datapimp/sync/dropbox_folder.rb', line 58

def delta
  @delta ||= dropbox.delta(cursor, remote_path)
end

#dropboxObject

Provides easy access to the Dropbox client



51
52
53
# File 'lib/datapimp/sync/dropbox_folder.rb', line 51

def dropbox
  @dropbox ||= Datapimp::Sync.dropbox
end

#local_pathObject

A Pointer to the local path we will be syncing with the Dropbox remote



63
64
65
# File 'lib/datapimp/sync/dropbox_folder.rb', line 63

def local_path
  Pathname(local)
end

#remote_pathObject



78
79
80
81
82
# File 'lib/datapimp/sync/dropbox_folder.rb', line 78

def remote_path
  dropbox.ls(remote)
rescue(Dropbox::API::Error::NotFound)
  nil
end

#remote_path_entriesObject



95
96
97
98
99
100
101
102
103
# File 'lib/datapimp/sync/dropbox_folder.rb', line 95

def remote_path_entries
  remote_path.map do |entry|
    if entry.is_dir
      dropbox.ls(entry.path)
    else
      entry
    end
  end.flatten
end

#remote_path_missing?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/datapimp/sync/dropbox_folder.rb', line 91

def remote_path_missing?
  remote_path.nil?
end

#remote_path_parentObject



84
85
86
87
88
89
# File 'lib/datapimp/sync/dropbox_folder.rb', line 84

def remote_path_parent
  parent, _ = File.split(remote)
  dropbox.ls(parent)
rescue(Dropbox::API::Error::NotFound)
  nil
end

#run(action, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/datapimp/sync/dropbox_folder.rb', line 5

def run(action, options={})
  action = action.to_sym

  log "DropboxFolder run:#{action}"

  if action == :push
    run_push_action
  elsif action == :pull
    run_pull_action
  elsif action == :create
    run_create_action
  end
end

#run_create_actionObject



19
20
21
# File 'lib/datapimp/sync/dropbox_folder.rb', line 19

def run_create_action
  dropbox.mkdir(remote)
end

#run_pull_actionObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/datapimp/sync/dropbox_folder.rb', line 23

def run_pull_action
  remote_path_entries.each do |entry|
    remote_dropbox_path = entry.path
    remote_content = dropbox.download(remote_dropbox_path)
    relative_local_path = remote_dropbox_path.gsub("#{remote}/",'')

    log "Saving #{ remote_content.length } bytes to #{ relative_local_path }"
    local_path.join(relative_local_path).open("w+") {|fh| fh.write(remote_content) }
  end
end

#run_push_actionObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/datapimp/sync/dropbox_folder.rb', line 34

def run_push_action
  if remote_path_missing?
    run_create_action()
  end

  Dir[local_path.join("**/*")].each do |f|
    f = Pathname(f)
    base = f.relative_path_from(local_path).to_s
    target_path = File.join(remote, base)

    log "Uploading #{ f } to #{target_path}"

    dropbox.upload(target_path, f.read, :overwrite => false)
  end
end