Class: BeJsonApiResponse

Inherits:
Object
  • Object
show all
Includes:
JsonapiRspec
Defined in:
lib/jsonapi_rspec/be_json_api_response.rb

Overview

Class BeJsonApiResponse provides custom RSpec matching for json:api responses in general.

It expects a Rack::Response (or similar) response object

Usage:

expect(response).to BeJsonApiResponse.new

Author:

Constant Summary

Constants included from JsonapiRspec

JsonapiRspec::VERSION

Instance Method Summary collapse

Methods included from JsonapiRspec

configure, #failure_message_when_negated, root

Instance Method Details

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jsonapi_rspec/be_json_api_response.rb', line 20

def matches?(response)
  return false unless valid_response?(response)

  @parsed_response = JSON.parse(response.body)

  return false if response_is_error?
  return false unless required_top_level_sections?
  return false if conflicting_sections?

  return false if JsonapiRspec.configuration.meta_required && !valid_meta_section?

  @parsed_response.each_key do |key|
    case key.to_sym
    when :data
      return false unless valid_data_section?
    when :meta
      return false unless valid_meta_section?
    when :jsonapi
      next # this can legally be anything
    when :included
      next # TODO: handle included objects
    when :links
      next # TODO: handle links objects
    else
      return failure_message(FailureMessages::UNEXPECTED_TOP_LVL_KEY % key)
    end
  end

  true
end