Class: Api::V1::InfoController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/info_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_user! ⇒ Object

Method overridden because the first time I have to ask for the token



53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/api/v1/info_controller.rb', line 53

def authenticate_user!
  username, password = ActionController::HttpAuthentication::Basic.user_name_and_password(request)
  if username
    user = User.find_by(username: username)
  end
  if user && user.valid_password?(password)
    @current_user = user
  else
    return unauthenticated!
  end
end

#available_roles ⇒ Object

api :GET, '/api/v1/info/available_roles', "Given auth credentials, in HTTP_BASIC form, it returns the roles list



25
26
27
# File 'app/controllers/api/v1/info_controller.rb', line 25

def available_roles
  render json: ROLES.to_json, status: 200
end

#schema ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/api/v1/info_controller.rb', line 34

def schema
  pivot = {}
  if Rails.env.development?
    Rails.configuration.eager_load_namespaces.each(&:eager_load!) if Rails.version.to_i == 5 #Rails 5
    Zeitwerk::Loader.eager_load_all if Rails.version.to_i >= 6 #Rails 6
  end
  ApplicationRecord.subclasses.each do |d|
    model = d.underscore.parameterize.underscore
    pivot[model] ||= {}
    d.columns_hash.each_pair do |key, val| 
      pivot[model][key] = val.type 
    end 
  end
  render json: pivot.to_json, status: 200
end

#token ⇒ Object

api :GET, '/api/v1/info/token', "Given auth credentials, in HTTP_BASIC form, it returns the AUTH_TOKEN, email and id of the user which performed the authentication." api!



16
17
18
19
20
21
# File 'app/controllers/api/v1/info_controller.rb', line 16

def token
  render json: {
    token: @current_user.authentication_token,
    email: @current_user.email
  }.to_json, status: 200
end

#translations ⇒ Object

GET '/api/v1/info/translations'



30
31
32
# File 'app/controllers/api/v1/info_controller.rb', line 30

def translations
  render json: I18n.t(".", locale: (params[:locale].presence || :it)).to_json, status: 200
end

#version ⇒ Object

api :GET, '/api/v1/info/version', "Just prints the APPVERSION." api!



7
8
9
10
11
# File 'app/controllers/api/v1/info_controller.rb', line 7

def version
  render json: {
    version: (APPVERSION rescue "No version specified for this app, please add an APPVERSION constant to an initializer to start versioning the application.")
  }.to_json, status: 200
end