Sha256: 3810c1aabac2c6879df7badebe0292e000ae55158c6d85733b2faf0b0a22140f

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mountapi-0.11.1 lib/mountapi/response/base.rb