oas_rails 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -0
  3. data/app/controllers/oas_rails/oas_rails_controller.rb +1 -1
  4. data/lib/generators/oas_rails/config/templates/oas_rails_initializer.rb +11 -0
  5. data/lib/oas_rails/builders/content_builder.rb +55 -0
  6. data/lib/oas_rails/builders/operation_builder.rb +32 -0
  7. data/lib/oas_rails/builders/parameter_builder.rb +28 -0
  8. data/lib/oas_rails/builders/parameters_builder.rb +39 -0
  9. data/lib/oas_rails/builders/path_item_builder.rb +22 -0
  10. data/lib/oas_rails/builders/request_body_builder.rb +60 -0
  11. data/lib/oas_rails/builders/response_builder.rb +40 -0
  12. data/lib/oas_rails/builders/responses_builder.rb +58 -0
  13. data/lib/oas_rails/configuration.rb +17 -5
  14. data/lib/oas_rails/extractors/oas_route_extractor.rb +66 -0
  15. data/lib/oas_rails/extractors/render_response_extractor.rb +11 -36
  16. data/lib/oas_rails/oas_route.rb +0 -5
  17. data/lib/oas_rails/spec/components.rb +85 -0
  18. data/lib/oas_rails/spec/contact.rb +18 -0
  19. data/lib/oas_rails/spec/hashable.rb +39 -0
  20. data/lib/oas_rails/{info.rb → spec/info.rb} +30 -24
  21. data/lib/oas_rails/spec/license.rb +18 -0
  22. data/lib/oas_rails/spec/media_type.rb +84 -0
  23. data/lib/oas_rails/spec/operation.rb +25 -0
  24. data/lib/oas_rails/spec/parameter.rb +34 -0
  25. data/lib/oas_rails/spec/path_item.rb +33 -0
  26. data/lib/oas_rails/spec/paths.rb +26 -0
  27. data/lib/oas_rails/spec/reference.rb +16 -0
  28. data/lib/oas_rails/spec/request_body.rb +21 -0
  29. data/lib/oas_rails/spec/response.rb +20 -0
  30. data/lib/oas_rails/spec/responses.rb +25 -0
  31. data/lib/oas_rails/spec/server.rb +17 -0
  32. data/lib/oas_rails/spec/specable.rb +51 -0
  33. data/lib/oas_rails/spec/specification.rb +50 -0
  34. data/lib/oas_rails/spec/tag.rb +18 -0
  35. data/lib/oas_rails/utils.rb +39 -0
  36. data/lib/oas_rails/version.rb +1 -1
  37. data/lib/oas_rails.rb +41 -16
  38. metadata +29 -17
  39. data/lib/oas_rails/contact.rb +0 -12
  40. data/lib/oas_rails/license.rb +0 -11
  41. data/lib/oas_rails/media_type.rb +0 -102
  42. data/lib/oas_rails/oas_base.rb +0 -30
  43. data/lib/oas_rails/operation.rb +0 -134
  44. data/lib/oas_rails/parameter.rb +0 -47
  45. data/lib/oas_rails/path_item.rb +0 -25
  46. data/lib/oas_rails/paths.rb +0 -19
  47. data/lib/oas_rails/request_body.rb +0 -29
  48. data/lib/oas_rails/response.rb +0 -12
  49. data/lib/oas_rails/responses.rb +0 -20
  50. data/lib/oas_rails/server.rb +0 -10
  51. data/lib/oas_rails/specification.rb +0 -72
  52. data/lib/oas_rails/tag.rb +0 -17
@@ -13,6 +13,14 @@ module OasRails
13
13
  'DateTime' => 'string'
14
14
  }.freeze
15
15
 
16
+ HTTP_STATUS_DEFINITIONS = {
17
+ 404 => "The requested resource could not be found.",
18
+ 401 => "You are not authorized to access this resource. You need to authenticate yourself first.",
19
+ 403 => "You are not allowed to access this resource. You do not have the necessary permissions.",
20
+ 500 => "An unexpected error occurred on the server. The server was unable to process the request.",
21
+ 422 => "The server could not process the request due to semantic errors. Please check your input and try again."
22
+ }.freeze
23
+
16
24
  class << self
17
25
  # Method for detect test framework of the Rails App
