Sha256: f147e037c19280df1c77d6a5f98f0f24d0c174ca3f30c4204ac126bc9241aea5

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

module Orchestrator
    class Base < ::ActionController::Base
        layout nil
        rescue_from Couchbase::Error::NotFound, with: :entry_not_found


        # Add headers to allow for CORS requests to the API
        before_filter :allow_cors


        # This is a preflight OPTIONS request
        def options
            render nothing: true
        end


        protected


        # Don't keep re-creating these objects for every request
        ALLOW_ORIGIN = 'Access-Control-Allow-Origin'.freeze
        ALLOW_METHODS = 'Access-Control-Allow-Methods'.freeze
        ALLOW_HEADERS = 'Access-Control-Allow-Headers'.freeze
        MAX_AGE = 'Access-Control-Max-Age'.freeze
        ANY_ORIGIN = '*'.freeze
        ANY_METHOD = 'GET, POST, PUT, DELETE, OPTIONS, PATCH'.freeze
        COMMON_HEADERS = 'Origin, Accept, Content-Type, X-Requested-With, Authorization, X-Frame-Options'.freeze
        ONE_DAY = '1728000'.freeze

        def allow_cors
            headers[ALLOW_ORIGIN] = ANY_ORIGIN
            headers[ALLOW_METHODS] = ANY_METHOD
            headers[ALLOW_HEADERS] = COMMON_HEADERS
            headers[MAX_AGE] = ONE_DAY
        end
        
    
        # Couchbase catch all
        def entry_not_found(err)
            logger.warn err.message
            logger.warn err.backtrace.join("\n") if err.respond_to?(:backtrace) && err.backtrace
            render nothing: true, status: :not_found  # 404
        end

        # Helper for extracting the id from the request
        def id
            return @id if @id
            params.require(:id)
            @id = params.permit(:id)[:id]
        end

        # Used to save and respond to all model requests
        def save_and_respond(model)
            yield if model.save && block_given?
            respond_with :api, model
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
orchestrator-1.0.2 app/controllers/orchestrator/base.rb
orchestrator-1.0.1 app/controllers/orchestrator/base.rb