Class: OasCore::Builders::ContentBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(specification) ⇒ ContentBuilder

Returns a new instance of ContentBuilder.



6
7
8
9
10
# File 'lib/oas_core/builders/content_builder.rb', line 6

def initialize(specification)
  @specification = specification
  @media_type = Spec::MediaType.new(specification)
  @content_type = 'application/json'
end

Instance Method Details

#buildObject



51
52
53
54
55
# File 'lib/oas_core/builders/content_builder.rb', line 51

def build
  {
    @content_type => @media_type
  }
end

#with_content_type(content_type) ⇒ Object



28
29
30
31
32
# File 'lib/oas_core/builders/content_builder.rb', line 28

def with_content_type(content_type)
  @content_type = content_type if content_type && !content_type.empty?

  self
end

#with_examples(examples) ⇒ Object



22
23
24
25
26
# File 'lib/oas_core/builders/content_builder.rb', line 22

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

  self
end

#with_examples_from_tags(tags) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oas_core/builders/content_builder.rb', line 34

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(' ', '_')
    if example.content.is_a? OasCore::Spec::Reference
      result[key] = example.content
    else
      value = {
        'summary' => example.text,
        'value' => example.content
      }
      result[key] = @specification.components.add_example(value)
    end
  end)

  self
end

#with_schema(schema) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/oas_core/builders/content_builder.rb', line 12

def with_schema(schema)
  @media_type.schema = if schema.is_a? OasCore::Spec::Reference
                         schema
                       else
                         @specification.components.add_schema(schema)
                       end

  self
end