18
26
  # It is used for generate examples in operations
@@ -61,6 +69,37 @@ module OasRails
61
69
  def ruby_type_to_json_type(ruby_type)
62
70
  TYPE_MAPPING.fetch(ruby_type, 'string')
63
71
  end
72
+
73
+ # Converts a status symbol or string to an integer.
74
+ #
75
+ # @param status [String, Symbol, nil] The status to convert.
76
+ # @return [Integer] The status code as an integer.
77
+ def status_to_integer(status)
78
+ return 200 if status.nil?
79
+
80
+ if status.to_s =~ /^\d+$/
81
+ status.to_i
82
+ else
83
+ status = "unprocessable_content" if status == "unprocessable_entity"
84
+ Rack::Utils::SYMBOL_TO_STATUS_CODE[status.to_sym]
85
+ end
86
+ end
87
+
88
+ # Converts a status code to its corresponding text description.
89
+ #
90
+ # @param status_code [Integer] The status code.
91
+ # @return [String] The text description of the status code.
92
+ def status_code_to_text(status_code)
93
+ Rack::Utils::HTTP_STATUS_CODES[status_code] || "Unknown Status Code"
94
+ end
95
+
96
+ def get_definition(status_code)
97
+ HTTP_STATUS_DEFINITIONS[status_code] || "Definition not found for status code #{status_code}"
98
+ end
99
+
100
+ def class_to_symbol(klass)
101
+ klass.name.underscore.to_sym
102
+ end
64
103
  end
65
104
  end
66
105
  end
@@ -1,3 +1,3 @@
1
1
  module OasRails
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/oas_rails.rb CHANGED
@@ -6,27 +6,44 @@ module OasRails
6
6
  require "oas_rails/version"
7
7
  require "oas_rails/engine"
8
8
 
9
- autoload :OasBase, "oas_rails/oas_base"
10
9
  autoload :Configuration, "oas_rails/configuration"
11
- autoload :Specification, "oas_rails/specification"
12
10
  autoload :OasRoute, "oas_rails/oas_route"
13
- autoload :Operation, "oas_rails/operation"
14
- autoload :Info, "oas_rails/info"
15
- autoload :Contact, "oas_rails/contact"
16
- autoload :Paths, "oas_rails/paths"
17
- autoload :PathItem, "oas_rails/path_item"
18
- autoload :Parameter, "oas_rails/parameter"
19
- autoload :Tag, "oas_rails/tag"
20
- autoload :License, "oas_rails/license"
21
- autoload :Server, "oas_rails/server"
22
- autoload :RequestBody, "oas_rails/request_body"
23
- autoload :MediaType, "oas_rails/media_type"
24
- autoload :Response, "oas_rails/response"
25
- autoload :Responses, "oas_rails/responses"
26
-
27
11
  autoload :Utils, "oas_rails/utils"
28
12
  autoload :EsquemaBuilder, "oas_rails/esquema_builder"
29
13
 
14
+ module Builders
15
+ autoload :OperationBuilder, "oas_rails/builders/operation_builder"
16
+ autoload :PathItemBuilder, "oas_rails/builders/path_item_builder"
17
+ autoload :ResponseBuilder, "oas_rails/builders/response_builder"
18
+ autoload :ResponsesBuilder, "oas_rails/builders/responses_builder"
19
+ autoload :ContentBuilder, "oas_rails/builders/content_builder"
20
+ autoload :ParametersBuilder, "oas_rails/builders/parameters_builder"
21
+ autoload :ParameterBuilder, "oas_rails/builders/parameter_builder"
22
+ autoload :RequestBodyBuilder, "oas_rails/builders/request_body_builder"
23
+ end
24
+
25
+ # This module contains all the clases that represent a part of the OAS file.
26
+ module Spec
27
+ autoload :Hashable, "oas_rails/spec/hashable"
28
+ autoload :Specable, "oas_rails/spec/specable"
29
+ autoload :Components, "oas_rails/spec/components"
30
+ autoload :Parameter, "oas_rails/spec/parameter"
31
+ autoload :License, "oas_rails/spec/license"
32
+ autoload :Response, "oas_rails/spec/response"
33
+ autoload :PathItem, "oas_rails/spec/path_item"
34
+ autoload :Operation, "oas_rails/spec/operation"
35
+ autoload :RequestBody, "oas_rails/spec/request_body"
36
+ autoload :Responses, "oas_rails/spec/responses"
37
+ autoload :MediaType, "oas_rails/spec/media_type"
38
+ autoload :Paths, "oas_rails/spec/paths"
39
+ autoload :Contact, "oas_rails/spec/contact"
40
+ autoload :Info, "oas_rails/spec/info"
41
+ autoload :Server, "oas_rails/spec/server"
42
+ autoload :Tag, "oas_rails/spec/tag"
43
+ autoload :Specification, "oas_rails/spec/specification"
44
+ autoload :Reference, "oas_rails/spec/reference"
45
+ end
46
+
30
47
  module YARD
