class OasRails::YARD::OasRailsFactory
def active_record_class?(text)
-
(Boolean)
- True if the text refers to an ActiveRecord class, false otherwise.
Parameters:
-
text
(String
) -- The text to check.
def active_record_class?(text) klass = text.constantize klass.ancestors.include? ActiveRecord::Base rescue StandardError false end
def description_and_type(text)
-
(MatchData)
- The match data from the regex.
Parameters:
-
text
(String
) -- The text to parse.
def description_and_type(text) match = text.match(/^(.*?)\s*\[(.*?)\]\s*(.*)$/) raise ArgumentError, "The request body tag is not valid: #{text}" if match.nil? match end
def eval_content(content)
-
(Hash)
- The evaluated hash, or an empty hash if an error occurs.
Parameters:
-
content
(String
) -- The content string to evaluate.
def eval_content(content) eval(content) rescue StandardError {} end
def extract_description_and_schema(text)
-
(Array)
- An array containing the description, class, schema, and required flag.
Parameters:
-
text
(String
) -- The text to parse.
def extract_description_and_schema(text) description, type, = extract_description_type_and_content(text) klass, schema, required = type_text_to_schema(type) [description, klass, schema, required] end
def extract_description_type_and_content(text, process_content: false, expresion: /^(.*?)\s*\[(.*)\]\s*(.*)$/)
-
(Array)
- An array containing the description, type, and content or remaining text.
Parameters:
-
process_content
(Boolean
) -- Whether to evaluate the content as a hash. -
text
(String
) -- The text to parse.
def extract_description_type_and_content(text, process_content: false, expresion: /^(.*?)\s*\[(.*)\]\s*(.*)$/) match = text.match(expresion) raise ArgumentError, "Invalid tag format: #{text}" if match.nil? description = match[1].strip type = match[2].strip content = process_content ? eval_content(match[3].strip) : match[3].strip [description, type, content] end
def extract_name_code_and_hash(text)
-
(Array)
- An array containing the name, code, and schema.
Parameters:
-
text
(String
) -- The text to parse.
def extract_name_code_and_hash(text) name, code = extract_text_and_parentheses_content(text) _, type, = extract_description_type_and_content(text) hash = eval_content(type) [name, code, hash] end
def extract_name_code_and_schema(text)
-
(Array)
- An array containing the name, code, and schema.
Parameters:
-
text
(String
) -- The text to parse.
def extract_name_code_and_schema(text) name, code = extract_text_and_parentheses_content(text) _, type, = extract_description_type_and_content(text) schema = type_text_to_schema(type)[1] [name, code, schema] end
def extract_name_location_schema_and_description(text)
-
(Array)
- An array containing the name, location, schema, and required flag.
Parameters:
-
text
(String
) -- The text to parse.
def extract_name_location_schema_and_description(text) match = text.match(/^(.*?)\s*\[(.*?)\]\s*(.*)$/) name, location = extract_text_and_parentheses_content(match[1].strip) schema, required = type_text_to_schema(match[2].strip)[1..] description = match[3].strip [name, location, schema, required, description] end
def extract_text_and_parentheses_content(input)
-
(Array)
- An array containing the name and location.
Parameters:
-
input
(String
) -- The input text to parse.
def extract_text_and_parentheses_content(input) return unless input =~ /^(.+?)\(([^)]+)\)/ text = ::Regexp.last_match(1).strip parenthesis_content = ::Regexp.last_match(2).strip [text, parenthesis_content] end
def parse_tag_with_parameter(tag_name, text)
-
(ParameterTag)
- The parsed parameter tag object.
Parameters:
-
text
(String
) -- The tag text to parse. -
tag_name
(String
) -- The name of the tag.
def parse_tag_with_parameter(tag_name, text) name, location, schema, required, description = extract_name_location_schema_and_description(text) ParameterTag.new(tag_name, name, description, schema, location, required:) end
def parse_tag_with_request_body(tag_name, text)
-
(RequestBodyTag)
- The parsed request body tag object.
Parameters:
-
text
(String
) -- The tag text to parse. -
tag_name
(String
) -- The name of the tag.
def parse_tag_with_request_body(tag_name, text) description, klass, schema, required = extract_description_and_schema(text) RequestBodyTag.new(tag_name, description, klass, schema:, required:) end
def parse_tag_with_request_body_example(tag_name, text)
-
(RequestBodyExampleTag)
- The parsed request body example tag object.
Parameters:
-
text
(String
) -- The tag text to parse. -
tag_name
(String
) -- The name of the tag.
def parse_tag_with_request_body_example(tag_name, text) description, _, hash = extract_description_type_and_content(text, process_content: true, expresion: /^(.*?)\[([^\]]*)\](.*)$/m) RequestBodyExampleTag.new(tag_name, description, content: hash) end
def parse_tag_with_response(tag_name, text)
-
(ResponseTag)
- The parsed response tag object.
Parameters:
-
text
(String
) -- The tag text to parse. -
tag_name
(String
) -- The name of the tag.
def parse_tag_with_response(tag_name, text) name, code, schema = extract_name_code_and_schema(text) ResponseTag.new(tag_name, code, name, schema) end
def parse_tag_with_response_example(tag_name, text)
-
(ResponseExampleTag)
- The parsed response example tag object.
Parameters:
-
text
(String
) -- The tag text to parse. -
tag_name
(String
) -- The name of the tag.
def parse_tag_with_response_example(tag_name, text) description, code, hash = extract_name_code_and_hash(text) ResponseExampleTag.new(tag_name, description, content: hash, code:) end
def text_and_required(text)
-
(Array)
- An array containing the text and a required flag.
Parameters:
-
text
(String
) -- The text to parse.
def text_and_required(text) if text.start_with?('!') [text.sub(/^!/, ''), true] else [text, false] end end
def type_text_to_schema(text)
-
(Array)
- An array containing the class, schema, and required flag.
Parameters:
-
text
(String
) -- The type text to convert.
def type_text_to_schema(text) type_text, required = text_and_required(text) if active_record_class?(type_text) klass = type_text.constantize schema = Builders::EsquemaBuilder.build_outgoing_schema(klass:) else schema = JsonSchemaGenerator.process_string(type_text)[:json_schema] klass = Object end [klass, schema, required] end