module Hcheck::ApplicationHelpers::Responders

Sinatra app controller helpers

Public Instance Methods

h_status() click to toggle source
# File lib/hcheck/application/helpers/responders.rb, line 7
def h_status
  authenticate!(params) if secured_access_enabled?

  @status = Hcheck.status

  if @status.find { |s| s[:status] == 'bad' }
    status 503
  else
    status 200
  end

  haml :index
rescue Hcheck::Errors::InvalidAuthentication, Hcheck::Errors::IncompleteAuthSetup => e
  status 401
  @msg = e.message

  haml :error
end
respond_with(message, status_code, view) click to toggle source
# File lib/hcheck/application/helpers/responders.rb, line 26
def respond_with(message, status_code, view)
  status status_code
  @msg = message

  haml view
end

Private Instance Methods

access_precheck!() click to toggle source
# File lib/hcheck/application/helpers/responders.rb, line 46
def access_precheck!
  # throw error when hcheck secure is enabled but token is not set yet
  raise Hcheck::Errors::IncompleteAuthSetup unless ENV['HCHECK_ACCESS_TOKEN'].present?

  # throw error when token is not sent
  raise Hcheck::Errors::InvalidAuthentication unless @token.present?
end
authenticate!(params) click to toggle source
# File lib/hcheck/application/helpers/responders.rb, line 39
def authenticate!(params)
  @token = params[:token]
  access_precheck!

  raise Hcheck::Errors::InvalidAuthentication unless ENV['HCHECK_ACCESS_TOKEN'].eql?(@token)
end
secured_access_enabled?() click to toggle source
# File lib/hcheck/application/helpers/responders.rb, line 35
def secured_access_enabled?
  ENV['HCHECK_SECURE']
end