Class: RubyBox::Client
- Inherits:
-
Object
- Object
- RubyBox::Client
- Defined in:
- lib/ruby-box/client.rb
Instance Method Summary collapse
- #create_folder(path) ⇒ Object
- #download(path) ⇒ Object
- #event_response(stream_position = 0, stream_type = :all, limit = 100) ⇒ Object
- #file(path) ⇒ Object
- #folder(path = '/') ⇒ Object
-
#initialize(session) ⇒ Client
constructor
A new instance of Client.
- #me ⇒ Object
- #root_folder ⇒ Object
- #search(query, item_limit = 100, offset = 0) ⇒ Object
- #split_path(path) ⇒ Object
- #stream(path, opts = {}) ⇒ Object
- #upload_data(path, data) ⇒ Object
- #upload_file(local_path, remote_path) ⇒ Object
Constructor Details
#initialize(session) ⇒ Client
Returns a new instance of Client.
10 11 12 |
# File 'lib/ruby-box/client.rb', line 10 def initialize(session) @session = session end |
Instance Method Details
#create_folder(path) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ruby-box/client.rb', line 56 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) ⇒ Object
32 33 34 35 |
# File 'lib/ruby-box/client.rb', line 32 def download(path) file = file(path) file.download if file end |
#event_response(stream_position = 0, stream_type = :all, limit = 100) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/ruby-box/client.rb', line 94 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) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/ruby-box/client.rb', line 25 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 |
#folder(path = '/') ⇒ Object
19 20 21 22 23 |
# File 'lib/ruby-box/client.rb', line 19 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 |
#me ⇒ Object
101 102 103 104 |
# File 'lib/ruby-box/client.rb', line 101 def me resp = @session.get( "#{RubyBox::API_URL}/users/me" ) User.new(@session, resp) end |
#root_folder ⇒ Object
14 15 16 17 |
# File 'lib/ruby-box/client.rb', line 14 def root_folder folder = Folder.new(@session, {'id' => '0'}) folder. end |
#search(query, item_limit = 100, offset = 0) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby-box/client.rb', line 42 def search(query, item_limit=100, offset=0) Enumerator.new do |yielder| while true url = "#{RubyBox::API_URL}/search?query=#{URI::encode(query)}&limit=#{item_limit}&offset=#{offset}" resp = @session.get( url ) resp['entries'].each do |entry| yielder.yield(RubyBox::Item.factory(@session, entry)) end offset += resp['entries'].count break if resp['offset'].to_i + resp['limit'].to_i >= resp['total_count'].to_i end end end |
#split_path(path) ⇒ Object
89 90 91 92 |
# File 'lib/ruby-box/client.rb', line 89 def split_path(path) path.gsub!(/(^\/)|(\/$)/, '') path.split('/') end |
#stream(path, opts = {}) ⇒ Object
37 38 39 40 |
# File 'lib/ruby-box/client.rb', line 37 def stream(path, opts={}) file = file(path) file.stream(opts) if file end |
#upload_data(path, data) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/ruby-box/client.rb', line 73 def upload_data(path, data) path = split_path(path) file_name = path.pop folder = create_folder(path.join('/')) folder.upload_file(file_name, data) if folder end |
#upload_file(local_path, remote_path) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/ruby-box/client.rb', line 80 def upload_file(local_path, remote_path) 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) end end |