Class: Spikard::StreamingResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/spikard/response.rb,
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.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/spikard/response.rb', line 98

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

  rebuild_native!
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



96
97
98
# File 'lib/spikard/response.rb', line 96

def headers
  @headers
end

#native_responseObject (readonly)

Returns the value of attribute native_response.



96
97
98
# File 'lib/spikard/response.rb', line 96

def native_response
  @native_response
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



96
97
98
# File 'lib/spikard/response.rb', line 96

def status_code
  @status_code
end

#streamObject (readonly)

Returns the value of attribute stream.



96
97
98
# File 'lib/spikard/response.rb', line 96

def stream
  @stream
end

Instance Method Details

#to_native_responseObject



113
114
115
# File 'lib/spikard/response.rb', line 113

def to_native_response
  @native_response
end