class BrowseEverything::Driver::Box
Driver
for accessing the Box
API (www.box.com/home)
Constants
- ITEM_LIMIT
Attributes
Public Class Methods
# File lib/browse_everything/driver/box.rb, line 15 def default_authentication_klass RubyBox::Session end
Constructor @param config_values [Hash] configuration for the driver
BrowseEverything::Driver::Base::new
# 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
Public Instance Methods
@return [String] Authorization url that is used to request the initial access code from Box
# File lib/browse_everything/driver/box.rb, line 63 def auth_link(*_args) box_session.authorize_url(callback.to_s) end
@return [Hash] Gets the appropriate tokens from Box
using the access code returned from :auth_link:
# 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
Retrieves the file entry objects for a given path to Box
resource @param [String] id of the file or folder in Box
@return [Array<BrowseEverything::FileEntry>]
# 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
# File lib/browse_everything/driver/box.rb, line 27 def icon 'cloud' end
@param [String] id of the file in Box
@return [Array<String, Hash>]
# 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
# 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
Private Instance Methods
# File lib/browse_everything/driver/box.rb, line 103 def authenticate session.authenticate end
# File lib/browse_everything/driver/box.rb, line 85 def box_client if token_expired? session = box_session register_access_token(session.refresh_token(box_refresh_token)) end RubyBox::Client.new(box_session) end
# File lib/browse_everything/driver/box.rb, line 129 def box_refresh_token return unless @token @token.fetch('refresh_token', nil) end
# File lib/browse_everything/driver/box.rb, line 107 def box_session authenticate end
# File lib/browse_everything/driver/box.rb, line 124 def box_token return unless @token @token.fetch('token', nil) end
Constructs a BrowseEverything::FileEntry
object for a Box
file resource @param file [String] ID to the file resource @return [BrowseEverything::File]
# File lib/browse_everything/driver/box.rb, line 143 def directory_entry(file) BrowseEverything::FileEntry.new(file.id, "#{key}:#{file.id}", file.name, file.size, file.created_at, file.type == 'folder') end
# File lib/browse_everything/driver/box.rb, line 134 def expiration_time return unless @token @token.fetch('expires_at', nil).to_i end
If there is an active session, {@token} will be set by {BrowseEverythingController} using data stored in the session. However, if there is no prior session, or the token has expired, we reset it here using # a new access_token received from {#box_session}.
@param [OAuth2::AccessToken] access_token
# File lib/browse_everything/driver/box.rb, line 116 def register_access_token(access_token) @token = { 'token' => access_token.token, 'refresh_token' => access_token.refresh_token, 'expires_at' => access_token.expires_at } end
# File lib/browse_everything/driver/box.rb, line 93 def session AuthenticationFactory.new( self.class.authentication_klass, client_id: config[:client_id], client_secret: config[:client_secret], access_token: box_token, refresh_token: box_refresh_token ) end
# File lib/browse_everything/driver/box.rb, line 80 def token_expired? return true if expiration_time.nil? Time.now.to_i > expiration_time end