Module: OasRails::Utils
- Defined in:
- lib/oas_rails/utils.rb
Constant Summary collapse
- TYPE_MAPPING =
{ 'String' => 'string', 'Integer' => 'number', 'Float' => 'number', 'TrueClass' => 'boolean', 'FalseClass' => 'boolean', 'Boolean' => 'boolean', 'NilClass' => 'null', 'Hash' => 'object', 'Object' => 'object', 'DateTime' => 'string' }.freeze
- HTTP_STATUS_DEFINITIONS =
{ 200 => "The request has succeeded.", 201 => "The request has been fulfilled and resulted in a new resource being created.", 404 => "The requested resource could not be found.", 401 => "You are not authorized to access this resource. You need to authenticate yourself first.", 403 => "You are not allowed to access this resource. You do not have the necessary permissions.", 500 => "An unexpected error occurred on the server. The server was unable to process the request.", 422 => "The server could not process the request due to semantic errors. Please check your input and try again." }.freeze
Class Method Summary collapse
-
.active_record_class?(klass_or_string) ⇒ Boolean
Checks if a given text refers to an ActiveRecord class.
- .class_to_symbol(klass) ⇒ Object
-
.detect_test_framework ⇒ Object
Method for detect test framework of the Rails App It is used for generate examples in operations.
- .find_model_from_route(path) ⇒ Object
-
.get_definition(status_code) ⇒ String
Converts a status code to its corresponding text description.
- .hash_to_json_schema(hash) ⇒ Object
- .hash_to_properties(hash) ⇒ Object
- .ruby_type_to_json_type(ruby_type) ⇒ Object
-
.status_to_integer(status) ⇒ Integer
Converts a status symbol or string to an integer.
Class Method Details
.active_record_class?(klass_or_string) ⇒ Boolean
Checks if a given text refers to an ActiveRecord class.
112 113 114 115 116 117 |
# File 'lib/oas_rails/utils.rb', line 112 def active_record_class?(klass_or_string) klass = klass_or_string.is_a?(Class) ? klass_or_string : klass_or_string.constantize klass.ancestors.map(&:to_s).include? 'ActiveRecord::Base' rescue StandardError false end |
.class_to_symbol(klass) ⇒ Object
85 86 87 |
# File 'lib/oas_rails/utils.rb', line 85 def class_to_symbol(klass) klass.name.underscore.to_sym end |
.detect_test_framework ⇒ Object
Method for detect test framework of the Rails App It is used for generate examples in operations
29 30 31 32 33 34 35 36 37 |
# File 'lib/oas_rails/utils.rb', line 29 def detect_test_framework if defined?(FactoryBot) :factory_bot elsif ActiveRecord::Base.connection.table_exists?('ar_internal_metadata') :fixtures else :unknown end end |
.find_model_from_route(path) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/oas_rails/utils.rb', line 89 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 |
.get_definition(status_code) ⇒ String
Converts a status code to its corresponding text description.
81 82 83 |
# File 'lib/oas_rails/utils.rb', line 81 def get_definition(status_code) HTTP_STATUS_DEFINITIONS[status_code] || "Definition not found for status code #{status_code}" end |
.hash_to_json_schema(hash) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/oas_rails/utils.rb', line 39 def hash_to_json_schema(hash) { type: 'object', properties: hash_to_properties(hash), required: [] } end |
.hash_to_properties(hash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/oas_rails/utils.rb', line 47 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 |
.ruby_type_to_json_type(ruby_type) ⇒ Object
59 60 61 |
# File 'lib/oas_rails/utils.rb', line 59 def ruby_type_to_json_type(ruby_type) TYPE_MAPPING.fetch(ruby_type, 'string') end |
.status_to_integer(status) ⇒ Integer
Converts a status symbol or string to an integer.
67 68 69 70 71 72 73 74 75 |
# File 'lib/oas_rails/utils.rb', line 67 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 |