lib/oas_rails.rb in oas_rails-0.1.1 vs lib/oas_rails.rb in oas_rails-0.2.0

- old
+ new

@@ -1,33 +1,38 @@ require "yard" require "method_source" require "esquema" -require_relative 'oas_rails/version' -require_relative 'oas_rails/engine' -require_relative 'oas_rails/oas_base' -require_relative 'oas_rails/configuration' -require_relative 'oas_rails/specification' -require_relative 'oas_rails/route_extractor' -require_relative 'oas_rails/oas_route' -require_relative 'oas_rails/operation' +module OasRails + require "oas_rails/version" + require "oas_rails/engine" -require_relative 'oas_rails/info' -require_relative 'oas_rails/contact' -require_relative 'oas_rails/paths' -require_relative 'oas_rails/path_item' -require_relative 'oas_rails/parameter' -require_relative 'oas_rails/tag' -require_relative 'oas_rails/license' -require_relative 'oas_rails/server' -require_relative "oas_rails/request_body" -require_relative "oas_rails/media_type" -require_relative 'oas_rails/yard/oas_yard_factory' -require_relative "oas_rails/response" -require_relative "oas_rails/responses" + autoload :OasBase, "oas_rails/oas_base" + autoload :Configuration, "oas_rails/configuration" + autoload :Specification, "oas_rails/specification" + autoload :RouteExtractor, "oas_rails/route_extractor" + autoload :OasRoute, "oas_rails/oas_route" + autoload :Operation, "oas_rails/operation" + autoload :Info, "oas_rails/info" + autoload :Contact, "oas_rails/contact" + autoload :Paths, "oas_rails/paths" + autoload :PathItem, "oas_rails/path_item" + autoload :Parameter, "oas_rails/parameter" + autoload :Tag, "oas_rails/tag" + autoload :License, "oas_rails/license" + autoload :Server, "oas_rails/server" + autoload :RequestBody, "oas_rails/request_body" + autoload :MediaType, "oas_rails/media_type" + autoload :Response, "oas_rails/response" + autoload :Responses, "oas_rails/responses" -module OasRails + autoload :Utils, "oas_rails/utils" + + module Yard + autoload :OasYardFactory, 'oas_rails/yard/oas_yard_factory' + end + class << self def configure yield config end @@ -41,11 +46,13 @@ 'Request body' => [:request_body, :with_request_body], 'Request body Example' => [:request_body_example, :with_request_body_example], 'Parameter' => [:parameter, :with_parameter], 'Response' => [:response, :with_response], 'Endpoint Tags' => [:tags], - 'Summary' => [:summary] + 'Summary' => [:summary], + 'No Auth' => [:no_auth], + 'Auth methods' => [:auth, :with_types] } yard_tags.each do |tag_name, (method_name, handler)| ::YARD::Tags::Library.define_tag(tag_name, method_name, handler) end end @@ -54,67 +61,8 @@ Esquema.configure do |config| config.exclude_associations = true config.exclude_foreign_keys = true config.excluded_columns = %i[id created_at updated_at deleted_at] end - end - - def detect_test_framework - if defined?(FactoryBot) - :factory_bot - elsif ActiveRecord::Base.connection.table_exists?('ar_internal_metadata') - :fixtures - else - :unknown - end - end - - TYPE_MAPPING = { - 'String' => 'string', - 'Integer' => 'number', - 'Float' => 'number', - 'TrueClass' => 'boolean', - 'FalseClass' => 'boolean', - 'Boolean' => 'boolean', - 'NilClass' => 'null', - 'Hash' => 'object', - 'Object' => 'object', - 'DateTime' => 'string' - }.freeze - - 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 - - def hash_to_json_schema(hash) - { - type: 'object', - properties: hash_to_properties(hash), - required: [] - } - end - - 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) - TYPE_MAPPING.fetch(ruby_type, 'string') end end end