Class: Nesta::Plugin::Drop::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nesta-plugin-drop/client.rb

Class Method Summary collapse

Class Method Details

.bootstrap!Object



128
129
130
131
132
133
134
135
# File 'lib/nesta-plugin-drop/client.rb', line 128

def self.bootstrap!
  Nesta::Plugin::Drop.logger.debug "NESTADROP: Bootstrapping local instance..."
  unless nestadrop_synced?
    Thread.new do
      cache_files
    end
  end
end

.bounce_server!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nesta-plugin-drop/client.rb', line 47

def self.bounce_server!
  return if syncing?
  Nesta::Plugin::Drop.logger.info "NESTADROP: Purging nesta file cache."
  Nesta::FileModel.purge_cache
  Nesta::Plugin::Drop.logger.info "NESTADROP: Restarting server..."
  unless system("bundle exec pumactl -S /tmp/.app_state phased-restart")
    Thread.new do
      Nesta::Plugin::Drop.logger.info "NESTADROP: Waiting for server to load before restarting."
      sleep(3)
      bounce_server!
    end
  end
end

.cache_file(file) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nesta-plugin-drop/client.rb', line 83

def self.cache_file(file)
  confirm_synced!
  local_path = [Nesta::App.root, file].join("/")
  Nesta::Plugin::Drop.logger.debug "NESTADROP: Caching '#{file}' to local filesystem at '#{local_path}'..."
  FileUtils.mkdir_p(File.dirname(local_path))
  file_contents = RestClient.get "#{host}file?file=#{URI.encode(file)}"
  File.open(local_path, 'w') do |fo|
    fo.write file_contents
  end
  Nesta::Plugin::Drop.logger.debug "NESTADROP: Cached '#{local_path}'."
  bounce_server!
rescue RuntimeError => ex
  puts ex
end

.cache_filesObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nesta-plugin-drop/client.rb', line 98

def self.cache_files
  self.uncached_files = Client.files
  return unless uncached_files.size > 0
  @syncing = true
  threads = []
  5.times do
    threads << Thread.new do
     Nesta::Plugin::Drop.logger.debug "NESTADROP: Creating worker thread to cache files..."
      file = nil
      while self.uncached_files.size > 0
        lock.synchronize do
          file = self.uncached_files.pop
        end
        cache_file(file) if file
      end
    end
    Nesta::Plugin::Drop.logger.debug "NESTADROP: Worker thread complete."
  end
  threads.each(&:join)
  @syncing = false
  bounce_server!
end

.confirm_synced!Object



20
21
22
23
24
25
26
# File 'lib/nesta-plugin-drop/client.rb', line 20

def self.confirm_synced!
  return true if nestadrop_synced?
  Nesta::Plugin::Drop.logger.debug "NESTADROP: Syncing with Dropbox filesystem."
  File.open("/tmp/.nestadropped", "w+") do |f|
    f.write "synced"
  end
end

.filesObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/nesta-plugin-drop/client.rb', line 61

def self.files
  lock.synchronize do
    Nesta::Plugin::Drop.logger.debug "NESTADROP: Retrieving file list..."
    @files ||= Yajl::Parser.parse(RestClient.get "#{host}files", {
      accept: :json, x_nestadrop_version: Nesta::Plugin::Drop::VERSION })
  end
  @files
rescue RestClient::Unauthorized
  return []
end

.hostObject



8
9
10
# File 'lib/nesta-plugin-drop/client.rb', line 8

def self.host
  ENV["NESTADROP_URL"]
end

.lockObject



16
17
18
# File 'lib/nesta-plugin-drop/client.rb', line 16

def self.lock
  @lock ||= Mutex.new
end

.nestadrop_configured?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/nesta-plugin-drop/client.rb', line 38

def self.nestadrop_configured?
  return true if nestadrop_synced?
  Nesta::Plugin::Drop.logger.debug "NESTADROP: Checking if account is linked to Dropbox."
  json = RestClient.get "#{host}account", {
    accept: :json, x_nestadrop_version: Nesta::Plugin::Drop::VERSION }
   = Yajl::Parser.parse json
  ["uid"] && ["token"] && ["domain"]
end

.nestadrop_synced?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/nesta-plugin-drop/client.rb', line 34

def self.nestadrop_synced?
  File.exists?("/tmp/.nestadropped")
end

.remove_file(file) ⇒ Object



121
122
123
124
125
126
# File 'lib/nesta-plugin-drop/client.rb', line 121

def self.remove_file(file)
  local_path = [Nesta::App.root, file].join("/")
  Nesta::Plugin::Drop.logger.debug "NESTADROP: Removing locally cached file at '#{local_path}'."
  FileUtils.rm_r(File.dirname(local_path), secure: true)
  bounce_server!
end

.syncing?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/nesta-plugin-drop/client.rb', line 28

def self.syncing?
  lock.synchronize do
    @syncing
  end
end

.uncached_filesObject



72
73
74
# File 'lib/nesta-plugin-drop/client.rb', line 72

def self.uncached_files
  @uncached_files
end

.uncached_files=(val) ⇒ Object



76
77
78
79
80
81
# File 'lib/nesta-plugin-drop/client.rb', line 76

def self.uncached_files=(val)
  lock.synchronize do
    @uncached_files ||= val
  end
  @uncached_files
end

.userinfoObject



12
13
14
# File 'lib/nesta-plugin-drop/client.rb', line 12

def self.userinfo
  URI.parse(host).userinfo.split(":")
end