module JunglePath::API::Helpers::Defaults

Public Instance Methods

set_default_authentication_check(debug_show_params=false) click to toggle source
# File lib/jungle_path/api/helpers/defaults.rb, line 62
def set_default_authentication_check debug_show_params=false
        before do
                puts ""
                puts "::::[request_start: #{Time.now.utc}]::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
                puts "::::[#{request.env['REMOTE_USER']}: #{request.request_method} #{request.url}"
                puts "(api_helpsers - before do - default authentication check)"
                #puts "[params] #{params.to_h}" if configatron.debug.show_params
                puts "[params] #{params.to_h}" if debug_show_params
                puts "authenticate..."
                # These three request paths return user info, so force no_cache = true so that stale user data is not returned!
                no_cache = request.path_info == '/authenticate' or request.path_info == '/current/user' or request.path_info == '/current/user/auth'
                puts "authenticate no_cache: #{no_cache}."
                authenticate no_cache
        end

        after do
                puts "[request end]"
        end
end
set_default_error_handling(logger) click to toggle source
# File lib/jungle_path/api/helpers/defaults.rb, line 26
def set_default_error_handling logger
        # These must be disabled to allow error methods to fire.
        disable :raise_errors
        disable :show_exceptions

        error JunglePath::Exceptions::MissingRequiredFields do
                e = env['sinatra.error']
                trace = e.backtrace.join("\n")
                logger.error "#{e.message}\n#{trace}."
                halt 400, e.message
        end

        error JunglePath::Exceptions::NotFoundException do
                e = env['sinatra.error']
                trace = e.backtrace.join("\n")
                logger.error "#{e.message}\n#{trace}."
                halt 404, e.message
        end

        error do
                e = env['sinatra.error']
                trace = e.backtrace.join("\n")
                logger.error "#{e.message}\n#{trace}."
                halt 500, e.message
        end
end
set_default_helpers() click to toggle source
# File lib/jungle_path/api/helpers/defaults.rb, line 53
def set_default_helpers
        # standard helpers:
        helpers JunglePath::API::Helpers::DataCache
        helpers JunglePath::API::Helpers::Logging
        helpers JunglePath::API::Helpers::Result
        helpers JunglePath::API::Helpers::Auth
        helpers JunglePath::API::Helpers::QueryFilters
end
set_default_rack_middleware(logger, issue_challenge=true) click to toggle source

default mixin that may be included in your Sinatra application class.

# File lib/jungle_path/api/helpers/defaults.rb, line 13
def set_default_rack_middleware logger, issue_challenge=true
        use ::Rack::MobileDetect
        use ::Rack::CommonLogger, logger
        use JunglePath::Rack::JsonBodyParser, true
        # This is rack middleware that adds 'REMOTE_USER' and 'REMOTE_PASSWORD'
        # keys with their associated basic auth values to request.env (if present in the HTTP header).
        # The "Authorization: Basic ..."" header must be present. Looks like this:
        #     Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
        # If this header is missing or incorrect. Error codes are returned.
        # See basic_credentials.rb for details.
        use JunglePath::Rack::BasicCredentials::Basic, "Basic Authentication Required.", issue_challenge
end