Class: Spikard::StreamingResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/spikard/streaming_response.rb

Overview

Represents a streaming HTTP response made of chunks produced lazily.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, status_code: 200, headers: nil) ⇒ StreamingResponse

Returns a new instance of StreamingResponse.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/spikard/streaming_response.rb', line 8

def initialize(stream, status_code: 200, headers: nil)
  unless stream.respond_to?(:next) || stream.respond_to?(:each)
    raise ArgumentError, 'StreamingResponse requires an object responding to #next or #each'
  end

  @stream = stream.respond_to?(:to_enum) ? stream.to_enum : stream
  @status_code = Integer(status_code || 200)
  header_hash = headers || {}
  @headers = header_hash.each_with_object({}) do |(key, value), memo|
    memo[String(key)] = String(value)
  end
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/spikard/streaming_response.rb', line 6

def headers
  @headers
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



6
7
8
# File 'lib/spikard/streaming_response.rb', line 6

def status_code
  @status_code
end

#streamObject (readonly)

Returns the value of attribute stream.



6
7
8
# File 'lib/spikard/streaming_response.rb', line 6

def stream
  @stream
end