31
48
  autoload :OasYARDFactory, 'oas_rails/yard/oas_yard_factory'
32
49
  end
@@ -34,9 +51,17 @@ module OasRails
34
51
  module Extractors
35
52
  autoload :RenderResponseExtractor, 'oas_rails/extractors/render_response_extractor'
36
53
  autoload :RouteExtractor, "oas_rails/extractors/route_extractor"
54
+ autoload :OasRouteExtractor, "oas_rails/extractors/oas_route_extractor"
37
55
  end
38
56
 
39
57
  class << self
58
+ def build
59
+ oas = Spec::Specification.new
60
+ oas.build
61
+
62
+ oas.to_spec
63
+ end
64
+
40
65
  # Configurations for make the OasRails engine Work.
41
66
  def configure
42
67
  OasRails.configure_yard!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oas_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - a-chacon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-02 00:00:00.000000000 Z
11
+ date: 2024-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: esquema
@@ -100,27 +100,39 @@ files:
100
100
  - lib/generators/oas_rails/config/config_generator.rb
101
101
  - lib/generators/oas_rails/config/templates/oas_rails_initializer.rb
102
102
  - lib/oas_rails.rb
103
+ - lib/oas_rails/builders/content_builder.rb
104
+ - lib/oas_rails/builders/operation_builder.rb
105
+ - lib/oas_rails/builders/parameter_builder.rb
106
+ - lib/oas_rails/builders/parameters_builder.rb
107
+ - lib/oas_rails/builders/path_item_builder.rb
108
+ - lib/oas_rails/builders/request_body_builder.rb
109
+ - lib/oas_rails/builders/response_builder.rb
110
+ - lib/oas_rails/builders/responses_builder.rb
103
111
  - lib/oas_rails/configuration.rb
104
- - lib/oas_rails/contact.rb
105
112
  - lib/oas_rails/engine.rb
106
113
  - lib/oas_rails/esquema_builder.rb
114
+ - lib/oas_rails/extractors/oas_route_extractor.rb
107
115
  - lib/oas_rails/extractors/render_response_extractor.rb
108
116
  - lib/oas_rails/extractors/route_extractor.rb
109
- - lib/oas_rails/info.rb
110
- - lib/oas_rails/license.rb
111
- - lib/oas_rails/media_type.rb
112
- - lib/oas_rails/oas_base.rb
113
117
  - lib/oas_rails/oas_route.rb
114
- - lib/oas_rails/operation.rb
115
- - lib/oas_rails/parameter.rb
116
- - lib/oas_rails/path_item.rb
117
- - lib/oas_rails/paths.rb
118
- - lib/oas_rails/request_body.rb
119
- - lib/oas_rails/response.rb
120
- - lib/oas_rails/responses.rb
121
- - lib/oas_rails/server.rb
122
- - lib/oas_rails/specification.rb
123
- - lib/oas_rails/tag.rb
118
+ - lib/oas_rails/spec/components.rb
119
+ - lib/oas_rails/spec/contact.rb
120
+ - lib/oas_rails/spec/hashable.rb
121
+ - lib/oas_rails/spec/info.rb
122
+ - lib/oas_rails/spec/license.rb
123
+ - lib/oas_rails/spec/media_type.rb
124
+ - lib/oas_rails/spec/operation.rb
125
+ - lib/oas_rails/spec/parameter.rb
126
+ - lib/oas_rails/spec/path_item.rb
127
+ - lib/oas_rails/spec/paths.rb
128
+ - lib/oas_rails/spec/reference.rb
129
+ - lib/oas_rails/spec/request_body.rb
130
+ - lib/oas_rails/spec/response.rb
131
+ - lib/oas_rails/spec/responses.rb
132
+ - lib/oas_rails/spec/server.rb
133
+ - lib/oas_rails/spec/specable.rb
134
+ - lib/oas_rails/spec/specification.rb
135
+ - lib/oas_rails/spec/tag.rb
124
136
  - lib/oas_rails/utils.rb
