lib/esplanade/response.rb in esplanade-0.1.0 vs lib/esplanade/response.rb in esplanade-1.0.0

- old
+ new

@@ -1,38 +1,32 @@ -require 'json-schema' +require 'esplanade/response/doc' +require 'esplanade/response/raw' +require 'esplanade/response/validation' module Esplanade class Response - attr_accessor :status, :body, :schemas + class Error < Esplanade::Error; end + class NotDocumented < Error; end + class BodyIsNotJson < Error; end + class Invalid < Error; end - class Unsuitable < RuntimeError; end - class NotDocumented < RuntimeError; end + attr_reader :request - def initialize(status, body, expect_request) - return unless Esplanade.configuration.validation_response && expect_request + def initialize(request, status, raw_body) + @request = request @status = status - @body = Body.craft(body) - @schemas = expect_request.find_responses(status: @status) - raise NotDocumented unless (@schemas&.first) || Esplanade.configuration.skip_not_documented - self + @raw_body = raw_body end - def error - return JSON::Validator.fully_validate(@schemas.first['body'], @body) if @schemas.size == 1 - - @schemas.each do |action| - res = JSON::Validator.fully_validate(action['body'], @body) - return res if res == [] - end - - ['invalid'] + def doc + @doc ||= Doc.new(@request, raw) end - def valid! - raise Unsuitable, error unless error.empty? + def raw + @raw ||= Raw.new(@request, @status, @raw_body) end - def validate? - @schemas&.first && Esplanade.configuration.validation_response + def validation + @validation ||= Validation.new(@request, doc, raw) end end end