Class: BrowseEverything::Driver::Box

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

Overview

Driver for accessing the Box API (www.box.com/home)

Constant Summary collapse

ITEM_LIMIT =
99999

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) ⇒ Box

Constructor

Parameters:

  • config_values (Hash)

    configuration for the driver



22
23
24
25
# File 'lib/browse_everything/driver/box.rb', line 22

def initialize(config_values)
  self.class.authentication_klass ||= self.class.default_authentication_klass
  super(config_values)
end

Class Attribute Details

.authentication_klassObject

Returns the value of attribute authentication_klass.



13
14
15
# File 'lib/browse_everything/driver/box.rb', line 13

def authentication_klass
  @authentication_klass
end

Class Method Details

.default_authentication_klassObject



15
16
17
# File 'lib/browse_everything/driver/box.rb', line 15

def default_authentication_klass
  RubyBox::Session
end

Instance Method Details

Authorization url that is used to request the initial access code from Box

Returns:

  • (String)


63
64
65
# File 'lib/browse_everything/driver/box.rb', line 63

def auth_link(*_args)
  box_session.authorize_url(callback.to_s)
end

#authorized?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/browse_everything/driver/box.rb', line 68

def authorized?
  box_token.present? && box_refresh_token.present? && !token_expired?
end

#connect(params, _data, _url_options) ⇒ Hash

Gets the appropriate tokens from Box using the access code returned from :auth_link:

Returns:

  • (Hash)


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

def connect(params, _data, _url_options)
  register_access_token(box_session.get_access_token(params[:code]))
end

#contents(id = '') ⇒ Array<BrowseEverything::FileEntry>

Retrieves the file entry objects for a given path to Box resource

Parameters:

  • id (String) (defaults to: '')

    of the file or folder in Box

Returns:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/browse_everything/driver/box.rb', line 39

def contents(id = '')
  folder = id.empty? ? box_client.root_folder : box_client.folder_by_id(id)
  values = []

  folder.items(ITEM_LIMIT, 0, %w[name size created_at]).collect do |f|
    values << directory_entry(f)
  end
  @entries = values.compact

  @sorter.call(@entries)
end

#iconObject



27
28
29
# File 'lib/browse_everything/driver/box.rb', line 27

def icon
  'cloud'
end

Parameters:

  • id (String)

    of the file in Box

Returns:

  • (Array<String, Hash>)


53
54
55
56
57
58
59
# File 'lib/browse_everything/driver/box.rb', line 53

def link_for(id)
  file = box_client.file_by_id(id)
  download_url = file.download_url
  auth_header = { 'Authorization' => "Bearer #{@token}" }
  extras = { auth_header: auth_header, expires: 1.hour.from_now, file_name: file.name, file_size: file.size.to_i }
  [download_url, extras]
end

#validate_configObject



31
32
33
34
# File 'lib/browse_everything/driver/box.rb', line 31

def validate_config
  return if config[:client_id] && config[:client_secret]
  raise InitializationError, 'Box driver requires both :client_id and :client_secret argument'
end