Method: HTTPAdapter.verified_response

Defined in:
lib/httpadapter.rb

.verified_response(response) ⇒ Array

Verifies a response tuple matches the specification.

Parameters:

  • response (Array)

    The response object to be verified.

Returns:

  • (Array)

    The tuple, after normalization.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/httpadapter.rb', line 229

def self.verified_response(response)
  if !response.kind_of?(Array)
    raise TypeError, "Expected Array, got #{response.class}."
  end
  if response.size == 3
    # Verify that the response object matches the specification
    status, headers, body = response
    status = status.to_i if status.respond_to?(:to_i)
    if !status.kind_of?(Integer)
      raise TypeError, "Expected Integer, got #{status.class}."
    end
    original_headers, headers = headers, []
    if original_headers.respond_to?(:each)
      original_headers.each do |header, value|
        if header.respond_to?(:to_str)
          header = header.to_str
        else
          raise TypeError, "Expected String, got #{header.class}."
        end
        if value.respond_to?(:to_str)
          value = value.to_str
        else
          raise TypeError, "Expected String, got #{value.class}."
        end
        headers << [header, value]
      end
    else
      raise TypeError, 'Expected headers to respond to #each.'
    end
    if body.kind_of?(String)
      raise TypeError,
        'Body must not be a String; it must respond to #each and ' +
        'emit String values.'
    end
    # Can't verify that all chunks are Strings because #each may be
    # effectively destructive.
    if !body.respond_to?(:each)
      raise TypeError, 'Expected body to respond to #each.'
    end
  else
    raise TypeError,
      "Expected tuple of [status, headers, body], got #{response.inspect}."
  end
  return [status, headers, body]
end