Class: OasCore::YARD::OasCoreFactory

Inherits:
YARD::Tags::DefaultFactory
  • Object
show all
Defined in:
lib/oas_core/yard/oas_core_factory.rb

Instance Method Summary collapse

Instance Method Details

#parse_boolean(value) ⇒ Boolean, Object

Parses a boolean value.

Parameters:

  • value (String)

    The value to parse.

Returns:

  • (Boolean, Object)

    The parsed boolean or the original value if it can't be coerced.



111
112
113
114
115
116
117
118
119
120
# File 'lib/oas_core/yard/oas_core_factory.rb', line 111

def parse_boolean(value)
  value_str = value.to_s.downcase
  if value_str == 'true'
    true
  elsif value_str == 'false'
    false
  else
    value
  end
end

#parse_default_tag(schema) ⇒ Value

Parses the default tag.

Parameters:

  • schema (Hash)

    The schema containing the default value.

Returns:

  • (Value)

    The parsed default value.



66
67
68
69
70
71
72
73
74
75
# File 'lib/oas_core/yard/oas_core_factory.rb', line 66

def parse_default_tag(schema)
  case schema[:type]
  when 'integer'
    parse_integer(schema[:default])
  when 'boolean'
    parse_boolean(schema[:default])
  else
    schema[:default]
  end
end

#parse_enum_tag(schema) ⇒ Array

Parses the enum tag.

Parameters:

  • schema (Hash)

    The schema containing the enum values.

Returns:

  • (Array)

    The parsed enum values.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/oas_core/yard/oas_core_factory.rb', line 80

def parse_enum_tag(schema)
  enum_values = schema[:enum]
  return enum_values unless enum_values.is_a?(Array)

  case schema[:type]
  when 'integer'
    enum_values.map do |value|
      parse_integer(value)
    end
  when 'boolean'
    enum_values.map do |value|
      parse_boolean(value)
    end
  else
    enum_values
  end
end

#parse_integer(value) ⇒ Integer, Object

Parses an integer value.

Parameters:

  • value (String)

    The value to parse.

Returns:

  • (Integer, Object)

    The parsed integer or the original value if it can't be coerced.



101
102
103
104
105
106
# File 'lib/oas_core/yard/oas_core_factory.rb', line 101

def parse_integer(value)
  Integer(value)
rescue ArgumentError, TypeError
  # Keep original value if it can't be coerced
  value
end

#parse_tag_with_parameter(tag_name, text) ⇒ ParameterTag

Parses a tag that represents a parameter.

Parameters:

  • tag_name (String)

    The name of the tag.

  • text (String)

    The tag text to parse.

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/oas_core/yard/oas_core_factory.rb', line 41

def parse_tag_with_parameter(tag_name, text)
  match = text.squish.match(/^(.*?)\s*\[(.*?)\]\s*(.*)$/)
  if match
    first = match[1]
    second = match[2]
    description = match[3]
  end
  name, location = text_and_last_parenthesis_content(first)
  raw_type, required = text_and_required(second)
  schema = raw_type_to_content(raw_type)
  name = "#{name}[]" if location == 'query' && schema[:type] == 'array'
  description, schema_keywords = extract_schema_keywords(description)
  schema.merge!(schema_keywords)

  schema[:default] = parse_default_tag(schema) if schema.key?(:default)
  schema[:enum] = parse_enum_tag(schema) if schema.key?(:enum)

  ParameterTag.new(tag_name, name, description.strip, schema, location, required:)
rescue StandardError => e
  raise TagParsingError, "Failed to parse parameter tag: #{e.message}"
end

#parse_tag_with_parameter_reference(tag_name, text) ⇒ ParameterReferenceTag

Parses a tag that represents a parameter reference.

Parameters:

  • tag_name (String)

    The name of the tag.

  • text (String)

    The tag text to parse.

Returns:



154
155
156
157
158
159
160
# File 'lib/oas_core/yard/oas_core_factory.rb', line 154

def parse_tag_with_parameter_reference(tag_name, text)
  ref = text.strip
  reference = OasCore::Spec::Reference.new(ref)
  ParameterReferenceTag.new(tag_name, reference)
rescue StandardError => e
  raise TagParsingError, "Failed to parse parameter reference tag: #{e.message}"
end

#parse_tag_with_request_body(tag_name, text) ⇒ RequestBodyTag