125
137
  - lib/oas_rails/version.rb
126
138
  - lib/oas_rails/yard/oas_yard_factory.rb
@@ -1,12 +0,0 @@
1
- module OasRails
2
- class Contact < OasBase
3
- attr_accessor :name, :url, :email
4
-
5
- def initialize(**kwargs)
6
- super()
7
- @name = kwargs[:name] || ''
8
- @url = kwargs[:url] || ''
9
- @email = kwargs[:email] || ''
10
- end
11
- end
12
- end
@@ -1,11 +0,0 @@
1
- module OasRails
2
- class License < OasBase
3
- attr_accessor :name, :url
4
-
5
- def initialize(**kwargs)
6
- super()
7
- @name = kwargs[:name] || 'GPL 3.0'
8
- @url = kwargs[:url] || 'https://www.gnu.org/licenses/gpl-3.0.html#license-text'
9
- end
10
- end
11
- end
@@ -1,102 +0,0 @@
1
- module OasRails
2
- class MediaType < OasBase
3
- attr_accessor :schema, :example, :examples, :encoding
4
-
5
- # Initializes a new MediaType object.
6
- #
7
- # @param schema [Hash] the schema of the media type.
8
- # @param kwargs [Hash] additional keyword arguments.
9
- def initialize(schema:, **kwargs)
10
- super()
11
- @schema = schema
12
- @example = kwargs[:example] || {}
13
- @examples = kwargs[:examples] || {}
14
- end
15
-
16
- class << self
17
- @context = :incoming
18
- # Creates a new MediaType object from a model class.
19
- #
20
- # @param klass [Class] the ActiveRecord model class.
21
- # @param examples [Hash] the examples hash.
22
- # @return [MediaType, nil] the created MediaType object or nil if the class is not an ActiveRecord model.
23
- def from_model_class(klass:, context: :incoming, examples: {})
24
- @context = context
25
- return unless klass.ancestors.include? ActiveRecord::Base
26
-
27
- model_schema = EsquemaBuilder.send("build_#{@context}_schema", klass:)
28
- model_schema["required"] = []
29
- schema = { type: "object", properties: { klass.to_s.downcase => model_schema } }
30
- examples.merge!(search_for_examples_in_tests(klass:))
31
- new(media_type: "", schema:, examples:)
32
- end
33
-
34
- # Searches for examples in test files based on the provided class and test framework.
35
- #
36
- # @param klass [Class] the class to search examples for.
37
- # @param utils [Module] a utility module that provides the `detect_test_framework` method. Defaults to `Utils`.
38
- # @return [Hash] a hash containing examples data or an empty hash if no examples are found.
39
- def search_for_examples_in_tests(klass:, context: :incoming, utils: Utils)
40
- @context = context
41
- case utils.detect_test_framework
42
- when :factory_bot
43
- fetch_factory_bot_examples(klass:)
44
- when :fixtures
45
- fetch_fixture_examples(klass:)
46
- else
47
- {}
48
- end
49
- end
50
-
51
- # Transforms tags into examples.
52
- #
53
- # @param tags [Array] the array of tags.
54
- # @return [Hash] the transformed examples hash.
55
- def tags_to_examples(tags:)
56
- tags.each_with_object({}).with_index(1) do |(example, result), _index|
57
- key = example.text.downcase.gsub(' ', '_')
58
- value = {
59
- "summary" => example.text,
60
- "value" => example.content
61
- }
62
- result[key] = value
63
- end
64
- end
65
-
66
- private
67
-
68
- # Fetches examples from FactoryBot for the provided class.
69
- #
70
- # @param klass [Class] the class to fetch examples for.
71
- # @return [Hash] a hash containing examples data or an empty hash if no examples are found.
72
- def fetch_factory_bot_examples(klass:)
73
- klass_sym = klass.to_s.downcase.to_sym
74
- begin
75
- FactoryBot.build_stubbed_list(klass_sym, 3).each_with_index.to_h do |obj, index|
76
- ["#{klass_sym}#{index + 1}", { value: { klass_sym => clean_example_object(obj: obj.as_json) } }]
77
- end
78
- rescue KeyError
79
- {}
80
- end
81
- end
82
-
83
- # Fetches examples from fixtures for the provided class.
84
- #
85
- # @param klass [Class] the class to fetch examples for.
86
- # @return [Hash] a hash containing examples data or an empty hash if no examples are found.
87
- def fetch_fixture_examples(klass:)
88
- fixture_file = Rails.root.join('test', 'fixtures', "#{klass.to_s.pluralize.downcase}.yml")
89
- begin
90
- fixture_data = YAML.load_file(fixture_file).with_indifferent_access
91
- rescue Errno::ENOENT
92
- return {}
93
- end
94
- fixture_data.transform_values { |attributes| { value: { klass.to_s.downcase => clean_example_object(obj: attributes) } } }
95
- end
96
-
97
- def clean_example_object(obj:)
98
- obj.reject { |key, _| OasRails.config.send("excluded_columns_#{@context}").include?(key.to_sym) }
99
- end
100
- end
101
- end
102
- end
@@ -1,30 +0,0 @@
1
- module OasRails
2
- class OasBase
3
- def to_spec
4
- hash = {}
5
- instance_variables.each do |var|
6
- key = var.to_s.delete('@')
7
- camel_case_key = key.camelize(:lower).to_sym
8
- value = instance_variable_get(var)
9
-
10
- processed_value = if value.respond_to?(:to_spec)
11
- value.to_spec
12
- else
13
- value
14
- end
15
-
16
- # hash[camel_case_key] = processed_value unless (processed_value.is_a?(Hash) || processed_value.is_a?(Array)) && processed_value.empty?
17
- hash[camel_case_key] = processed_value
18
- end
19
- hash
20
- end
21
-
22
- private
23
-
24
- def snake_to_camel(snake_str)
25
- words = snake_str.to_s.split('_')
26
- words[1..].map!(&:capitalize)
27
- (words[0] + words[1..].join).to_sym
28
- end
29
- end
30
- end
@@ -1,134 +0,0 @@
1
- module OasRails
2
- class Operation < OasBase
3
- attr_accessor :tags, :summary, :description, :operation_id, :parameters, :method, :docstring, :request_body, :responses, :security
4
-
5
- def initialize(method:, summary:, operation_id:, **kwargs)
6
- super()
7
- @method = method
8
- @summary = summary
9
- @operation_id = operation_id
10
- @tags = kwargs[:tags] || []
11
- @description = kwargs[:description] || @summary
12
- @parameters = kwargs[:parameters] || []
13
- @request_body = kwargs[:request_body] || {}
14
- @responses = kwargs[:responses] || {}
15
- @security = kwargs[:security] || []
16
- end
17
-
18
- class << self
19
- def from_oas_route(oas_route:)
20
- summary = extract_summary(oas_route:)
21
- operation_id = extract_operation_id(oas_route:)
22
- tags = extract_tags(oas_route:)
23
- description = oas_route.docstring
24
- parameters = extract_parameters(oas_route:)
25
- request_body = extract_request_body(oas_route:)
26
- responses = extract_responses(oas_route:)
27
- security = extract_security(oas_route:)
28
- new(method: oas_route.verb.downcase, summary:, operation_id:, tags:, description:, parameters:, request_body:, responses:, security:)
29
- end
30
-
31
- def extract_summary(oas_route:)
32
- oas_route.docstring.tags(:summary).first.try(:text) || generate_crud_name(oas_route.method, oas_route.controller.downcase) || oas_route.verb + " " + oas_route.path
33
- end
34
-
35
- def generate_crud_name(method, controller)
36
- controller_name = controller.to_s.underscore.humanize.downcase.pluralize
37
-
38
- case method.to_sym
39
- when :index
40
- "List #{controller_name}"
41
- when :show
42
- "View #{controller_name.singularize}"
43
- when :create
44
- "Create new #{controller_name.singularize}"
45
- when :update
46
- "Update #{controller_name.singularize}"
47
- when :destroy
48
- "Delete #{controller_name.singularize}"
49
- end
50
- end
51
-
52
- def extract_operation_id(oas_route:)
53
- "#{oas_route.method}#{oas_route.path.gsub('/', '_').gsub(/[{}]/, '')}"
54
- end
55
-
56
- # This method should check tags defined by yard, then extract tag from path namespace or controller name depending on configuration
57
- def extract_tags(oas_route:)
58
- tags = oas_route.docstring.tags(:tags).first
59
- if !tags.nil?
60
- tags.text.split(",").map(&:strip).map(&:titleize)
61
- else
62
- default_tags(oas_route:)
63
- end
64
- end
65
-
66
- def default_tags(oas_route:)
67
- tags = []
68
- if OasRails.config.default_tags_from == "namespace"
69
- tag = oas_route.path.split('/').reject(&:empty?).first.try(:titleize)
70
- tags << tag unless tag.nil?
71
- else
72
- tags << oas_route.controller.titleize
73
- end
74
- tags
75
- end
76
-
77
- def extract_parameters(oas_route:)
78
- parameters = []
79
- parameters.concat(parameters_from_tags(tags: oas_route.docstring.tags(:parameter)))
80
- oas_route.path_params.try(:map) do |p|
81
- parameters << Parameter.from_path(path: oas_route.path, param: p) unless parameters.any? { |param| param.name.to_s == p.to_s }
82
- end
83
- parameters
84
- end
85
-
86
- def parameters_from_tags(tags:)
87
- tags.map do |t|
88
- Parameter.new(name: t.name, location: t.location, required: t.required, schema: t.schema, description: t.text)
89
- end
90
- end
91
-
92
- def extract_request_body(oas_route:)
93
- tag_request_body = oas_route.docstring.tags(:request_body).first
94
- if tag_request_body.nil? && OasRails.config.autodiscover_request_body
95
- oas_route.detect_request_body if %w[create update].include? oas_route.method
96
- elsif !tag_request_body.nil?
97
- RequestBody.from_tags(tag: tag_request_body, examples_tags: oas_route.docstring.tags(:request_body_example))
98
- else
99
- {}
100
- end
101
- end
102
-
103
- def extract_responses(oas_route:)
104
- responses = Responses.from_tags(tags: oas_route.docstring.tags(:response))
105
-
106
- if OasRails.config.autodiscover_responses
107
- new_responses = Extractors::RenderResponseExtractor.extract_responses_from_source(source: oas_route.source_string)
108
-
109
- new_responses.each do |new_response|
110
- responses.responses << new_response unless responses.responses.any? { |r| r.code == new_response.code }
111
- end
112
- end
113
-
114
- responses
115
- end
116
-
117
- def extract_security(oas_route:)
118
- return [] if oas_route.docstring.tags(:no_auth).any?
119
-
120
- if (methods = oas_route.docstring.tags(:auth).first)
121
- OasRails.config.security_schemas.keys.map { |key| { key => [] } }.select do |schema|
122
- methods.types.include?(schema.keys.first.to_s)
123
- end
124
- elsif OasRails.config.authenticate_all_routes_by_default
125
- OasRails.config.security_schemas.keys.map { |key| { key => [] } }
126
- else
127
- []
128
- end
129
- end
130
-
131
- def external_docs; end
132
- end
133
- end
134
- end
@@ -1,47 +0,0 @@
1
- module OasRails
2
- class Parameter
3
- STYLE_DEFAULTS = { query: 'form', path: 'simple', header: 'simple', cookie: 'form' }.freeze
4
-
5
- attr_accessor :name, :in, :style, :description, :required, :schema
6
-
7
- def initialize(name:, location:, description:, **kwargs)
8
- @name = name
9
- @in = location
10
- @description = description
11
-
12
- @required = kwargs[:required] || required?
13
- @style = kwargs[:style] || default_from_in
14
- @schema = kwargs[:schema] || { "type": 'string' }
15
- end
16
-
17
- def self.from_path(path:, param:)
18
- new(name: param, location: 'path',
19
- description: "#{param.split('_')[-1].titleize} of existing #{extract_word_before(path, param).singularize}.")
20
- end
21
-
22
- def self.extract_word_before(string, param)
23
- regex = %r{/(\w+)/\{#{param}\}}
24
- match = string.match(regex)
25
- match ? match[1] : nil
26
- end
27
-
28
- def default_from_in
29
- STYLE_DEFAULTS[@in.to_sym]
30
- end
31
-
32
- def required?
33
- @in == 'path'
34
- end
35
-
36
- def to_spec
37
- {
38
- "name": @name,
39
- "in": @in,
40
- "description": @description,
41
- "required": @required,
42
- "schema": @schema,
43
- "style": @style
44
- }
45
- end
46
- end
47
- end
@@ -1,25 +0,0 @@
1
- module OasRails
2
- class PathItem
3
- attr_reader :path, :operations, :parameters
4
-
5
- def initialize(path:, operations:, parameters:)
6
- @path = path
7
- @operations = operations
8
- @parameters = parameters
9
- end
10
-
11
- def self.from_oas_routes(path:, oas_routes:)
12
- new(path: path, operations: oas_routes.map do |oas_route|
13
- Operation.from_oas_route(oas_route: oas_route)
14
- end, parameters: [])
15
- end
16
-
17
- def to_spec
18
- spec = {}
19
- @operations.each do |o|
20
- spec[o.method] = o.to_spec
21
- end
22
- spec
23
- end
24
- end
25
- end
@@ -1,19 +0,0 @@
1
- module OasRails
2
- class Paths
3
- attr_accessor :path_items
4
-
5
- def initialize(path_items:)
6
- @path_items = path_items
7
- end
8
-
9
- def self.from_string_paths(string_paths:)
10
- new(path_items: string_paths.map do |s|
11
- PathItem.from_oas_routes(path: s, oas_routes: Extractors::RouteExtractor.host_routes_by_path(s))
12
- end)
13
- end
14
-
15
- def to_spec
16
- @path_items.each_with_object({}) { |p, object| object[p.path] = p.to_spec }
17
- end
18
- end
19
- end
@@ -1,29 +0,0 @@
1
- module OasRails
2
- class RequestBody < OasBase
3
- attr_accessor :description, :content, :required
4
-
5
- def initialize(description:, content:, required: false)
6
- super()
7
- @description = description
8
- @content = content # Should be an array of media type object
9
- @required = required
10
- end
11
-
12
- class << self
13
- def from_tags(tag:, examples_tags: [])
14
- if tag.klass.ancestors.include? ActiveRecord::Base
15
- from_model_class(klass: tag.klass, description: tag.text, required: tag.required, examples_tags:)
16
- else
17
- # hash content to schema
18
- content = { "application/json": MediaType.new(schema: tag.schema, examples: MediaType.tags_to_examples(tags: examples_tags)) }
19
- new(description: tag.text, content:, required: tag.required)
20
- end
21
- end
22
-
23
- def from_model_class(klass:, **kwargs)
24
- content = { "application/json": MediaType.from_model_class(klass:, examples: MediaType.tags_to_examples(tags: kwargs[:examples_tags] || {})) }
25
- new(description: kwargs[:description] || klass.to_s, content:, required: kwargs[:required])
26
- end
27
- end
28
- end
29
- end
@@ -1,12 +0,0 @@
1
- module OasRails
2
- class Response < OasBase
3
- attr_accessor :code, :description, :content
4
-
5
- def initialize(code:, description:, content:)
6
- super()
7
- @code = code
8
- @description = description
9
- @content = content # Should be an array of media type object
10
- end
11
- end
12
- end
@@ -1,20 +0,0 @@
1
- module OasRails
2
- class Responses < OasBase
3
- attr_accessor :responses
4
-
5
- def initialize(responses)
6
- super()
7
- @responses = responses
8
- end
9
-
10
- def to_spec
11
- @responses.each_with_object({}) { |r, object| object[r.code] = r.to_spec }
12
- end
13
-
14
- class << self
15
- def from_tags(tags:)
16
- new(tags.map { |t| Response.new(code: t.name.to_i, description: t.text, content: { "application/json": MediaType.new(schema: t.schema) }) })
17
- end
18
- end
19
- end
20
- end
@@ -1,10 +0,0 @@
1
- module OasRails
2
- class Server < OasBase
3
- attr_accessor :url, :description
4
-
5
- def initialize(url:, description:)
6
- @url = url
7
- @description = description
8
- end
9
- end
10
- end