Module: InfinumJsonApiSetup::Rspec::Helpers::ResponseHelper
- Defined in:
- lib/infinum_json_api_setup/rspec/helpers/response_helper.rb
Instance Method Summary collapse
- #json_response ⇒ Object
- #response_collection ⇒ Object
- #response_included ⇒ Object
- #response_included_relationship(name) ⇒ Object
- #response_item ⇒ Object
- #response_meta ⇒ Object
- #response_relationships(response_type: :item) ⇒ Object
Instance Method Details
#json_response ⇒ Object
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_collection ⇒ Object
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_included ⇒ Object
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_item ⇒ Object
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_meta ⇒ Object
31 32 33 |
# File 'lib/infinum_json_api_setup/rspec/helpers/response_helper.rb', line 31 def 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 |