Module: InfinumJsonApiSetup::Rspec::Helpers::ResponseHelper

Defined in:
lib/infinum_json_api_setup/rspec/helpers/response_helper.rb

Instance Method Summary collapse

Instance Method Details

#json_responseObject



5
6
7
# File 'lib/infinum_json_api_setup/rspec/helpers/response_helper.rb', line 5

def json_response
  JSON.parse(response.body, symbolize_names: true)
end

#response_collectionObject



15
16
17
18
19
20
21
# File 'lib/infinum_json_api_setup/rspec/helpers/response_helper.rb', line 15

def response_collection
  raise 'json response is not a collection' unless json_response[:data].is_a?(Array)

  json_response[:data].map do |item|
    OpenStruct.new(id: item[:id], type: item[:type], **item[:attributes])
  end
end

#response_includedObject



35
36
37
38
39
# File 'lib/infinum_json_api_setup/rspec/helpers/response_helper.rb', line 35

def response_included
  json_response[:included].map do |item|
    OpenStruct.new(id: item[:id], type: item[:type], **item[:attributes])
  end
end

#response_included_relationship(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/infinum_json_api_setup/rspec/helpers/response_helper.rb', line 41

def response_included_relationship(name)
  data = response_relationships.fetch(name)[:data]

  return if data.nil?

  relationship_id, relationship_type =
    response_relationships.fetch(name)[:data].values_at(:id, :type)

  response_included.find do |object|
    object.id == relationship_id && object.type == relationship_type
  end
end

#response_itemObject



9
10
11
12
13
# File 'lib/infinum_json_api_setup/rspec/helpers/response_helper.rb', line 9

def response_item
  raise 'json response is not an item' if json_response[:data].is_a?(Array)

  OpenStruct.new(json_response[:data][:attributes])
end

#response_metaObject



31
32
33
# File 'lib/infinum_json_api_setup/rspec/helpers/response_helper.rb', line 31

def response_meta
  json_response[:meta]
end

#response_relationships(response_type: :item) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/infinum_json_api_setup/rspec/helpers/response_helper.rb', line 23

def response_relationships(response_type: :item)
  case response_type
  when :item then json_response.dig(:data, :relationships)
  when :collection then json_response[:data].pluck(:relationships)
  else raise ArgumentError ':response_type must be one of [:item, :collection]'
  end
end