Class: Chef::HTTP::StreamHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/http.rb

Overview

Class for applying middleware behaviors to streaming responses. Collects stream handlers (if any) from each middleware. When #handle_chunk is called, the chunk gets passed to all handlers in turn for processing.

Instance Method Summary collapse

Constructor Details

#initialize(middlewares, response) ⇒ StreamHandler

Returns a new instance of StreamHandler


44
45
46
47
48
49
50
51
# File 'lib/chef/http.rb', line 44

def initialize(middlewares, response)
  middlewares = middlewares.flatten
  @stream_handlers = []
  middlewares.each do |middleware|
    stream_handler = middleware.stream_response_handler(response)
    @stream_handlers << stream_handler unless stream_handler.nil?
  end
end

Instance Method Details

#handle_chunk(next_chunk) ⇒ Object


53
54
55
56
57
58
59
60
# File 'lib/chef/http.rb', line 53

def handle_chunk(next_chunk)
  # stream handlers handle responses so must be applied in reverse order
  # (same as #apply_stream_complete_middleware or #apply_response_midddleware)
  @stream_handlers.reverse.inject(next_chunk) do |chunk, handler|
    Chef::Log.debug("Chef::HTTP::StreamHandler calling #{handler.class}#handle_chunk")
    handler.handle_chunk(chunk)
  end
end