Sha256: 61b881c7afb19ccc0643452e58fcf83dc7e236ff51df3dc1b56c868750a89801

Contents?: true

Size: 885 Bytes

Versions: 2

Compression:

Stored size: 885 Bytes

Contents

require "json-schema"

module JsonMatchers
  class Matcher
    def initialize(schema_path, options = {})
      @schema_path = schema_path
      @options = default_options.merge(options)
    end

    def matches?(response)
      JSON::Validator.validate!(
        schema_path.to_s,
        json_from(response).to_s,
        options,
      )
    rescue JSON::Schema::ValidationError => ex
      @validation_failure_message = ex.message
      false
    rescue JSON::Schema::JsonParseError
      raise InvalidSchemaError
    end

    def validation_failure_message
      @validation_failure_message.to_s
    end

    private

    attr_reader :schema_path, :options

    def json_from(response)
      if response.respond_to?(:body)
        response.body
      else
        response
      end
    end

    def default_options
      JsonMatchers.configuration.options || {}
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json_matchers-0.6.2 lib/json_matchers/matcher.rb
json_matchers-0.6.1 lib/json_matchers/matcher.rb