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.

Defined Under Namespace

Modules: Reader

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Readable

#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
  @bytesize = 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

#bytesizeObject



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

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

#closeObject



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

def close
  self
end

#eachObject



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

def each
  return to_enum unless block_given?
  
  while @index < @chunks.count
    yield @chunks[@index]
    @index += 1
  end
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @chunks.empty?
end

#inspectObject



89
90
91
# File 'lib/async/http/body/buffered.rb', line 89

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

#readObject



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

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

#rewindObject



85
86
87
# File 'lib/async/http/body/buffered.rb', line 85

def rewind
  @index = 0
end