Class: RspecApiDocumentation::Writers::AppendJsonIndex

Inherits:
JsonIndex
  • Object
show all
Defined in:
lib/rspec_api_documentation/writers/append_json_writer.rb

Instance Method Summary collapse

Methods inherited from JsonIndex

#examples, #sections

Constructor Details

#initialize(index, configuration, existing_index_hash = nil) ⇒ AppendJsonIndex

Returns a new instance of AppendJsonIndex.



23
24
25
26
27
# File 'lib/rspec_api_documentation/writers/append_json_writer.rb', line 23

def initialize(index, configuration, existing_index_hash = nil)
  @index = index
  @configuration = configuration
  @existing_index_hash = clean_index_hash(existing_index_hash)
end

Instance Method Details

#as_json(opts = nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/rspec_api_documentation/writers/append_json_writer.rb', line 29

def as_json(opts = nil)
  sections.inject(@existing_index_hash) do |h, section|
    h[:resources].push(section_hash(section))
    h
  end
end

#clean_index_hash(existing_index_hash) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rspec_api_documentation/writers/append_json_writer.rb', line 36

def clean_index_hash(existing_index_hash)
  unless existing_index_hash.is_a?(Hash) && existing_index_hash["resources"].is_a?(Array) #check format
    existing_index_hash = {:resources => []}
  end
  existing_index_hash = existing_index_hash.deep_symbolize_keys
  existing_index_hash[:resources].map!(&:deep_symbolize_keys).reject! do |resource|
    resource_names = sections.map{|s| s[:resource_name]}
    resource_names.include? resource[:name]
  end
  existing_index_hash
end