Sha256: ebe90b4dc85dcef6abab699b6487f2a7111cb93170b92144ba4e62bd517b2e0e

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

module Workarea
  module Api
    module Storefront
      class ApplicationController < Workarea::ApplicationController
        include Workarea::Storefront::HttpCaching
        include Api::Storefront::Authentication
        include Api::Storefront::UserActivity

        respond_to :json

        before_action :set_json_format
        before_action :skip_session
        before_action { params.permit! }
        after_action :disable_cors_protection

        rescue_from Mongoid::Errors::DocumentNotFound, with: :handle_not_found
        rescue_from Mongoid::Errors::Validations, with: :handle_invalid
        rescue_from Mongoid::Errors::UnknownAttribute, with: :handle_unknown_attribute
        rescue_from Authentication::InvalidError, with: :handle_invalid_auth

        helper :all

        def cache_page
          expires_in Workarea.config.page_cache_ttl, public: true
        end

        def skip_session
          request.session_options[:skip] = true
        end

        private

        def set_json_format
          request.format = :json
        end

        def handle_not_found(e)
          payload = { params: e.params, problem: e.problem }
          render json: payload, status: :not_found
        end

        def handle_invalid(e)
          render(
            json: { problems: e.document.errors.full_messages },
            status: :unprocessable_entity
          )
        end

        def handle_unknown_attribute(e)
          render json: e.as_json.slice('problem'), status: :unprocessable_entity
        end

        def handle_invalid_auth
          request_http_token_authentication
        end

        def disable_cors_protection
          headers['Access-Control-Allow-Origin'] = '*'
          headers['Access-Control-Allow-Headers'] = '*'
          headers['Access-Control-Allow-Methods'] = 'GET, POST, PATCH, PUT, DELETE, OPTIONS'
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
workarea-api-4.4.7 storefront/app/controllers/workarea/api/storefront/application_controller.rb
workarea-api-storefront-4.4.7 app/controllers/workarea/api/storefront/application_controller.rb
workarea-api-4.4.6 storefront/app/controllers/workarea/api/storefront/application_controller.rb
workarea-api-storefront-4.4.6 app/controllers/workarea/api/storefront/application_controller.rb