class ActiveModelSerializers::Test::Schema::AssertSchema

Attributes

document_store[R]
message[R]
payload[R]
request[R]
response[R]
schema_path[R]

Public Class Methods

new(schema_path, request, response, message, payload = nil) click to toggle source

Interface may change.

# File lib/active_model_serializers/test/schema.rb, line 42
def initialize(schema_path, request, response, message, payload = nil)
  require_json_schema!
  @request = request
  @response = response
  @payload = payload
  @schema_path = schema_path || schema_path_default
  @message = message
  @document_store = JsonSchema::DocumentStore.new
  add_schema_to_document_store
end

Public Instance Methods

call() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 53
def call
  json_schema.expand_references!(store: document_store)
  status, errors = json_schema.validate(response_body)
  @message = [message, errors.map(&:to_s).to_sentence].compact.join(': ')
  status
end

Protected Instance Methods

action() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 68
def action
  request.filtered_parameters.with_indifferent_access[:action]
end
add_schema_to_document_store() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 100
def add_schema_to_document_store
  Dir.glob("#{schema_directory}/**/*.json").each do |path|
    schema_data = load_json_file(path)
    extra_schema = JsonSchema.parse!(schema_data)
    document_store.add_schema(extra_schema)
  end
end
controller_path() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 64
def controller_path
  request.filtered_parameters.with_indifferent_access[:controller]
end
json_schema() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 96
def json_schema
  @json_schema ||= JsonSchema.parse!(schema_data)
end
load_json(json) click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 108
def load_json(json)
  JSON.parse(json)
rescue JSON::ParserError => ex
  raise InvalidSchemaError, ex.message
end
load_json_file(path) click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 114
def load_json_file(path)
  load_json(File.read(path))
rescue Errno::ENOENT
  raise MissingSchema, "No Schema file at #{schema_full_path}"
end
request_params() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 92
def request_params
  request.env['action_dispatch.request.request_parameters']
end
require_json_schema!() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 120
def require_json_schema!
  require 'json_schema'
rescue LoadError
  raise LoadError, "You don't have json_schema installed in your application. Please add it to your Gemfile and run bundle install"
end
response_body() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 88
def response_body
  load_json(response.body)
end
schema_data() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 84
def schema_data
  load_json_file(schema_full_path)
end
schema_directory() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 72
def schema_directory
  ActiveModelSerializers.config.schema_path
end
schema_full_path() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 76
def schema_full_path
  "#{schema_directory}/#{schema_path}"
end
schema_path_default() click to toggle source
# File lib/active_model_serializers/test/schema.rb, line 80
def schema_path_default
  "#{controller_path}/#{action}.json"
end