Class: Rspec::Autoswagger::DocParts

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/autoswagger/doc_parts.rb

Constant Summary collapse

DEFAULT_OUTPUT_PATH =
'./tmp'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocParts

Returns a new instance of DocParts.



11
12
13
14
15
16
# File 'lib/rspec/autoswagger/doc_parts.rb', line 11

def initialize
  @info = Parts::Info.generate_hash
  @paths = {}
  @definitions = {}
  @specification = {}
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



6
7
8
# File 'lib/rspec/autoswagger/doc_parts.rb', line 6

def definitions
  @definitions
end

#infoObject (readonly)

Returns the value of attribute info.



6
7
8
# File 'lib/rspec/autoswagger/doc_parts.rb', line 6

def info
  @info
end

#output_pathObject

Returns the value of attribute output_path.



7
8
9
# File 'lib/rspec/autoswagger/doc_parts.rb', line 7

def output_path
  @output_path
end

#pathsObject (readonly)

Returns the value of attribute paths.



6
7
8
# File 'lib/rspec/autoswagger/doc_parts.rb', line 6

def paths
  @paths
end

#specificationObject (readonly)

Returns the value of attribute specification.



6
7
8
# File 'lib/rspec/autoswagger/doc_parts.rb', line 6

def specification
  @specification
end

Instance Method Details

#add(rspec_core_obj, example) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rspec/autoswagger/doc_parts.rb', line 22

def add(rspec_core_obj, example)
  doc_part = DocPart.new(rspec_core_obj, example)
  path, param_definitions = doc_part.create_path
  method = path.values.first.keys.first
  endpoint = path.keys.first
  if paths.keys.include?(endpoint)
    if paths[endpoint].keys.include?(method)
      paths.each do |key, value|
        value[method]['responses'].merge!(path.values.first[method]['responses']) if key.to_s == endpoint.to_s
      end
    else
      paths[endpoint].merge!(path.values.first)
    end
  else
    paths.merge!(path)
  end
  definitions.merge!(doc_part.create_definition(output_path))
  definitions.merge!(param_definitions) unless param_definitions.empty?
end

#aggregateObject



42
43
44
45
46
47
48
# File 'lib/rspec/autoswagger/doc_parts.rb', line 42

def aggregate
  specification.merge!(info)
  specification.merge!({ 'paths' => paths })
  specification.merge!({ 'definitions' => definitions })

  specification
end

#to_yamlObject



50
51
52
53
54
# File 'lib/rspec/autoswagger/doc_parts.rb', line 50

def to_yaml
  aggregate if specification.empty?
  FileUtils::mkdir_p(output_path)
  YAML.dump(specification, File.open(output_path + '/swagger.yml', 'w'))
end