Class: JsonMatchers::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/json_matchers/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema_path, **options) ⇒ Matcher

Returns a new instance of Matcher.



5
6
7
8
# File 'lib/json_matchers/matcher.rb', line 5

def initialize(schema_path, **options)
  @schema_path = schema_path
  @options = options
end

Instance Method Details

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/json_matchers/matcher.rb', line 10

def matches?(response)
  @response = response

  validator_options = {
    strict: true,
  }.merge(options)

  JSON::Validator.validate!(
    schema_path.to_s,
    response.body,
    validator_options,
  )
rescue JSON::Schema::ValidationError => ex
  @validation_failure_message = ex.message
  false
rescue JSON::ParserError
  raise InvalidSchemaError
end

#validation_failure_messageObject



29
30
31
# File 'lib/json_matchers/matcher.rb', line 29

def validation_failure_message
  @validation_failure_message.to_s
end