lib/ruby-box/client.rb in ruby-box-1.11.1 vs lib/ruby-box/client.rb in ruby-box-1.12.1
- old
+ new
@@ -10,20 +10,29 @@
def initialize(session)
@session = session
end
def root_folder
- folder = Folder.new(@session, {'id' => '0'})
+ folder_by_id('0')
+ end
+
+ def folder_by_id(id)
+ folder = Folder.new(@session, {'id' => id})
folder.reload_meta
end
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
+ def file_by_id(id)
+ file = File.new(@session, {'id' => id})
+ file.reload_meta
+ end
+
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
@@ -86,18 +95,19 @@
folder = create_folder(path.join('/'))
folder.upload_file(file_name, data, overwrite) if folder
end
def upload_file(local_path, remote_path, overwrite=true)
- file_name = local_path.split('/').pop
folder = create_folder( remote_path )
- return unless folder
- ::File.open( local_path ) do |data|
- folder.upload_file(file_name, data, overwrite)
- end
+ upload_file_to_folder(local_path, folder, overwrite)
end
+ 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
+
def split_path(path)
path.gsub!(/(^\/)|(\/$)/, '')
path.split('/')
end
@@ -111,12 +121,28 @@
def me
resp = @session.get( "#{RubyBox::API_URL}/users/me" )
User.new(@session, resp)
end
+ 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
+ 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
+
def folder_from_split_path(path)
folder = root_folder
path.each do |folder_name|
folder = folder.folders(folder_name).first
return nil unless folder
@@ -126,10 +152,10 @@
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].include?(stream_type) ? stream_type : :all
+ 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
end