Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: organize OAS models into Spec module. #13

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ group :development, :test do
end

# Start debugger with binding.b [https://github.com/ruby/debug]
# gem "debug", ">= 1.0.0"
gem "debug", ">= 1.0.0"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ GEM
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
drb (2.2.1)
erubi (1.13.0)
esquema (0.1.2)
Expand Down Expand Up @@ -212,6 +215,7 @@ PLATFORMS

DEPENDENCIES
bcrypt (~> 3.1.7)
debug (>= 1.0.0)
factory_bot_rails
faker
jwt
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/oas_rails/oas_rails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
respond_to do |format|
format.html
format.json do
render json: Specification.new.to_json, status: :ok
render json: OasRails.build.to_json, status: :ok
end
end
end
Expand Down
39 changes: 23 additions & 16 deletions lib/oas_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ module OasRails
require "oas_rails/version"
require "oas_rails/engine"

autoload :OasBase, "oas_rails/oas_base"
autoload :Configuration, "oas_rails/configuration"
autoload :Specification, "oas_rails/specification"
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"

autoload :Utils, "oas_rails/utils"
autoload :EsquemaBuilder, "oas_rails/esquema_builder"

# This module contains all the clases that represent a part of the OAS file.
module Spec
autoload :Specable, "oas_rails/spec/specable"
autoload :Parameter, "oas_rails/spec/parameter"
autoload :License, "oas_rails/spec/license"
autoload :Response, "oas_rails/spec/response"
autoload :PathItem, "oas_rails/spec/path_item"
autoload :Operation, "oas_rails/spec/operation"
autoload :RequestBody, "oas_rails/spec/request_body"
autoload :Responses, "oas_rails/spec/responses"
autoload :MediaType, "oas_rails/spec/media_type"
autoload :Paths, "oas_rails/spec/paths"
autoload :Contact, "oas_rails/spec/contact"
autoload :Info, "oas_rails/spec/info"
autoload :Server, "oas_rails/spec/server"
autoload :Tag, "oas_rails/spec/tag"
autoload :Specification, "oas_rails/spec/specification"
end

module YARD
autoload :OasYARDFactory, 'oas_rails/yard/oas_yard_factory'
end
Expand All @@ -37,6 +40,10 @@ module Extractors
end

class << self
def build
Spec::Specification.new.to_spec
end

# Configurations for make the OasRails engine Work.
def configure
OasRails.configure_yard!
Expand Down
8 changes: 4 additions & 4 deletions lib/oas_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Configuration
attr_reader :servers, :tags, :security_schema

def initialize
@info = Info.new
@info = Spec::Info.new
@servers = default_servers
@tags = []
@swagger_version = '3.1.0'
Expand All @@ -24,15 +24,15 @@ def security_schema=(value)
end

def default_servers
[Server.new(url: "http://localhost:3000", description: "Rails Default Development Server")]
[Spec::Server.new(url: "http://localhost:3000", description: "Rails Default Development Server")]
end

def servers=(value)
@servers = value.map { |s| Server.new(url: s[:url], description: s[:description]) }
@servers = value.map { |s| Spec::Server.new(url: s[:url], description: s[:description]) }
end

def tags=(value)
@tags = value.map { |t| Tag.new(name: t[:name], description: t[:description]) }
@tags = value.map { |t| Spec::Tag.new(name: t[:name], description: t[:description]) }
end

def excluded_columns_incoming
Expand Down
12 changes: 0 additions & 12 deletions lib/oas_rails/contact.rb

This file was deleted.

10 changes: 5 additions & 5 deletions lib/oas_rails/extractors/render_response_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class << self
def extract_responses_from_source(source:)
render_calls = extract_render_calls(source)

return [Response.new(code: 204, description: "No Content", content: {})] if render_calls.empty?
return [Spec::Response.new(code: 204, description: "No Content", content: {})] if render_calls.empty?

render_calls.map { |render_content, status| process_render_content(render_content.strip, status) }
end
Expand All @@ -33,10 +33,10 @@ def extract_render_calls(source)
def process_render_content(content, status)
schema, examples = build_schema_and_examples(content)
status_int = status_to_integer(status)
Response.new(
Spec::Response.new(
code: status_int,
description: status_code_to_text(status_int),
content: { "application/json": MediaType.new(schema:, examples:) }
content: { "application/json": Spec::MediaType.new(schema:, examples:) }
)
end

Expand Down Expand Up @@ -84,7 +84,7 @@ def process_non_hash_content(content)
# @return [Array<Hash, Hash>] An array where the first element is the schema and the second is the examples.
def build_singular_model_schema_and_examples(_maybe_a_model, errors, klass, schema)
if errors.nil?
[schema, MediaType.search_for_examples_in_tests(klass:, context: :outgoing)]
[schema, Spec::MediaType.search_for_examples_in_tests(klass:, context: :outgoing)]
else
[
{
Expand Down Expand Up @@ -112,7 +112,7 @@ def build_singular_model_schema_and_examples(_maybe_a_model, errors, klass, sche
# @param schema [Hash] The schema for the model.
# @return [Array<Hash, Hash>] An array where the first element is the schema and the second is the examples.
def build_array_model_schema_and_examples(maybe_a_model, klass, schema)
examples = { maybe_a_model => { value: MediaType.search_for_examples_in_tests(klass:, context: :outgoing).values.map { |p| p.dig(:value, maybe_a_model.singularize.to_sym) } } }
examples = { maybe_a_model => { value: Spec::MediaType.search_for_examples_in_tests(klass:, context: :outgoing).values.map { |p| p.dig(:value, maybe_a_model.singularize.to_sym) } } }
[{ type: "array", items: schema }, examples]
end

Expand Down
11 changes: 0 additions & 11 deletions lib/oas_rails/license.rb

This file was deleted.

102 changes: 0 additions & 102 deletions lib/oas_rails/media_type.rb

This file was deleted.

30 changes: 0 additions & 30 deletions lib/oas_rails/oas_base.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/oas_rails/oas_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def controller_path_extractor(controller)

def detect_request_body
klass = @controller.singularize.camelize.constantize
RequestBody.from_model_class(klass:, required: true)
Spec::RequestBody.from_model_class(klass:, required: true)
end
end
end
Loading