Methods
D
I
Included Modules
Constants
PATH_IGNORES = ["Rhino::OpenApiInfo", "Rhino::InfoGraph"].freeze
 
RESPONSE_CODES = [200, 400, 403, 422].freeze
 

gist.github.com/bantic/5688232#gistcomment-3274704

SUCCESS_CODES = [200].freeze
 
Class Public methods
describe()
   # File rhino/rhino/app/resources/rhino/open_api_info.rb
13 def self.describe
14   nil
15 end
describe_info()
    # File rhino/rhino/app/resources/rhino/open_api_info.rb
 98 def self.describe_info
 99   {
100     title: "#{Rails.application.class.module_parent_name} API",
101     version: '0.0.0',
102     'x-rhino': {
103       modules: Rhino.registered_modules
104     }
105   }
106 end
describe_parameter(param)
   # File rhino/rhino/app/resources/rhino/open_api_info.rb
32 def self.describe_parameter(param)
33   {
34     name: param,
35     in: "path",
36     required: true
37   }
38 end
describe_path(path)
   # File rhino/rhino/app/resources/rhino/open_api_info.rb
59 def self.describe_path(path) # rubocop:todo Metrics/AbcSize
60   new_path = path.index_by { |p| p[:verb].downcase.to_sym }
61   new_path.transform_values! do |verb_values|
62     klass = verb_values[:rhino_resource].constantize
63 
64     verb_info = {
65       operationId: "#{klass.model_name.singular}-#{verb_values[:action]}",
66       parameters: verb_values[:required_names].map { |param| describe_parameter(param) },
67       responses: describe_path_responses(klass),
68       tags: [klass.model_name.singular]
69     }
70 
71     verb_info
72   end
73 
74   new_path
75 end
describe_path_content(klass, code)
   # File rhino/rhino/app/resources/rhino/open_api_info.rb
43 def self.describe_path_content(klass, code)
44   {
45     'application/json':
46       ({ schema: { "$ref": "#/components/schemas/#{klass.model_name.singular}" } } if SUCCESS_CODES.include?(code)) || {}
47   }
48 end
describe_path_responses(klass)
   # File rhino/rhino/app/resources/rhino/open_api_info.rb
50 def self.describe_path_responses(klass)
51   RESPONSE_CODES.index_with do |code|
52     {
53       description: Rack::Utils::HTTP_STATUS_CODES[code],
54       content: describe_path_content(klass, code)
55     }
56   end
57 end
describe_paths()
   # File rhino/rhino/app/resources/rhino/open_api_info.rb
78 def self.describe_paths # rubocop:todo Metrics/AbcSize
79   routes = Rails.application.routes.routes
80 
81   # Extract the path information we need
82   paths = routes.map { |r| r.defaults.merge(verb: r.verb, path: r.path.spec.to_s, required_names: r.path.required_names) }
83   paths = paths.group_by { |r| r[:path] }
84 
85   # Get rid of non-rhino paths
86   # Rhino paths have rhino_resource set as an attribute when the routes are created
87   paths.transform_values! { |v| v.select { |p| p[:rhino_resource].present? && PATH_IGNORES.exclude?(p[:rhino_resource]) } }
88 
89   # Remove empty hashes
90   paths.compact_blank!
91 
92   # For each path, list the verbs it supports
93   paths.transform_values! do |path|
94     describe_path(path)
95   end
96 end
describe_schemas()
   # File rhino/rhino/app/resources/rhino/open_api_info.rb
28 def self.describe_schemas
29   Rhino.resource_classes.index_with(&:describe).compact.transform_keys { |r| r.model_name.singular }
30 end
index()
   # File rhino/rhino/app/resources/rhino/open_api_info.rb
17 def self.index
18   JSON.pretty_generate({
19     openapi: '3.0.3',
20     components: {
21       schemas: describe_schemas
22     },
23     paths: describe_paths,
24     info: describe_info
25   })
26 end