Parses a tag that represents a request body.

Parameters:

  • tag_name (String)

    The name of the tag.

  • text (String)

    The tag text to parse.

Returns:



12
13
14
15
16
17
18
19
20
21
# File 'lib/oas_core/yard/oas_core_factory.rb', line 12

def parse_tag_with_request_body(tag_name, text)
  description, raw_type = split_description_and_type(text.squish)
  description, content_type = text_and_last_parenthesis_content(description)
  raw_type, required = text_and_required(raw_type)
  content = raw_type_to_content(raw_type)

  RequestBodyTag.new(tag_name, description, content:, required:, content_type:)
rescue StandardError => e
  raise TagParsingError, "Failed to parse request body tag: #{e.message}"
end

#parse_tag_with_request_body_example(tag_name, text) ⇒ RequestBodyExampleTag

Parses a tag that represents a request body example.

Parameters:

  • tag_name (String)

    The name of the tag.

  • text (String)

    The tag text to parse.

Returns:



27
28
29
30
31
32
33
34
35
# File 'lib/oas_core/yard/oas_core_factory.rb', line 27

def parse_tag_with_request_body_example(tag_name, text)
  description, raw_type = split_description_and_type(text.squish)
  raw_type, = text_and_required(raw_type)
  content = raw_type_to_content(raw_type)

  RequestBodyExampleTag.new(tag_name, description, content: content)
rescue StandardError => e
  raise TagParsingError, "Failed to parse request body example tag: #{e.message}"
end

#parse_tag_with_request_body_reference(tag_name, text) ⇒ RequestBodyReferenceTag

Parses a tag that represents a request body reference.

Parameters:

  • tag_name (String)

    The name of the tag.

  • text (String)

    The tag text to parse.

Returns:



166
167
168
169
170
171
172
# File 'lib/oas_core/yard/oas_core_factory.rb', line 166

def parse_tag_with_request_body_reference(tag_name, text)
  ref = text.strip
  reference = OasCore::Spec::Reference.new(ref)
  RequestBodyReferenceTag.new(tag_name, reference)
rescue StandardError => e
  raise TagParsingError, "Failed to parse request body reference tag: #{e.message}"
end

#parse_tag_with_response(tag_name, text) ⇒ ResponseTag

Parses a tag that represents a response.

Parameters:

  • tag_name (String)

    The name of the tag.

  • text (String)

    The tag text to parse.

Returns:



126
127
128
129
130
131
132
133
134
# File 'lib/oas_core/yard/oas_core_factory.rb', line 126

def parse_tag_with_response(tag_name, text)
  description, raw_type = split_description_and_type(text.squish)
  description, code = text_and_last_parenthesis_content(description)
  content = raw_type_to_content(raw_type)

  ResponseTag.new(tag_name, description, code, content)
rescue StandardError => e
  raise TagParsingError, "Failed to parse response tag: #{e.message}"
end

#parse_tag_with_response_example(tag_name, text) ⇒ ResponseExampleTag

Parses a tag that represents a response example.

Parameters:

  • tag_name (String)

    The name of the tag.

  • text (String)

    The tag text to parse.

Returns:



140
141
142
143
144
145
146
147
148
# File 'lib/oas_core/yard/oas_core_factory.rb', line 140

def parse_tag_with_response_example(tag_name, text)
  description, raw_type = split_description_and_type(text.squish)
  description, code = text_and_last_parenthesis_content(description)
  content = raw_type_to_content(raw_type)

  ResponseExampleTag.new(tag_name, description, content: content, code:)
rescue StandardError => e
  raise TagParsingError, "Failed to parse response example tag: #{e.message}"
end

#parse_tag_with_response_reference(tag_name, text) ⇒ ResponseReferenceTag

Parses a tag that represents a response reference.

Parameters:

  • tag_name (String)

    The name of the tag.

  • text (String)

    The tag text to parse.

Returns:



178
179
180
181
182
183
184
# File 'lib/oas_core/yard/oas_core_factory.rb', line 178

def parse_tag_with_response_reference(tag_name, text)
  reference_str, code_text = text_and_first_parenthesis_content(text.strip)
  reference = OasCore::Spec::Reference.new(reference_str)
  ResponseReferenceTag.new(tag_name, reference, code: code_text.to_i)
rescue StandardError => e
  raise TagParsingError, "Failed to parse response reference tag: #{e.message}"
end