Module: Scorpio::Response

Defined in:
lib/scorpio/response.rb

Instance Method Summary collapse

Instance Method Details

#body_objectObject

the body (String) is parsed according to the response media type, if supported (only JSON is currently supported), and instantiated as a JSI instance of #response_schema if that is defined.



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

def body_object
  if 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 = response_schema.new_jsi(body_object, mutable: true)
    end

    body_object
  elsif content_type && content_type.type_text? && content_type.subtype?('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

the schema for this response according to its OpenAPI doc

Returns:

  • (::JSI::Schema)


9
10
11
# File 'lib/scorpio/response.rb', line 9

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