Class: Api::V2::InfoController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/api/v2/info_controller.rb

Overview

require ‘model_driven_api/version’

Direct Known Subclasses

Api::V3::InfoController

Instance Method Summary collapse

Instance Method Details

#dslObject

GET ‘/api/v2/info/dsl’



114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/api/v2/info_controller.rb', line 114

def dsl
  pivot = {}
  ApplicationRecord.subclasses.each do |d|
    # Only if current user can read the model
    if can? :read, d
      model = d.to_s.underscore.tableize
      pivot[model] = (d.instance_methods(false).include?(:json_attrs) && !d.json_attrs.blank?) ? d.json_attrs : nil
    end
  end
  render json: pivot.to_json, status: 200
end

#heartbeatObject

api :GET, ‘/api/v2/info/heartbeat’ Just keeps the session alive by returning a new token



20
21
22
# File 'app/controllers/api/v2/info_controller.rb', line 20

def heartbeat
  render json: current_user.to_json, status: 200
end

#ntpObject

api :GET, ‘/api/v2/info/ntp’ Returns the current server time in utc, to check if the client time is synchronized with the server time



26
27
28
# File 'app/controllers/api/v2/info_controller.rb', line 26

def ntp
  render json: { server_time_utc: Time.current.utc }.to_json, status: 200
end

#openapiObject Also known as: swagger

GET ‘/api/v2/info/schema’



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/api/v2/info_controller.rb', line 69

def openapi
  uri = URI(request.url)
  pivot = {
    "openapi": "3.0.0",
    "info": {
      "title": "#{Settings.ns(:main).app_name} API",
      "description": Api::OpenApi::V2.new(ApplicationRecord.subclasses, request).description,
      "version": "v2",
    },
    "servers": [
      {
        # i.e. "http://localhost:3001/api/v2"
        "url": "#{uri.scheme}://#{uri.host}#{":#{uri.port}" if uri.port.present?}/api/v2",
        "description": "The URL at which this API responds.",
      },
    ],
    # 1) Define the security scheme type (HTTP bearer)
    "components": {
      "securitySchemes": {
        "basicAuth": {
          "type": "http",
          "scheme": "basic",
        },
        "bearerAuth": { # arbitrary name for the security scheme
          "type": "http",
          "scheme": "bearer",
          "bearerFormat": "JWT", # optional, arbitrary value for documentation purposes
        },
      },
    },
    # 2) Apply the security globally to all operations
    "security": [
      {
        "bearerAuth": [], # use the same name as above
      },
    ],
    "paths": Api::OpenApi::V2.new(ApplicationRecord.subclasses, request).generate,
  }

  render json: pivot.to_json, status: 200
end

#rolesObject

api :GET, ‘/api/v2/info/roles’ it returns the roles list



14
15
16
# File 'app/controllers/api/v2/info_controller.rb', line 14

def roles
  render json: ::Role.all.to_json, status: 200
end

#schemaObject

GET ‘/api/v2/info/schema’



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/api/v2/info_controller.rb', line 36

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|
    # Only if current user can read the model
    if can? :read, d
      model = d.to_s.underscore.tableize
      pivot[model] ||= {}
      d.columns_hash.each_pair do |key, val|
        pivot[model][key] = val.type unless key.ends_with? "_id"
      end
      # Only application record descendants to have a clean schema
      pivot[model][:associations] ||= {
        has_many: d.reflect_on_all_associations(:has_many).map { |a|
          a.name if (((a.options[:class_name].presence || a.name).to_s.classify.constantize.new.is_a? ApplicationRecord) rescue false)
        }.compact,
        has_one: d.reflect_on_all_associations(:has_one).map { |a|
          a.name if (((a.options[:class_name].presence || a.name).to_s.classify.constantize.new.is_a? ApplicationRecord) rescue false)
        }.compact,
        belongs_to: d.reflect_on_all_associations(:belongs_to).map { |a|
          a.name if (((a.options[:class_name].presence || a.name).to_s.classify.constantize.new.is_a? ApplicationRecord) rescue false)
        }.compact,
      }
      pivot[model][:methods] ||= (d.instance_methods(false).include?(:json_attrs) && !d.json_attrs.blank?) ? d.json_attrs[:methods] : nil
    end
  end
  render json: pivot.to_json, status: 200
end

#settingsObject



126
127
128
# File 'app/controllers/api/v2/info_controller.rb', line 126

def settings
  render json: ThecoreSettings::Setting.pluck(:ns, :key, :raw).inject({}) { |result, array| (result[array.first] ||= {})[array.second] = array.third; result }.to_json, status: 200
end

#translationsObject

GET ‘/api/v2/info/translations’



31
32
33
# File 'app/controllers/api/v2/info_controller.rb', line 31

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

#versionObject

api :GET, ‘/api/v2/info/version’, “Just prints the APPVERSION.”



8
9
10
# File 'app/controllers/api/v2/info_controller.rb', line 8

def version
  render json: { version: "TODO: Find a Way to Dynamically Obtain It" }.to_json, status: 200
end