Class: Vimeo::Advanced::SimpleUpload::Chunk

Inherits:
Object
  • Object
show all
Defined in:
lib/vimeo/advanced/simple_upload/chunk.rb

Constant Summary collapse

MULTIPART_BOUNDARY =
"-----------RubyMultipartPost"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, data) ⇒ Chunk

Returns a new instance of Chunk.



11
12
13
14
15
16
17
# File 'lib/vimeo/advanced/simple_upload/chunk.rb', line 11

def initialize(task, data)
  @task = task
  @vimeo = task.vimeo
  @data = data
  @size = data.size
  @index = task.chunks.count
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/vimeo/advanced/simple_upload/chunk.rb', line 9

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/vimeo/advanced/simple_upload/chunk.rb', line 7

def id
  @id
end

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/vimeo/advanced/simple_upload/chunk.rb', line 7

def index
  @index
end

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/vimeo/advanced/simple_upload/chunk.rb', line 9

def size
  @size
end

#taskObject (readonly)

Returns the value of attribute task.



8
9
10
# File 'lib/vimeo/advanced/simple_upload/chunk.rb', line 8

def task
  @task
end

#vimeoObject (readonly)

Returns the value of attribute vimeo.



8
9
10
# File 'lib/vimeo/advanced/simple_upload/chunk.rb', line 8

def vimeo
  @vimeo
end

Instance Method Details

#uploadObject

Performs the upload via Multipart.



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
# File 'lib/vimeo/advanced/simple_upload/chunk.rb', line 20

def upload
  endpoint = "#{task.endpoint}"

  response = task.oauth_consumer.request(:post, endpoint, vimeo.get_access_token, {}, {}) do |req|
    req.set_content_type("multipart/form-data", { "boundary" => MULTIPART_BOUNDARY })

    io = StringIO.new(data)
    io.instance_variable_set :"@original_filename", task.filename
    def io.original_filename; @original_filename; end
    def io.content_type; "application/octet-stream"; end

    parts = []
    parts << Parts::ParamPart.new(MULTIPART_BOUNDARY, "ticket_id", task.id)
    parts << Parts::ParamPart.new(MULTIPART_BOUNDARY, "chunk_id", index)
    parts << Parts::FilePart.new(MULTIPART_BOUNDARY, "file_data", io)
    parts << Parts::EpiloguePart.new(MULTIPART_BOUNDARY)

    ios                = parts.map{|p| p.to_io }
    req.content_length = parts.inject(0) {|sum,i| sum + i.length }
    req.body_stream    = CompositeReadIO.new(*ios)

    :continue
  end

  # free memory (for big file uploads)
  @data = nil

  @id = response.body
end