Sha256: f302a1ecfdb008dfa6311e490ced4b4b99d98b3d463cf9e4cb3cec3d0dd8f917

Contents?: true

Size: 1.41 KB

Versions: 12

Compression:

Stored size: 1.41 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'

    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
    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

12 entries across 12 versions & 1 rubygems

Version Path
motherbrain-1.2.1 lib/mb/api/v1.rb
motherbrain-1.2.0 lib/mb/api/v1.rb
motherbrain-1.1.3 lib/mb/api/v1.rb
motherbrain-1.1.2 lib/mb/api/v1.rb
motherbrain-1.1.1 lib/mb/api/v1.rb
motherbrain-1.1.0 lib/mb/api/v1.rb
motherbrain-1.0.0 lib/mb/api/v1.rb
motherbrain-0.14.5 lib/mb/api/v1.rb
motherbrain-0.14.4 lib/mb/api/v1.rb
motherbrain-0.14.3 lib/mb/api/v1.rb
motherbrain-0.14.2 lib/mb/api/v1.rb
motherbrain-0.13.1 lib/mb/api/v1.rb