Sha256: 5b7f445fae041a01f6e9b57f93d52bd2513eabc7c3e73408165671429bd9da5f

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'grape-swagger'

module MotherBrain::API
  class V1 < MB::API::Endpoint
    require_relative 'v1/config_endpoint'
    require_relative 'v1/environments_endpoint'
    require_relative 'v1/jobs_endpoint'
    require_relative 'v1/plugins_endpoint'
    require_relative 'v1/chef_endpoint'

    version 'v1', using: :header, vendor: 'motherbrain'
    format :json
    default_format :json

    rescue_from Grape::Exceptions::Validation do |e|
      body = MultiJson.encode(
        status: e.status,
        message: e.message,
        param: e.param
      )
      rack_response(body, e.status, "Content-type" => "application/json")
    end

    rescue_from :all do |ex|
      body = if ex.is_a?(MB::MBError)
        ex.to_json
      else
        MB.log.fatal { "an unknown error occured: #{ex}" }
        MultiJson.encode(code: -1, message: ex.message)
      end

      rack_response(body, 500, "Content-type" => "application/json")
    end

    before do
      header['Access-Control-Allow-Origin'] = '*'
      header['Access-Control-Request-Method'] = '*'
      header.delete('Transfer-Encoding')
    end

    mount V1::ConfigEndpoint
    mount V1::JobsEndpoint
    mount V1::EnvironmentsEndpoint
    mount V1::PluginsEndpoint
    mount V1::ChefEndpoint
    add_swagger_documentation

    if MB.testing?
      get :mb_error do
        raise MB::InternalError, "a nice error message"
      end

      get :unknown_error do
        raise ::ArgumentError, "hidden error message"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motherbrain-1.3.0 lib/mb/api/v1.rb