Class: Async::HTTP::Body::Buffered

Inherits:
Readable
  • Object
show all
Defined in:
lib/async/http/body/buffered.rb

Overview

A body which buffers all it’s contents.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Readable

#each, #join, #stop

Constructor Details

#initialize(chunks) ⇒ Buffered

Returns a new instance of Buffered.



49
50
51
52
53
54
# File 'lib/async/http/body/buffered.rb', line 49

def initialize(chunks)
	@chunks = chunks
	@length = nil
	
	@index = 0
end

Class Method Details

.for(body) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/async/http/body/buffered.rb', line 39

def self.for(body)
	chunks = []
	
	body.each do |chunk|
		chunks << chunk
	end
	
	self.new(chunks)
end

.wrap(body) ⇒ Object

Wraps an array into a buffered body.



29
30
31
32
33
34
35
36
37
# File 'lib/async/http/body/buffered.rb', line 29

def self.wrap(body)
	if body.is_a? Async::HTTP::Body::Readable
		return body
	elsif body.is_a? Array
		return self.new(body)
	else
		return self.for(body)
	end
end

Instance Method Details

#closeObject



64
65
66
# File 'lib/async/http/body/buffered.rb', line 64

def close
	self
end

#empty?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/async/http/body/buffered.rb', line 60

def empty?
	@index >= @chunks.length
end

#inspectObject



82
83
84
# File 'lib/async/http/body/buffered.rb', line 82

def inspect
	"\#<#{self.class} #{@chunks.count} chunks, #{self.length} bytes>"
end

#lengthObject



56
57
58
# File 'lib/async/http/body/buffered.rb', line 56

def length
	@length ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize}
end

#readObject



68
69
70
71
72
73
74
75
76
# File 'lib/async/http/body/buffered.rb', line 68

def read
	if chunk = @chunks[@index]
		@index += 1
		
		return chunk.dup
	end
	
	return nil
end

#rewindObject



78
79
80
# File 'lib/async/http/body/buffered.rb', line 78

def rewind
	@index = 0
end