Sha256: 41914538ced8eb90ecaf867e6f03ad880f58c9cc727c140a564de95e8b39594d

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 KB

Contents

module InfinumJsonApiSetup
  module Rspec
    module Helpers
      module ResponseHelper
        def json_response
          JSON.parse(response.body, symbolize_names: true)
        end

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

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

        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

        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

        def response_meta
          json_response[:meta]
        end

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

        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
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
infinum_json_api_setup-0.0.8 lib/infinum_json_api_setup/rspec/helpers/response_helper.rb
infinum_json_api_setup-0.0.7 lib/infinum_json_api_setup/rspec/helpers/response_helper.rb
infinum_json_api_setup-0.0.6 lib/infinum_json_api_setup/rspec/helpers/response_helper.rb
infinum_json_api_setup-0.0.5 lib/infinum_json_api_setup/rspec/helpers/response_helper.rb
infinum_json_api_setup-0.0.4 lib/infinum_json_api_setup/rspec/helpers/response_helper.rb
infinum_json_api_setup-0.0.3 lib/infinum_json_api_setup/rspec/helpers/response_helper.rb