Method: Fitting::Doc::ContentType#initialize

Defined in:
lib/fitting/doc/content_type.rb

#initialize(content_type, subvalue) ⇒ ContentType

Returns a new instance of ContentType.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fitting/doc/content_type.rb', line 9

def initialize(content_type, subvalue)
  @logs = []
  @step_key = content_type
  @next_steps = []
  @step_cover_size = 0
  return self if content_type != 'application/json'

  definitions = subvalue.inject({}) do |sum, sv|
    if sv['body']["definitions"] != nil
      sum.merge!(sv['body'].delete('definitions'))
    end
    sum
  end

  if subvalue.size == 1
    if definitions && definitions != {}
      res = merge_definitions(subvalue[0], definitions)
      if res && res['properties']['type'] == 'object'
        @next_steps.push(JsonSchema.new(
          {
            "$schema" => "http://json-schema.org/draft-04/schema#",
          }.merge(res['properties']), false
        ))
      elsif res
        @next_steps.push(JsonSchema.new(
          {
            "$schema" => "http://json-schema.org/draft-04/schema#",
            "type" => "object"
          }.merge(res), false
        ))
      else
        @next_steps.push(JsonSchema.new({}, false))
      end
    else
      @next_steps.push(JsonSchema.new(subvalue[0]['body'], false))
    end
  else
    if definitions && definitions != {}
      @next_steps.push(JsonSchema.new(
        {
          "$schema" => "http://json-schema.org/draft-04/schema#",
          "type" => "object",
          "oneOf" => subvalue.inject([]) do |sum, sv|
            res = merge_definitions(sv, definitions)
            sum.push(res)
          end
        }, false
      ))
    else
      super_schema = false
      subvalue.each do |sv|
        super_schema = true if sv['body']["properties"] == nil
      end

      if super_schema
        @next_steps.push(JsonSchema.new(
          {
            "$schema" => "http://json-schema.org/draft-04/schema#",
            "type" => "object",
            "oneOf" => subvalue.inject([]) do |sum, sv|
              if sv['body']["properties"]
                sum.push(check_body(sv['body']["properties"], sv))
              end
              sum
            end
          }, super_schema
        ))
      else
        @next_steps.push(JsonSchema.new(
          {
            "$schema" => "http://json-schema.org/draft-04/schema#",
            "type" => "object",
            "oneOf" => subvalue.inject([]) do |sum, sv|
              sum.push(check_body(sv['body']["properties"], sv))
            end
          }, super_schema
        ))
        super_schema
      end
    end
  end
end