class RubyBox::Client

Public Class Methods

new(session) click to toggle source
# File lib/ruby-box/client.rb, line 10
def initialize(session)
  @session = session
end

Public Instance Methods

create_folder(path) click to toggle source
# File lib/ruby-box/client.rb, line 75
def create_folder(path)
  folder = root_folder
  folder_names = split_path(path)
  folder_names.each do |folder_name|
    new_folder = folder.folders(folder_name).first        
    if !new_folder
      begin
        new_folder = folder.create_subfolder(folder_name)
      rescue RubyBox::ItemNameInUse => e
        new_folder = folder.folders(folder_name).first
      end
    end
    folder = new_folder
  end
  folder
end
download(path) click to toggle source
# File lib/ruby-box/client.rb, line 51
def download(path)
  file = file(path)
  file.download if file
end
event_response(stream_position=0, stream_type=:all, limit=100) click to toggle source
# File lib/ruby-box/client.rb, line 114
def event_response(stream_position=0, stream_type=:all, limit=100)
  q = fmt_events_args stream_position, stream_type, limit
  url = "#{RubyBox::API_URL}/events?#{q}"
  resp = @session.get( url )
  EventResponse.new(@session, resp)
end
file(path) click to toggle source
# File lib/ruby-box/client.rb, line 34
def file(path)
  path = split_path( path.sub(/^\.\//, '') )
  file_name = path.pop
  folder = folder_from_split_path( path )
  folder.files(file_name).first if folder
end
file_by_id(id) click to toggle source
# File lib/ruby-box/client.rb, line 29
def file_by_id(id)
  file = File.new(@session, {'id' => id})
  file.reload_meta
end
folder(path='/') click to toggle source
# File lib/ruby-box/client.rb, line 23
def folder(path='/')
  path = path.sub(/(^\.$)|(^\.\/)/, '') if path # handle folders with leading '.'
  return root_folder if ['', '/'].member?(path)
  folder_from_split_path( split_path(path) )
end
folder_by_id(id) click to toggle source
# File lib/ruby-box/client.rb, line 18
def folder_by_id(id)
  folder = Folder.new(@session, {'id' => id})
  folder.reload_meta
end
item(path) click to toggle source
# File lib/ruby-box/client.rb, line 41
def item(path)
  path = split_path( path.sub(/^\.\//, '') )
  item_name = path.pop
  folder = folder_from_split_path( path )

  folder.items.select do |item|
    item.instance_variable_get(:@raw_item)['name'] and item.name == item_name
  end.first
end
me() click to toggle source
# File lib/ruby-box/client.rb, line 121
def me
  resp = @session.get( "#{RubyBox::API_URL}/users/me" )
  User.new(@session, resp)
end
root_folder() click to toggle source
# File lib/ruby-box/client.rb, line 14
def root_folder
  folder_by_id('0')
end
split_path(path) click to toggle source
# File lib/ruby-box/client.rb, line 109
def split_path(path)
  path.gsub!(/(^\/)|(\/$)/, '')
  path.split('/')
end
stream(path, opts={}) click to toggle source
# File lib/ruby-box/client.rb, line 56
def stream(path, opts={})
  file = file(path)
  file.stream(opts) if file
end
upload_data(path, data, overwrite=true) click to toggle source
# File lib/ruby-box/client.rb, line 92
def upload_data(path, data, overwrite=true)
  path = split_path(path)
  file_name = path.pop
  folder = create_folder(path.join('/'))
  folder.upload_file(file_name, data, overwrite) if folder
end
upload_file(local_path, remote_path, overwrite=true) click to toggle source
# File lib/ruby-box/client.rb, line 99
def upload_file(local_path, remote_path, overwrite=true)
  folder = create_folder( remote_path )
  upload_file_to_folder(local_path, folder, overwrite)
end
upload_file_by_folder_id(local_path, folder_id, overwrite=true) click to toggle source
# File lib/ruby-box/client.rb, line 104
def upload_file_by_folder_id(local_path, folder_id, overwrite=true)
  folder = folder_by_id(folder_id)
  upload_file_to_folder(local_path, folder, overwrite)
end
users(filter_term = "", limit = 100, offset = 0) click to toggle source
# File lib/ruby-box/client.rb, line 126
def users(filter_term = "", limit = 100, offset = 0)
  url = "#{RubyBox::API_URL}/users?filter_term=#{URI::encode(filter_term)}&limit=#{limit}&offset=#{offset}"
  resp = @session.get( url )
  resp['entries'].map do |entry|
    RubyBox::Item.factory(@session, entry)
  end
end

Private Instance Methods

fmt_events_args(stream_position, stream_type, limit) click to toggle source
# File lib/ruby-box/client.rb, line 153
def fmt_events_args(stream_position, stream_type, limit)
  unless stream_position.to_s == 'now'
    stream_position = stream_position.kind_of?(Numeric) ? stream_position : 0
  end
  stream_type = [:all, :changes, :sync, :admin_logs].include?(stream_type) ? stream_type : :all
  limit = limit.kind_of?(Fixnum) ? limit : 100
  "stream_position=#{stream_position}&stream_type=#{stream_type}&limit=#{limit}"
end
folder_from_split_path(path) click to toggle source
# File lib/ruby-box/client.rb, line 144
def folder_from_split_path(path)
  folder = root_folder
  path.each do |folder_name|
    folder = folder.folders(folder_name).first
    return nil unless folder
  end
  folder
end
upload_file_to_folder(local_path, folder, overwrite) click to toggle source
# File lib/ruby-box/client.rb, line 136
def upload_file_to_folder(local_path, folder, overwrite)
  file_name = local_path.split('/').pop
  return unless folder
  ::File.open(local_path, 'rb') do |data|
    folder.upload_file(file_name, data, overwrite)
  end
end