Class: Scorpio::Response

Inherits:
Ur::Response
  • Object
show all
Defined in:
lib/scorpio/response.rb

Instance Method Summary collapse

Instance Method Details

#body_objectObject

Returns the body (String) is parsed according to the response media type and if supported (only application/json is currently supported) instantiated according to

response_schema.

Returns:

  • (Object)

    the body (String) is parsed according to the response media type and if supported (only application/json is currently supported) instantiated according to

    response_schema



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/scorpio/response.rb', line 11

def body_object
  # TODO handle media types like `application/schema-instance+json` or vendor things like github's
  if media_type == 'application/json'
    if body.empty?
      # an empty body isn't valid json, of course, but we'll just return nil for it.
      body_object = nil
    else
      begin
        body_object = ::JSON.parse(body)
      #rescue ::JSON::ParserError
        # TODO
      end
    end

    if response_schema && (body_object.respond_to?(:to_hash) || body_object.respond_to?(:to_ary))
      body_object = JSI.class_for_schema(response_schema).new(JSI::JSON::Node.new_doc(body_object))
    end

    body_object
  elsif media_type == 'text/plain'
    body
  else
    # we will return the body if we do not have a supported parsing. for now.
    body
  end
end

#response_schema::JSI::Schema

Returns the schema for this response according to its OpenAPI doc.

Returns:

  • (::JSI::Schema)

    the schema for this response according to its OpenAPI doc



4
5
6
# File 'lib/scorpio/response.rb', line 4

def response_schema
  ur.scorpio_request.operation.response_schema(status: status, media_type: media_type)
end