Class: Webmachine::Adapters::Ring::RingResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/webmachine/adapters/ring.rb

Class Method Summary collapse

Class Method Details

.from_webmachine(response) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/webmachine/adapters/ring.rb', line 153

def self.from_webmachine(response)
  headers = response.headers

  # HTTP-Kit sets its own Content-Length
  headers.delete("Content-Length")

  headers.each do |k, v|
    headers[k] = v.join(", ") if v.is_a? Array
  end

  response_body = if response.body.respond_to?(:call)
                    Webmachine::ChunkedBody.new([response.body.call])
                  elsif response.body.respond_to?(:each)
                    # This might be an IOEncoder with a Content-Length, which shouldn't be chunked.
                    if response.headers["Transfer-Encoding"] == "chunked"
                      Webmachine::ChunkedBody.new(response.body)
                    else
                      response.body
                    end
                  elsif response.body.is_a? IO
                    response.body.to_outputstream
                  else
                    response.body
                  end

  if response_body.respond_to?(:each)
    ring_body = ""
    response_body.each do |chunk|
      ring_body << chunk.to_s
    end
  else
    ring_body = response_body
  end

  headers = Java::JavaUtil::HashMap.new(headers)

  ring_response = [
    Ring::STATUS  , response.code,
    Ring::HEADERS , headers,
    Ring::BODY    , ring_body
  ]

  return PersistentHashMap.create(ring_response.to_java)
end