Class: Diesel::Swagger::Parser

Inherits:
Object
  • Object
show all
Includes:
Utils::Inflections
Defined in:
lib/diesel/swagger/parser.rb

Instance Method Summary collapse

Methods included from Utils::Inflections

acronym_regex, acronyms, #camelize, #constantize, #underscore

Instance Method Details

#build_specification(json) ⇒ Object



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
# File 'lib/diesel/swagger/parser.rb', line 15

def build_specification(json)
  specification = build_node(Specification, json)
  specification.info = build_node(Info, json['info'])
  specification.external_docs = build_node(ExternalDocs, json['externalDocs'])
  specification.schemes = json['schemes']
  specification.produces = json['produces'] || []
  specification.consumes = json['consumes'] || []
  build_security_definition_hash(specification, json)
  build_security_hash(specification, json)
  specification.paths = build_node_hash(Path, json, 'paths') do |path, path_json|
    [:get, :put, :post, :delete, :options, :head, :patch].each do |method|
      if op_json = path_json[method.to_s]
        op = build_node(Operation, op_json, constructor_args: [method])
        op.external_docs = build_node(ExternalDocs, op_json['externalDocs'])
        op.parameters = build_node_list(Parameter, op_json, 'parameters') do |param, param_json|
          if param_json["schema"] && param_json["schema"].kind_of?(Hash)
            schema_json = param_json["schema"]
            param.schema = build_node(Definition, schema_json)
          end
        end
        build_security_definition_hash(specification, json)
        build_security_hash(op, op_json)
        path.send("#{method}=".to_sym, op)
      end
    end
  end
  specification.definitions = build_node_hash(Definition, json, 'definitions') do |definition, def_json|
    definition.properties = build_node_hash(Property, def_json, 'properties') do |prop, prop_json|
      prop.enum = prop_json["enum"]
      prop.items = prop_json["items"]
    end
  end
  specification
end

#parse(spec) ⇒ Object



11
12
13
# File 'lib/diesel/swagger/parser.rb', line 11

def parse(spec)
  build_specification(MultiJson.load(spec))
end