-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathappend_json_writer.rb
49 lines (44 loc) · 1.57 KB
/
append_json_writer.rb
1
2
3
4
5
6
7
8
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
require 'rspec_api_documentation/writers/formatter'
module RspecApiDocumentation
module Writers
class AppendJsonWriter < JsonWriter
def write
index_file = docs_dir.join("index.json")
if File.exists?(index_file) && (output = File.read(index_file)).length >= 2
existing_index_hash = JSON.parse(output)
end
File.open(index_file, "w+") do |f|
f.write Formatter.to_json(AppendJsonIndex.new(index, configuration, existing_index_hash))
end
write_examples
end
def self.clear_docs(docs_dir)
nil #noop
end
end
class AppendJsonIndex < JSONIndex
def initialize(index, configuration, existing_index_hash = nil)
@index = index
@configuration = configuration
@existing_index_hash = clean_index_hash(existing_index_hash)
end
def as_json(opts = nil)
sections.inject(@existing_index_hash) do |h, section|
h[:resources].push(section_hash(section))
h
end
end
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
end
end
end