class Nesta::Plugin::ContentFocus::Client

Public Class Methods

bootstrap!() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 128
def self.bootstrap!
  Nesta::Plugin::ContentFocus.logger.debug "CONTENTFOCUS: Bootstrapping local instance..."
  unless contentfocus_synced?
    Thread.new do
      cache_files
    end
  end
end
bounce_server!() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 47
def self.bounce_server!
  return if syncing?
  Nesta::Plugin::ContentFocus.logger.info "CONTENTFOCUS: Purging nesta file cache."
  Nesta::FileModel.purge_cache
  Nesta::Plugin::ContentFocus.logger.info "CONTENTFOCUS: Restarting server..."
  unless system("bundle exec pumactl -S /tmp/.app_state phased-restart")
    Thread.new do
      Nesta::Plugin::ContentFocus.logger.info "CONTENTFOCUS: Waiting for server to load before restarting."
      sleep(3)
      bounce_server!
    end
  end
end
cache_file(file) click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 83
def self.cache_file(file)
  confirm_synced!
  local_path = [Nesta::App.root, file].join("/")
  Nesta::Plugin::ContentFocus.logger.debug "CONTENTFOCUS: 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::ContentFocus.logger.debug "CONTENTFOCUS: Cached '#{local_path}'."
  bounce_server!
rescue RuntimeError => ex
  puts ex
end
cache_files() click to toggle source
# File lib/nesta-plugin-contentfocus/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::ContentFocus.logger.debug "CONTENTFOCUS: 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::ContentFocus.logger.debug "CONTENTFOCUS: Worker thread complete."
  end
  threads.each(&:join)
  @syncing = false
  bounce_server!
end
confirm_synced!() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 20
def self.confirm_synced!
  return true if contentfocus_synced?
  Nesta::Plugin::ContentFocus.logger.debug "CONTENTFOCUS: Syncing with Dropbox filesystem."
  File.open("/tmp/.contentfocus", "w+") do |f|
    f.write "synced"
  end
end
contentfocus_configured?() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 38
def self.contentfocus_configured?
  return true if contentfocus_synced?
  Nesta::Plugin::ContentFocus.logger.debug "CONTENTFOCUS: Checking if account is linked to Dropbox."
  json = RestClient.get "#{host}account", {
    accept: :json, x_contentfocus_version: Nesta::Plugin::ContentFocus::VERSION }
  account = Yajl::Parser.parse json
  account["uid"] && account["token"] && account["domain"]
end
contentfocus_synced?() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 34
def self.contentfocus_synced?
  File.exists?("/tmp/.contentfocus")
end
files() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 61
def self.files
  lock.synchronize do
    Nesta::Plugin::ContentFocus.logger.debug "CONTENTFOCUS: Retrieving file list..."
    @files ||= Yajl::Parser.parse(RestClient.get "#{host}files", {
      accept: :json, x_contentfocus_version: Nesta::Plugin::ContentFocus::VERSION })
  end
  @files
rescue RestClient::Unauthorized
  return []
end
host() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 8
def self.host
  ENV["CONTENTFOCUS_URL"]
end
lock() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 16
def self.lock
  @lock ||= Mutex.new
end
remove_file(file) click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 121
def self.remove_file(file)
  local_path = [Nesta::App.root, file].join("/")
  Nesta::Plugin::ContentFocus.logger.debug "CONTENTFOCUS: Removing locally cached file at '#{local_path}'."
  FileUtils.rm_r(File.dirname(local_path), secure: true)
  bounce_server!
end
syncing?() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 28
def self.syncing?
  lock.synchronize do
    @syncing
  end
end
uncached_files() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 72
def self.uncached_files
  @uncached_files
end
uncached_files=(val) click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 76
def self.uncached_files=(val)
  lock.synchronize do
    @uncached_files ||= val
  end
  @uncached_files
end
userinfo() click to toggle source
# File lib/nesta-plugin-contentfocus/client.rb, line 12
def self.userinfo
  URI.parse(host).userinfo.split(":")
end