Class: RubyBox::Folder

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

Instance Method Summary collapse

Methods inherited from Item

#as_json, #create, #create_shared_link, #delete, #disable_shared_link, has_many, has_many_paginated, #initialize, #method_missing, #move_to, #reload_meta, #shared_link, #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

#copy_to(destination, name = nil) ⇒ Object

see developers.box.com/docs/#folders-copy-a-folder for a description of the behavior



72
73
74
75
76
77
# File 'lib/ruby-box/folder.rb', line 72

def copy_to(destination, name=nil)
  parent = {'parent' => {'id' => destination.id}}
  parent.merge!('name' => name) if name

  RubyBox::Folder.new(@session, post(folder_method(:copy), parent))
end

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



62
63
64
65
66
67
68
# File 'lib/ruby-box/folder.rb', line 62

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

#create_subfolder(name) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/ruby-box/folder.rb', line 51

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, overwrite = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby-box/folder.rb', line 16

def upload_file(filename, data, overwrite=true)
  file = RubyBox::File.new(@session, {
    'name' => filename,
    'parent' => RubyBox::Folder.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
    
    # if overwrite flag is false, simply raise exception.
    raise e unless overwrite

    # otherwise let's attempt to overwrite the file.
    data.rewind

    # The Box API occasionally does not return
    # context info for an ItemNameInUse exception.
    # This is a workaround around:
    begin
      # were were given context information about this conflict?
      file = RubyBox::File.new(@session, {
        'id' => e['context_info']['conflicts'][0]['id']
      })
    rescue
      # we were not given context information about this conflict.
      # attempt to lookup the file.
      file = files(filename, 1000).pop
    end

    raise e unless file # re-raise the ItemNameInUse exception.
    resp = file.update_content( data )
  end
end