Class: Uploader::Upload::Put::Chunker

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-uploader/uploader.rb

Instance Method Summary collapse

Constructor Details

#initialize(sock, content_length, handlers) ⇒ Chunker

Returns a new instance of Chunker.



74
75
76
77
78
79
80
81
# File 'lib/ruby-uploader/uploader.rb', line 74

def initialize(sock, content_length, handlers)
  @sock = sock
  @prev = nil
  @count = 0
  @total_count = nil
  @content_length = content_length.to_i
  @handlers = handlers
end

Instance Method Details

#finishObject



105
106
107
# File 'lib/ruby-uploader/uploader.rb', line 105

def finish
  @sock.write("0\r\n\r\n")
end

#write(buf) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ruby-uploader/uploader.rb', line 83

def write(buf)
  @total_count = @content_length / buf.bytesize.to_i if @total_count.nil?

  @handlers[:before_chunk].each do |handler|
    handler.execute buf, @count, @total_count, @content_length
  end

  puts "#{@count} / #{@total_count}"

  @sock.write("#{buf.bytesize.to_s(16)}\r\n")
  rv = @sock.write(buf)
  @sock.write("\r\n")

  @count += 1

  @handlers[:after_chunk].each do |handler|
    handler.execute buf, @count, @total_count, @content_length
  end

  rv
end