Module: OasCore::Utils

Defined in:
lib/oas_core/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
STATUS_SYMBOL_MAPPING =
{
  ok: 200,
  created: 201,
  no_content: 204,
  bad_request: 400,
  unauthorized: 401,
  forbidden: 403,
  not_found: 404,
  unprocessable_entity: 422,
  internal_server_error: 500
}.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

Class Method Details

.class_to_symbol(klass) ⇒ Object



89
90
91
# File 'lib/oas_core/utils.rb', line 89

def class_to_symbol(klass)
  klass.name.underscore.to_sym
end

.get_definition(status_code) ⇒ String

Converts a status code to its corresponding text description.

Parameters:

  • status_code (Integer)

    The status code.

Returns:

  • (String)

    The text description of the status code.



85
86
87
# File 'lib/oas_core/utils.rb', line 85

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



41
42
43
44
45
46
47
# File 'lib/oas_core/utils.rb', line 41

def hash_to_json_schema(hash)
  {
    type: 'object',
    properties: hash_to_properties(hash),
    required: []
  }
end

.hash_to_properties(hash) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/oas_core/utils.rb', line 49

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



61
62
63
# File 'lib/oas_core/utils.rb', line 61

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.

Parameters:

  • status (String, Symbol, nil)

    The status to convert.

Returns:

  • (Integer)

    The status code as an integer.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/oas_core/utils.rb', line 69

def status_to_integer(status)
  return 200 if status.nil?

  if status.to_s =~ /^\d+$/
    status.to_i
  else
    STATUS_SYMBOL_MAPPING.fetch(status.to_sym) do
      raise ArgumentError, "Unknown status symbol: #{status}"
    end
  end
end