Class: RubyBox::Folder

Inherits:
Item
  • Object
show all
Defined in:
lib/ruby-box/folder.rb

Instance Method Summary collapse

Methods inherited from Item

#create, #delete, has_many, has_many_paginated, #initialize, #method_missing, #reload_meta, #update

Constructor Details

This class inherits a constructor from RubyBox::Item

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RubyBox::Item

Instance Method Details

#create_collaboration(email, role = :viewer) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ruby-box/folder.rb', line 44

def create_collaboration(email, role=:viewer)
  collaboration = RubyBox::Collaboration.new(@session, {
      'item' => {'id' => id, 'type' => type},
      'accessible_by' => {'login' => email},
      'role' => role.to_s
  })

  collaboration.create
end

#create_subfolder(name) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ruby-box/folder.rb', line 33

def create_subfolder(name)
  url = "#{RubyBox::API_URL}/#{resource_name}"
  uri = URI.parse(url)
  request = Net::HTTP::Post.new( uri.request_uri )
  request.body = JSON.dump({ "name" => name, "parent" => {"id" => id} })
  resp = @session.request(uri, request)
  RubyBox::Folder.new(@session, resp)
end

#files(name = nil, item_limit = 100, offset = 0, fields = nil) ⇒ Object



8
9
10
# File 'lib/ruby-box/folder.rb', line 8

def files(name=nil, item_limit=100, offset=0, fields=nil)
  items_by_type(RubyBox::File, name, item_limit, offset, fields)
end

#folders(name = nil, item_limit = 100, offset = 0, fields = nil) ⇒ Object



12
13
14
# File 'lib/ruby-box/folder.rb', line 12

def folders(name=nil, item_limit=100, offset=0, fields=nil)
  items_by_type(RubyBox::Folder, name, item_limit, offset, fields)
end

#upload_file(filename, data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby-box/folder.rb', line 16

def upload_file(filename, data)
  file = RubyBox::File.new(@session, {
    'name' => filename,
    'parent' => RubyBox::User.new(@session, {'id' => id})
  })

  begin
    resp = file.upload_content(data) #write a new file. If there is a conflict, update the conflicted file.
  rescue RubyBox::ItemNameInUse => e
    file = RubyBox::File.new(@session, {
      'id' => e['context_info']['conflicts'][0]['id']
    })
    data.rewind
    resp = file.update_content( data )
  end
end