Sha256: 51f109e266915bee8c4bf011e572a6b350be421260a4b28a69d0c70c2cad8779

Contents?: true

Size: 1.38 KB

Versions: 14

Compression:

Stored size: 1.38 KB

Contents

module RubyBox
  class File < Item

    has_many :comments

    def download
      resp = stream.read
    end

    def stream( opts={} )
      url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/content"
      @session.do_stream( url, opts )
    end

    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

    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

    private

    def resource_name
      'files'
    end

    def update_fields
      ['name', 'description']
    end

    def prepare_upload(data, fname)
      UploadIO.new(data, "application/pdf", fname)
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ruby-box-1.5.0 lib/ruby-box/file.rb
ruby-box-1.4.0 lib/ruby-box/file.rb
ruby-box-1.3.1 lib/ruby-box/file.rb
ruby-box-1.3.0 lib/ruby-box/file.rb
ruby-box-1.2.1 lib/ruby-box/file.rb
ruby-box-1.2.0 lib/ruby-box/file.rb
ruby-box-1.1.2 lib/ruby-box/file.rb
ruby-box-1.1.1 lib/ruby-box/file.rb
ruby-box-1.1.0 lib/ruby-box/file.rb
ruby-box-1.0.6 lib/ruby-box/file.rb
ruby-box-1.0.5 lib/ruby-box/file.rb
ruby-box-1.0.4 lib/ruby-box/file.rb
ruby-box-1.0.3 lib/ruby-box/file.rb
ruby-box-1.0.1 lib/ruby-box/file.rb