class BrowseEverything::Driver::Box

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

Constants

ITEM_LIMIT

Attributes

authentication_klass[RW]

Public Class Methods

default_authentication_klass() click to toggle source
# File lib/browse_everything/driver/box.rb, line 15
def default_authentication_klass
  RubyBox::Session
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/box.rb, line 22
def initialize(config_values)
  self.class.authentication_klass ||= self.class.default_authentication_klass
  super(config_values)
end

Public Instance Methods

authorized?() click to toggle source

@return [Boolean]

# 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) click to toggle source

@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
contents(id = '') click to toggle source

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
icon() click to toggle source
# File lib/browse_everything/driver/box.rb, line 27
def icon
  'cloud'
end
validate_config() click to toggle source
# 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

authenticate() click to toggle source
# File lib/browse_everything/driver/box.rb, line 103
def authenticate
  session.authenticate
end
box_client() click to toggle source
# 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
box_refresh_token() click to toggle source
# File lib/browse_everything/driver/box.rb, line 129
def box_refresh_token
  return unless @token
  @token.fetch('refresh_token', nil)
end
box_session() click to toggle source
# File lib/browse_everything/driver/box.rb, line 107
def box_session
  authenticate
end
box_token() click to toggle source
# File lib/browse_everything/driver/box.rb, line 124
def box_token
  return unless @token
  @token.fetch('token', nil)
end
directory_entry(file) click to toggle source

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
expiration_time() click to toggle source
# File lib/browse_everything/driver/box.rb, line 134
def expiration_time
  return unless @token
  @token.fetch('expires_at', nil).to_i
end
register_access_token(access_token) click to toggle source

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
session() click to toggle source
# 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
token_expired?() click to toggle source
# 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