Class: Rack::Client::Response

Inherits:
Response
  • Object
show all
Defined in:
lib/rack/client/core/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(status, headers = {}, body = [], &block) ⇒ Response

Returns a new instance of Response.



4
5
6
7
8
9
10
11
12
# File 'lib/rack/client/core/response.rb', line 4

def initialize(status, headers = {}, body = [], &block)
  @status = status.to_i
  @header = Utils::HeaderHash.new({"Content-Type" => "text/html"}.
                                  merge(headers))
  @body   = body
  @loaded = false

  @stream = block if block_given?
end

Instance Method Details

#bodyObject



20
21
22
23
24
# File 'lib/rack/client/core/response.rb', line 20

def body
  load_body

  @body
end

#each(&block) ⇒ Object



14
15
16
17
18
# File 'lib/rack/client/core/response.rb', line 14

def each(&block)
  load_body(&block)

  @body
end

#load_body(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack/client/core/response.rb', line 26

def load_body(&block)
  return @body.each(&block) if @body_loaded
  body = []

  @body.each do |chunk|
    unless chunk.empty?
      body << chunk
      yield chunk if block_given?
    end
  end

  if @stream
    @stream.call(lambda do |chunk|
      unless chunk.empty?
        body << chunk
        yield chunk if block_given?
      end
    end)
  end

  @body, @body_loaded = body, true
end