Class: HTTPkit::Body

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Mutable
Defined in:
lib/httpkit/body.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Body) initialize(source = nil, length = nil, closed = Promise.new)



18
19
20
21
22
23
24
25
# File 'lib/httpkit/body.rb', line 18

def initialize(source = nil, length = nil, closed = Promise.new)
  @source, @length = source, length
  @closed = closed
  @chunks = []

  apply_source
  @closed.fulfill if source
end

Instance Attribute Details

- (Object) closed (readonly)

Returns the value of attribute closed



15
16
17
# File 'lib/httpkit/body.rb', line 15

def closed
  @closed
end

Class Method Details

+ (Object) build(source = nil, length = nil)



7
8
9
10
11
12
13
# File 'lib/httpkit/body.rb', line 7

def self.build(source = nil, length = nil)
  if source.is_a?(self)
    source
  else
    new(source, length)
  end
end

Instance Method Details

- (Object) chunks



56
57
58
# File 'lib/httpkit/body.rb', line 56

def chunks
  @chunks.dup
end

- (Object) each(&block)



42
43
44
45
46
47
48
49
50
# File 'lib/httpkit/body.rb', line 42

def each(&block)
  return to_enum(__method__) unless block

  if @source.respond_to?(:each)
    @source.each(&block)
  else
    each_chunk(&block)
  end
end

- (Object) inspect



60
61
62
63
# File 'lib/httpkit/body.rb', line 60

def inspect
  len = length_known? ? length : 'unknown'
  sprintf('#<%s:0x%d @length=%s>', self.class.name, object_id, len)
end

- (Object) length



27
28
29
# File 'lib/httpkit/body.rb', line 27

def length
  @length || 0
end

- (Boolean) length_known?



31
32
33
# File 'lib/httpkit/body.rb', line 31

def length_known?
  !@length.nil?
end

- (Object) to_s



52
53
54
# File 'lib/httpkit/body.rb', line 52

def to_s
  each.to_a.join
end

- (Object) write(chunk)



35
36
37
38
39
40
# File 'lib/httpkit/body.rb', line 35

def write(chunk)
  if @closed.pending? && !chunk.empty?
    @chunks << chunk
    @closed.progress(chunk)
  end
end