Class: BoxCli::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/box_cli/wrapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, username, password) ⇒ Wrapper

Returns a new instance of Wrapper.



40
41
42
# File 'lib/box_cli/wrapper.rb', line 40

def initialize(api_key, username, password)
  @api_key, @username, @password = api_key, username, password
end

Class Method Details

.auth_token_manager_classObject



48
49
50
# File 'lib/box_cli/wrapper.rb', line 48

def self.auth_token_manager_class
  @auth_token_manager_class || AuthTokenManager
end

.auth_token_manager_class=(auth_token_manager_class) ⇒ Object



44
45
46
# File 'lib/box_cli/wrapper.rb', line 44

def self.auth_token_manager_class=(auth_token_manager_class)
  @auth_token_manager_class = auth_token_manager_class
end

Instance Method Details

#account_infoObject



60
61
62
# File 'lib/box_cli/wrapper.rb', line 60

def 
  api.
end

#auth_token_managerObject



56
57
58
# File 'lib/box_cli/wrapper.rb', line 56

def auth_token_manager
  @auth_token_manager || self.class.auth_token_manager_class.new(@username)
end

#auth_token_manager=(auth_token_manager) ⇒ Object



52
53
54
# File 'lib/box_cli/wrapper.rb', line 52

def auth_token_manager=(auth_token_manager)
  @auth_token_manager = auth_token_manager
end

#create_folder(folder_name) ⇒ Object



64
65
66
67
68
69
# File 'lib/box_cli/wrapper.rb', line 64

def create_folder(folder_name)
  folder = root.create(folder_name)
  folder.id
rescue Box::Api::NameTaken => e
  raise FolderNameTaken.new("Folder '#{folder_name}' already exists")
end

#delete(remote_path) ⇒ Object

Raises:



71
72
73
74
75
# File 'lib/box_cli/wrapper.rb', line 71

def delete(remote_path)
  item = root.at(remote_path)
  raise NotFound.new("Nothing found at '#{remote_path}'") unless item
  api.delete(item.type, item.id)
end

#info(remote_path) ⇒ Object

Raises:



77
78
79
80
81
# File 'lib/box_cli/wrapper.rb', line 77

def info(remote_path)
  item = root.at(remote_path)
  raise NotFound.new("Nothing found at '#{remote_path}'") unless item
  ActiveSupport::OrderedHash[:type, item.type, :data, item.data]
end

#root_folder_idObject



104
105
106
# File 'lib/box_cli/wrapper.rb', line 104

def root_folder_id
  root.id
end

#upload(local_path, remote_path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/box_cli/wrapper.rb', line 83

def upload(local_path, remote_path)
  target_folder = root.at(remote_path)
  basename = File.basename(local_path)
  target_path = File.join(remote_path, basename)
  if item = root.at(target_path)
    raise FileNameTaken.new("File '#{basename}' already exists at #{remote_path}")
  else
    api.upload(local_path, target_folder.id)
  end
end

#upload_or_overwrite(local_path, remote_path) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/box_cli/wrapper.rb', line 94

def upload_or_overwrite(local_path, remote_path)
  target_folder = root.at(remote_path)
  target_path = File.join(remote_path, File.basename(local_path))
  if item = root.at(target_path)
    api.overwrite(local_path, item.id)
  else
    api.upload(local_path, target_folder.id)
  end
end