class Jsonschema::Matchers::MatchJsonschema

Match Jsonschema snapshot

Attributes

errors[R]
metadata[R]
name[R]
path[R]

Public Class Methods

new(metadata, name) click to toggle source
# File lib/jsonschema/matchers/match_jsonschema.rb, line 11
def initialize(metadata, name)
  @metadata = metadata
  @path = "#{root_dir}/#{Metadater.call(metadata)}"
  @name = name.downcase.gsub(%r{[\/\s]}, '/' => '_', ' ' => '_')
end

Public Instance Methods

failure_message() click to toggle source
# File lib/jsonschema/matchers/match_jsonschema.rb, line 26
def failure_message
  errors.join("\n")
end
matches?(actual) click to toggle source
# File lib/jsonschema/matchers/match_jsonschema.rb, line 17
def matches?(actual)
  file_full_path = "#{path}/#{name}.json"
  record(actual) unless File.exist?(file_full_path)
  file = File.open(file_full_path)

  @errors = JSON::Validator.fully_validate(file.path, actual)
  errors.empty?
end

Private Instance Methods

record(actual) click to toggle source
# File lib/jsonschema/matchers/match_jsonschema.rb, line 36
def record(actual)
  json_schema = Jsonschema::Generator.call(actual)
  file_name = "#{name}.json"
  snap_path = File.join(path, file_name)

  unless Dir.exist?(File.dirname(snap_path))
    FileUtils.mkdir_p(File.dirname(snap_path))
  end

  file = File.new(snap_path, 'w+')
  file.write(JSON.pretty_generate(json_schema))
  file.close
end
root_dir() click to toggle source
# File lib/jsonschema/matchers/match_jsonschema.rb, line 32
def root_dir
  Jsonschema::Matchers.configuration.schema_dir
end