Class: BrowseEverything::Driver::Dropbox

Inherits:
Base
  • Object
show all
Defined in:
lib/browse_everything/driver/dropbox.rb

Defined Under Namespace

Classes: FileEntryFactory, FileFactory, ResourceFactory

Class Attribute Summary collapse

Attributes inherited from Base

#code, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#config, default_sorter, inherited, #key, #name

Constructor Details

#initialize(config_values) ⇒ Dropbox

Constructor

Parameters:

  • config_values (Hash)

    configuration for the driver



68
69
70
71
72
# 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

Class Attribute Details

.authentication_klassObject

Returns the value of attribute authentication_klass.



59
60
61
# File 'lib/browse_everything/driver/dropbox.rb', line 59

def authentication_klass
  @authentication_klass
end

Class Method Details

.default_authentication_klassObject



61
62
63
# File 'lib/browse_everything/driver/dropbox.rb', line 61

def default_authentication_klass
  DropboxApi::Authenticator
end

Instance Method Details



126
127
128
# 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

#authorized?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/browse_everything/driver/dropbox.rb', line 138

def authorized?
  token.present?
end

#connect(params, _data, url_options) ⇒ Object



130
131
132
133
134
135
136
# 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 = '') ⇒ Object



83
84
85
86
87
88
89
# 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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object



109
110
111
112
113
114
115
116
# 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

#iconObject



74
75
76
# File 'lib/browse_everything/driver/dropbox.rb', line 74

def icon
  'dropbox'
end


118
119
120
121
122
123
124
# 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) ⇒ Object



104
105
106
107
# 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_configObject



78
79
80
81
# 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