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

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

Returns a new instance of Body

Parameters:

  • source (String, #each, nil) (defaults to: nil)


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

#closedObject (readonly)

Returns the value of attribute closed



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

def closed
  @closed
end

Class Method Details

.build(source = nil, length = nil) ⇒ Object



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

#chunksObject



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

def chunks
  @chunks.dup
end

#each(&block) ⇒ Object



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

#inspectObject



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

#lengthObject



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

def length
  @length || 0
end

#length_known?Boolean

Returns:

  • (Boolean)


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

def length_known?
  !@length.nil?
end

#to_sObject



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

def to_s
  each.to_a.join
end

#write(chunk) ⇒ Object



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