Class: Box::Folder

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

Constant Summary collapse

LIMIT =
1000

Instance Attribute Summary

Attributes inherited from Item

#client, #metadata

Instance Method Summary collapse

Methods inherited from Item

#file?, find, #folder?, #initialize, type

Constructor Details

This class inherits a constructor from Box::Item

Instance Method Details

#find_or_create_subfolder(folder_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/box/folder.rb', line 21

def find_or_create_subfolder(folder_name)
  folder = subfolder(folder_name)
  return folder unless folder.nil?

  Box.log "Creating subfolder in #{self.name} for #{folder_name}"
  response = @client.post('folders', {name: folder_name, parent:{id: self.id}})

  if response.status == 201 # created
    folder = Box::Folder.new(@client, response.body)
    Box.log "Created folder for #{folder_name} in #{name} as #{folder.id}"
    folder
  else
    Box.log "Error creating folder, #{response.body}"
    nil
  end

end

#foldersObject



61
62
63
# File 'lib/box/folder.rb', line 61

def folders
  items.select {|item| item.type == 'folder' }
end

#has_item?(name) ⇒ Boolean

Check to see if an item of the same name in the folder

Returns:

  • (Boolean)


11
12
13
# File 'lib/box/folder.rb', line 11

def has_item?(name)
  items.find {|item| item.name == name}
end

#items(params = {}, collection = []) ⇒ Object

Warning: This gets on all files for a directory with no limit by recursively calling itself until it reaches the limit



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/box/folder.rb', line 41

def items(params = {}, collection = [])
  # Format params defaults
  params = {fields: 'sha1,name,path_collection,size', limit: LIMIT, offset: 0}.merge(params)
  # Add expected fields and limit
  response = @client.get("/folders/#{id}/items", params)

  # Add the results to the total collection
  collection.push *@client.parse_items(response.body)

  total_count = response.body['total_count']
  offset      = (LIMIT * (params[:offset] + 1))

  if total_count > offset
    Box.log "Recursively calling for items in folder #{name} - #{LIMIT}, #{offset}, #{total_count}"
    return self.items({offset: offset}, collection)
  end

  collection
end

#load_info!Object



6
7
8
# File 'lib/box/folder.rb', line 6

def load_info!
  @client.get("/folders/#{id}")
end

#subfolder(folder_name) ⇒ Object



15
16
17
18
19
# File 'lib/box/folder.rb', line 15

def subfolder(folder_name)
  folders = items.select {|item| item.folder? and item.name == folder_name}
  return nil if folders.empty?
  folders.first
end