Class: OasRails::Builders::ContentBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/oas_rails/builders/content_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(specification, context) ⇒ ContentBuilder

Returns a new instance of ContentBuilder.



4
5
6
7
8
# File 'lib/oas_rails/builders/content_builder.rb', line 4

def initialize(specification, context)
  @context = context || :incoming
  @specification = specification
  @media_type = Spec::MediaType.new(specification)
end

Instance Method Details

#buildObject



48
49
50
51
52
# File 'lib/oas_rails/builders/content_builder.rb', line 48

def build
  {
    "application/json": @media_type
  }
end

#from_model_class(klass) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/oas_rails/builders/content_builder.rb', line 35

def from_model_class(klass)
  return self unless klass.ancestors.include? ActiveRecord::Base

  model_schema = Builders::EsquemaBuilder.send("build_#{@context}_schema", klass:)
  model_schema["required"] = []
  schema = { type: "object", properties: { klass.to_s.downcase => model_schema } }
  examples = Spec::MediaType.search_for_examples_in_tests(klass, context: @context)
  @media_type.schema = @specification.components.add_schema(schema)
  @media_type.examples = @media_type.examples.merge(examples)

  self
end

#with_examples(examples) ⇒ Object



16
17
18
19
20
# File 'lib/oas_rails/builders/content_builder.rb', line 16

def with_examples(examples)
  @media_type.examples = @specification.components.add_example(examples)

  self
end

#with_examples_from_tags(tags) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/oas_rails/builders/content_builder.rb', line 22

def with_examples_from_tags(tags)
  @media_type.examples = @media_type.examples.merge(tags.each_with_object({}).with_index(1) do |(example, result), _index|
    key = example.text.downcase.gsub(' ', '_')
    value = {
      "summary" => example.text,
      "value" => example.content
    }
    result[key] = @specification.components.add_example(value)
  end)

  self
end

#with_schema(schema) ⇒ Object



10
11
12
13
14
# File 'lib/oas_rails/builders/content_builder.rb', line 10

def with_schema(schema)
  @media_type.schema = @specification.components.add_schema(schema)

  self
end