Class: Fitting::Doc::ContentType
- Defined in:
- lib/fitting/doc/content_type.rb
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary
Attributes inherited from Step
#index_after, #index_before, #index_medium, #next_steps, #res_after, #res_before, #res_medium, #step_cover_size, #step_key
Instance Method Summary collapse
- #check_body(res, sv) ⇒ Object
- #cover!(log) ⇒ Object
- #debug(debug) ⇒ Object
-
#initialize(content_type, subvalue) ⇒ ContentType
constructor
A new instance of ContentType.
- #logs ⇒ Object
- #merge_definitions(sv, definitions) ⇒ Object
Methods inherited from Step
#index_offset, #mark_enum, #mark_range, #mark_required, #new_index_offset, #next, #nocover!, #range, #report, #to_hash, #valid?
Constructor Details
#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 |
Instance Method Details
#check_body(res, sv) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/fitting/doc/content_type.rb', line 138 def check_body(res, sv) if sv['body']["required"] == nil { "properties" => res } else { "properties" => res, "required" => sv['body']["required"] } end end |
#cover!(log) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/fitting/doc/content_type.rb', line 92 def cover!(log) if @step_key == log.content_type && log.content_type == 'application/json' @step_cover_size += 1 @logs.push(log.body) @next_steps.each { |json_schema| json_schema.cover!(log) } elsif @step_key != 'application/json' && log.content_type != 'application/json' @step_cover_size += 1 end rescue Fitting::Doc::JsonSchema::NotFound => e raise NotFound.new "content-type: #{@step_key}\n\n"\ "#{e.}" end |
#debug(debug) ⇒ Object
105 106 107 108 |
# File 'lib/fitting/doc/content_type.rb', line 105 def debug(debug) return self if @step_key == debug.content_type && debug.content_type == 'application/json' nil end |
#logs ⇒ Object
110 111 112 |
# File 'lib/fitting/doc/content_type.rb', line 110 def logs @logs end |
#merge_definitions(sv, definitions) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/fitting/doc/content_type.rb', line 114 def merge_definitions(sv, definitions) if sv['body']["properties"] res = sv['body']["properties"] elsif sv['body']['type'] != 'array' res = sv['body'] end definitions.each_pair do |key, value| while JSON.dump(res).include?("\"$ref\":\"#/definitions/#{key}\"") do new_res_array = JSON.dump(res).split('{') index = new_res_array.index { |js| js.include?("\"$ref\":\"#/definitions/#{key}\"") } if index != nil def_size = "\"$ref\":\"#/definitions/#{key}\"".size new_res_array[index] = JSON.dump(value)[1..-2] + new_res_array[index][def_size..-1] res = JSON.load(new_res_array.join("{")) end end end if res == nil nil else check_body(res, sv) end end |