Class: SwaggerDocsRails::ControllerParser

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_docs_rails/controller_parser.rb

Constant Summary collapse

CRUD_ACTIONS =
%w[index show create update destroy].freeze
CUSTOM_HTTP_HINTS =

Heurística nome → HTTP para ações customizadas

{
  /\A(import|importar|upload|enviar|processar|executar|sincronizar|
      gerar|calcular|acionar|criar|cadastrar)/x => :post,
  /\A(download|exportar|relatorio|report|preview|visualizar|listar|buscar)/x => :get,
  /\A(ativar|desativar|aprovar|rejeitar|cancelar|bloquear|
      desbloquear|definir|remover|atualizar|alterar)/x => :patch,
  /\Adelete_/ => :delete
}.freeze
FIELD_TYPE_MAP =

Tipo OpenAPI pelo nome do campo

{
  /_id\z/                                          => { type: "integer", format: "int64" },
  /\Aemail|_email\z/                               => { type: "string",  format: "email" },
  /password|senha/                                 => { type: "string",  format: "password" },
  /\Acpf\z|\Acnpj\z|cpf_cnpj/                      => { type: "string" },
  /\Adata_|\A.*_at\z|data_nascimento/              => { type: "string",  format: "date" },
  /valor|preco|percentual|taxa/                    => { type: "number",  format: "double" },
  /\Aativo\z|proprio|embutido|possui|admin|bloqueado/ => { type: "boolean" },
  /nivel|numero|tentativas/                        => { type: "integer" }
}.freeze
MULTIPART_INDICATORS =

Indicadores de upload no corpo de uma ação ou nos seus params privados

[
  /\.original_filename\b/,
  /\.content_type\b/,
  /\.read\b/,
  /Roo::/,
  /ActionDispatch.*UploadedFile/,
  /CarrierWave|Shrine|ActiveStorage\.attach/,
  /params\[:arquivo\]/,
  /params\[:file\]/,
  /params\[:anexo\]/,
  /params\[:imagem\]/,
  /params\[:foto\]/,
  /\.tempfile\b/
].freeze
FILE_FIELD_PATTERN =

Padrões de campo de arquivo pelo nome (excluindo _url)

/
  \Aimagem_|\Afoto_|\Aanexo_|\Adocumento_|\Aplanilha_|  # prefixo
  _imagem\z|_foto\z|_anexo\z|_arquivo\z|               # sufixo
  \Aarquivo\z|\Afile\z|_file\z|                         # exatos
  \Aanexo\z|\Aimagem\z|\Afoto\z|                        # exatos pt-br
  \Alogo\z|\Aavatar\z|\Athumbnail\z|\Acover\z|          # imagens comuns
  \Adocumento\z|\Aplanilha\z|\Arelatorio\z              # docs
/x.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths = nil) ⇒ ControllerParser

Returns a new instance of ControllerParser.



70
71
72
73
74
75
# File 'lib/swagger_docs_rails/controller_parser.rb', line 70

def initialize(paths = nil)
  @search_paths = Array(paths || [Rails.root.join("app", "controllers", "api")])
  @controllers  = []
  @routes_map   = parse_routes_file!
  parse!
end

Instance Attribute Details

#controllersObject (readonly)

Returns the value of attribute controllers.



68
69
70
# File 'lib/swagger_docs_rails/controller_parser.rb', line 68

def controllers
  @controllers
end

Class Method Details

.infer_field_type(field_name) ⇒ Object



81
82
83
84
85
86
# File 'lib/swagger_docs_rails/controller_parser.rb', line 81

def self.infer_field_type(field_name)
  FIELD_TYPE_MAP.each do |pattern, type_def|
    return type_def.dup if field_name.to_s.match?(pattern)
  end
  { type: "string" }
end

Instance Method Details

#to_resourcesObject



77
78
79
# File 'lib/swagger_docs_rails/controller_parser.rb', line 77

def to_resources
  @controllers
end