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
auth_link(url_options)
click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 126 def auth_link(url_options) authenticator.auth_code.authorize_url redirect_uri: redirect_uri(url_options) 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) built_redirect_uri = redirect_uri(url_options) token_code = params[:code] auth_bearer = authenticator.auth_code.get_token(token_code, redirect_uri: built_redirect_uri) 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
link_for(path)
click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 118 def link_for(path) uri = uri_for(path) file_name = File.basename(path) file_size = file_size_for(path) [uri, { file_name: file_name, file_size: file_size }] 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 152 def authenticate session.authenticate end
authenticator()
click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 156 def authenticator @authenticator ||= authenticate end
client()
click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 160 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 171 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 178 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 164 def redirect_uri(url_options) connector_response_url(**url_options) end
session()
click to toggle source
# File lib/browse_everything/driver/dropbox.rb, line 144 def session AuthenticationFactory.new( self.class.authentication_klass, config[:client_id], config[:client_secret] ) end