require "mountapi/error/invalid_response" module Mountapi module Response module Base def self.included(base) base.extend Macros base.class_eval do include InstanceMethods end end module Macros def status(status) @status = status end def get_status @status end end module InstanceMethods def status self.class.get_status end # Since we return a mountapi response they must have a custom rack response to enable customizing def response @response ||= Rack::Response.new([]) end # Use the existing (or default) rack response def to_rack response.status = status response.write(to_json) response.set_header("content-type", "application/json") response.finish end def to_json payload.to_json end def default_schema Mountapi::Response.default_schema end def validate(definitions) errors = Schema::Validator.call(definitions[status].to_json_schema, payload) errors.empty? || raise(Error::InvalidResponse, errors) end end end end end