Sha256: 20dc777b5a32c1992d707e8595180bc1946d148bdc0870738c285131f24c7772

Contents?: true

Size: 759 Bytes

Versions: 1

Compression:

Stored size: 759 Bytes

Contents

require 'json'
require 'active_support/core_ext/hash'

module APIMatchers
  module ResponseBody
    class HaveJsonNode < Base
      def matches?(actual)
        @actual = actual

        begin
          node = Core::FindInJSON.new(json).find(node: @expected_node.to_s, value: @with_value)

          if @expected_including_text
            node.to_s.include?(@expected_including_text)
          else
            true # the node is present
          end
        rescue ::APIMatchers::KeyNotFound
          false # the key was not found
        end
      end

      def json
        JSON.parse(response_body)
      rescue JSON::ParserError => exception
        raise ::APIMatchers::InvalidJSON.new("Invalid JSON: '#{response_body}'")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api_matchers-0.2.0 lib/api_matchers/response_body/have_json_node.rb