Class: Box::File

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

Overview

Represents a file stored on Box. Any attributes or actions typical to a Box file can be accessed through this class.

Instance Attribute Summary

Attributes inherited from Item

api, data, parent

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Item

#copy, #delete, #force_cached_info, #id, #info, #initialize, #method_missing, #move, #path, #rename, #respond_to?, #set_description, #share_private, #share_public, #type, #types, types, #unshare

Constructor Details

This class inherits a constructor from Box::Item

Dynamic Method Handling

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

Class Method Details

+ (String) type

The string representation of this item.

Returns:

  • (String)

    The string representation of this item.



9
# File 'lib/box/file.rb', line 9

def self.type; 'file'; end

Instance Method Details

- (Comment) add_comment(message)

Add a comment to the file.

Returns:

  • (Comment)

    The created comment.



57
58
59
60
61
# File 'lib/box/file.rb', line 57

def add_comment(message)
  response = @api.add_comment(type, id, message)

  Comment.create(@api, response['comment']).first
end

- (Object) download(path)

Download this file to the specified path.

Parameters:

  • path (String)

    The path to write the file.



14
15
16
# File 'lib/box/file.rb', line 14

def download(path)
  @api.download(path, id)
end

- (String) embed_code(options = Hash.new)

Note:

This function will share the file if it is not already shared.

Request the HTML embed code for this file.

Parameters:

  • options (Optional, Hash{:allow_download,:allow_print,:allow_share, :width,:height,:color => String}) (defaults to: Hash.new)

    Options to use when generating the embed code.

Returns:

  • (String)

    HTML code to use to embed the file.



71
72
73
74
75
76
77
78
# File 'lib/box/file.rb', line 71

def embed_code(options = Hash.new)
  begin
    @api.file_embed(id, options)['file_embed_html']
  rescue Api::NotShared
    
    retry
  end
end

- (Array) get_comments

Get the comments left on this file.

Returns:

  • (Array)

    An array of Comments.



46
47
48
49
50
51
52
# File 'lib/box/file.rb', line 46

def get_comments
  comments = @api.get_comments(type, id)['comments']

  comments = Comment.create(@api, comments['comment']) if comments

  @data['comments'] = comments || Array.new
end

- (File) upload_copy(path)

Upload a new copy of this file. The name will be "file (#).ext" for the each additional copy.

Parameters:

  • path (String)

    The path to the file to upload.

Returns:

  • (File)

    The newly created file.



36
37
38
39
40
41
# File 'lib/box/file.rb', line 36

def upload_copy(path)
  info = @api.new_copy(path, id)['files']['file']
  parent.delete_info('files')

  self.class.new(api, parent, info)
end

- (File) upload_overwrite(path)

Overwrite this file, using the file at the specified path

Parameters:

  • path (String)

    The path to the file to upload.

Returns:



22
23
24
25
26
27
28
29
# File 'lib/box/file.rb', line 22

def upload_overwrite(path)
  info = @api.overwrite(path, id)['files']['file']

  clear_info
  update_info(info)

  self
end