Class: RubyBox::File

Inherits:
Item
  • Object
show all
Defined in:
lib/ruby-box/file.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(folder_id, name = nil) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby-box/file.rb', line 14

def copy_to( folder_id, name=nil )

  # Allow either a folder_id or a folder object
  # to be passed in.
  folder_id = folder_id.id if folder_id.instance_of?(RubyBox::Folder)

  uri = URI.parse( "#{RubyBox::API_URL}/#{resource_name}/#{id}/copy" )
  request = Net::HTTP::Post.new( uri.request_uri )
  request.body = JSON.dump({
    "parent" => {"id" => folder_id},
    "name" => name
  })

  resp = @session.request(uri, request)
end

#create_comment(message) ⇒ Object


70
71
72
73
74
75
# File 'lib/ruby-box/file.rb', line 70

def create_comment(message)
  RubyBox::Comment.new(@session, {
      'item' => {'id' => id, 'type' => type},
      'message' => message
  }).create
end

#downloadObject


6
7
8
# File 'lib/ruby-box/file.rb', line 6

def download
  resp = stream.read
end

#download_urlObject


10
11
12
# File 'lib/ruby-box/file.rb', line 10

def download_url
  @session.get( file_content_url )["location"]
end

#stream(opts = {}) ⇒ Object


30
31
32
# File 'lib/ruby-box/file.rb', line 30

def stream( opts={} )
  open(download_url, opts)
end

#update_content(data) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby-box/file.rb', line 51

def update_content( data )

  url = "#{RubyBox::UPLOAD_URL}/#{resource_name}/#{id}/content"
  uri = URI.parse(url)

  request = Net::HTTP::Post::Multipart.new(uri.path, {
    "filename" => prepare_upload(data, name),
    "folder_id" => parent.id
  }, {"if-match" => etag })

  resp = @session.request(uri, request)
  if resp['entries']
    @raw_item = resp['entries'][0]
  else
    @raw_item = resp
  end
  self
end

#upload_content(data) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby-box/file.rb', line 34

def upload_content( data )
  url = "#{RubyBox::UPLOAD_URL}/#{resource_name}/content"
  uri = URI.parse(url)
  request = Net::HTTP::Post::Multipart.new(uri.path, {
    "filename" => UploadIO.new(data, "application/pdf", name),
    "folder_id" => parent.id
  })

  resp = @session.request(uri, request)
  if resp['entries']
    @raw_item = resp['entries'][0]
  else
    @raw_item = resp
  end
  self
end