class BrowseEverything::Driver::Dropbox

Attributes

authentication_klass[RW]

Public Class Methods

default_authentication_klass() click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 61
def default_authentication_klass
  DropboxApi::Authenticator
end
new(config_values) click to toggle source

Constructor @param config_values [Hash] configuration for the driver

Calls superclass method BrowseEverything::Driver::Base::new
# File lib/browse_everything/driver/dropbox.rb, line 68
def initialize(config_values)
  self.class.authentication_klass ||= self.class.default_authentication_klass
  @downloaded_files = {}
  super(config_values)
end

Public Instance Methods

authorized?() click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 135
def authorized?
  token.present?
end
connect(params, _data, url_options) click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 130
def connect(params, _data, url_options)
  auth_bearer = authenticator.get_token params[:code], redirect_uri: redirect_uri(url_options)
  self.token = auth_bearer.token
end
contents(path = '') click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 83
def contents(path = '')
  path = '/' + path unless path == ''
  response = client.list_folder(path)
  values = response.entries.map { |entry| FileEntryFactory.build(metadata: entry, key: key) }
  @entries = values.compact
  @sorter.call(@entries)
end
downloaded_file_for(path) click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 91
def downloaded_file_for(path)
  return @downloaded_files[path] if @downloaded_files.key?(path)

  # This ensures that the name of the file its extension are preserved for user downloads
  temp_file_path = File.join(download_directory_path, File.basename(path))
  temp_file = File.open(temp_file_path, mode: 'w+', encoding: 'ascii-8bit')
  client.download(path) do |chunk|
    temp_file.write chunk
  end
  temp_file.close
  @downloaded_files[path] = temp_file
end
file_size_for(path) click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 109
def file_size_for(path)
  downloaded_file = downloaded_file_for(path)
  size = File.size(downloaded_file.path)
  size.to_i
rescue StandardError => error
  Rails.logger.error "Failed to find the file size for #{path}: #{error}"
  0
end
icon() click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 74
def icon
  'dropbox'
end
uri_for(path) click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 104
def uri_for(path)
  temp_file = downloaded_file_for(path)
  "file://#{temp_file.path}"
end
validate_config() click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 78
def validate_config
  raise InitializationError, 'Dropbox driver requires a :client_id argument' unless config[:client_id]
  raise InitializationError, 'Dropbox driver requires a :client_secret argument' unless config[:client_secret]
end

Private Instance Methods

authenticate() click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 149
def authenticate
  session.authenticate
end
authenticator() click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 153
def authenticator
  @authenticator ||= authenticate
end
client() click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 157
def client
  DropboxApi::Client.new(token)
end
default_download_directory() click to toggle source

Ensures that the “tmp” directory is used if there is no default download directory specified in the configuration @return [String]

# File lib/browse_everything/driver/dropbox.rb, line 168
def default_download_directory
  Rails.root.join('tmp')
end
download_directory_path() click to toggle source

Retrieves the directory path for downloads used when retrieving the resource from Dropbox @return [String]

# File lib/browse_everything/driver/dropbox.rb, line 175
def download_directory_path
  dir_path = config[:download_directory] || default_download_directory
  File.expand_path(dir_path)
end
redirect_uri(url_options) click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 161
def redirect_uri(url_options)
  connector_response_url(**url_options)
end
session() click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 141
def session
  AuthenticationFactory.new(
    self.class.authentication_klass,
    config[:client_id],
    config[:client_secret]
  )
end