module OasRails::Utils
def class_to_symbol(klass)
def class_to_symbol(klass) klass.name.underscore.to_sym end
def detect_test_framework
Method for detect test framework of the Rails App
def detect_test_framework if defined?(FactoryBot) :factory_bot elsif ActiveRecord::Base.connection.table_exists?('ar_internal_metadata') :fixtures else :unknown end end
def find_model_from_route(path)
def find_model_from_route(path) parts = path.split('/') model_name = parts.last.singularize.camelize namespace_combinations = (0..parts.size).map do |i| parts.first(i).map(&:camelize).join('::') end namespace_combinations.reverse.each do |namespace| full_class_name = [namespace, model_name].reject(&:empty?).join('::') begin return full_class_name.constantize rescue NameError next end end nil # Return nil if no matching constant is found end
def get_definition(status_code)
-
(String)
- The text description of the status code.
Parameters:
-
status_code
(Integer
) -- The status code.
def get_definition(status_code) HTTP_STATUS_DEFINITIONS[status_code] || "Definition not found for status code #{status_code}" end
def hash_to_json_schema(hash)
def hash_to_json_schema(hash) { type: 'object', properties: hash_to_properties(hash), required: [] } end
def hash_to_properties(hash)
def hash_to_properties(hash) hash.transform_values do |value| if value.is_a?(Hash) hash_to_json_schema(value) elsif value.is_a?(Class) { type: ruby_type_to_json_type(value.name) } else { type: ruby_type_to_json_type(value.class.name) } end end end
def ruby_type_to_json_type(ruby_type)
def ruby_type_to_json_type(ruby_type) TYPE_MAPPING.fetch(ruby_type, 'string') end
def status_to_integer(status)
-
(Integer)
- The status code as an integer.
Parameters:
-
status
(String, Symbol, nil
) -- The status to convert.
def status_to_integer(status) return 200 if status.nil? if status.to_s =~ /^\d+$/ status.to_i else Rack::Utils.status_code(status.to_sym) end end
def type_to_schema(type_string)
def type_to_schema(type_string) if type_string.start_with?('Array<') inner_type = type_string[/Array<(.+)>$/, 1] { "type" => "array", "items" => type_to_schema(inner_type) } else { "type" => TYPE_MAPPING.fetch(type_string, 'string') } end end