diff --git a/lib/rspec_api_documentation.rb b/lib/rspec_api_documentation.rb index a8bfcca4..f3bba058 100644 --- a/lib/rspec_api_documentation.rb +++ b/lib/rspec_api_documentation.rb @@ -28,12 +28,14 @@ module RspecApiDocumentation module Writers extend ActiveSupport::Autoload + autoload :Writer autoload :GeneralMarkupWriter autoload :HtmlWriter autoload :TextileWriter autoload :JsonWriter + autoload :AppendJsonWriter autoload :JsonIodocsWriter - autoload :IndexWriter + autoload :IndexHelper autoload :CombinedTextWriter autoload :CombinedJsonWriter end diff --git a/lib/rspec_api_documentation/api_documentation.rb b/lib/rspec_api_documentation/api_documentation.rb index dec55ca3..2503f594 100644 --- a/lib/rspec_api_documentation/api_documentation.rb +++ b/lib/rspec_api_documentation/api_documentation.rb @@ -10,10 +10,9 @@ def initialize(configuration) end def clear_docs - if File.exists?(docs_dir) - FileUtils.rm_rf(docs_dir, :secure => true) + writers.each do |writer| + writer.clear_docs(docs_dir) end - FileUtils.mkdir_p(docs_dir) end def document_example(rspec_example) diff --git a/lib/rspec_api_documentation/views/markup_index.rb b/lib/rspec_api_documentation/views/markup_index.rb index f0bd6c3d..597bd240 100644 --- a/lib/rspec_api_documentation/views/markup_index.rb +++ b/lib/rspec_api_documentation/views/markup_index.rb @@ -14,7 +14,7 @@ def api_name end def sections - RspecApiDocumentation::Writers::IndexWriter.sections(examples, @configuration) + RspecApiDocumentation::Writers::IndexHelper.sections(examples, @configuration) end end end diff --git a/lib/rspec_api_documentation/writers/append_json_writer.rb b/lib/rspec_api_documentation/writers/append_json_writer.rb new file mode 100644 index 00000000..5eae1f7b --- /dev/null +++ b/lib/rspec_api_documentation/writers/append_json_writer.rb @@ -0,0 +1,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 diff --git a/lib/rspec_api_documentation/writers/combined_json_writer.rb b/lib/rspec_api_documentation/writers/combined_json_writer.rb index a77f0c1a..1c1959d1 100644 --- a/lib/rspec_api_documentation/writers/combined_json_writer.rb +++ b/lib/rspec_api_documentation/writers/combined_json_writer.rb @@ -2,7 +2,7 @@ module RspecApiDocumentation module Writers - class CombinedJsonWriter + class CombinedJsonWriter < Writer def self.write(index, configuration) File.open(configuration.docs_dir.join("combined.json"), "w+") do |f| examples = [] diff --git a/lib/rspec_api_documentation/writers/combined_text_writer.rb b/lib/rspec_api_documentation/writers/combined_text_writer.rb index 3078641c..5126e764 100644 --- a/lib/rspec_api_documentation/writers/combined_text_writer.rb +++ b/lib/rspec_api_documentation/writers/combined_text_writer.rb @@ -1,6 +1,6 @@ module RspecApiDocumentation module Writers - class CombinedTextWriter + class CombinedTextWriter < Writer def self.write(index, configuration) index.examples.each do |rspec_example| example = CombinedTextExample.new(rspec_example) diff --git a/lib/rspec_api_documentation/writers/general_markup_writer.rb b/lib/rspec_api_documentation/writers/general_markup_writer.rb index 93c94d05..9b5514cb 100644 --- a/lib/rspec_api_documentation/writers/general_markup_writer.rb +++ b/lib/rspec_api_documentation/writers/general_markup_writer.rb @@ -1,20 +1,8 @@ module RspecApiDocumentation module Writers - class GeneralMarkupWriter - attr_accessor :index, :configuration - + class GeneralMarkupWriter < Writer INDEX_FILE_NAME = 'index' - - def initialize(index, configuration) - self.index = index - self.configuration = configuration - end - def self.write(index, configuration) - writer = new(index, configuration) - writer.write - end - def write File.open(configuration.docs_dir.join(index_file_name + '.' + extension), "w+") do |f| f.write markup_index_class.new(index, configuration).render diff --git a/lib/rspec_api_documentation/writers/html_writer.rb b/lib/rspec_api_documentation/writers/html_writer.rb index 32348a95..34bb0650 100644 --- a/lib/rspec_api_documentation/writers/html_writer.rb +++ b/lib/rspec_api_documentation/writers/html_writer.rb @@ -1,8 +1,6 @@ module RspecApiDocumentation module Writers class HtmlWriter < GeneralMarkupWriter - attr_accessor :index, :configuration - EXTENSION = 'html' def markup_index_class diff --git a/lib/rspec_api_documentation/writers/index_writer.rb b/lib/rspec_api_documentation/writers/index_helper.rb similarity index 96% rename from lib/rspec_api_documentation/writers/index_writer.rb rename to lib/rspec_api_documentation/writers/index_helper.rb index bab7f03a..b52b26b4 100644 --- a/lib/rspec_api_documentation/writers/index_writer.rb +++ b/lib/rspec_api_documentation/writers/index_helper.rb @@ -2,7 +2,7 @@ module RspecApiDocumentation module Writers - module IndexWriter + module IndexHelper def sections(examples, configuration) resources = examples.group_by(&:resource_name).inject([]) do |arr, (resource_name, examples)| ordered_examples = configuration.keep_source_order ? examples : examples.sort_by(&:description) diff --git a/lib/rspec_api_documentation/writers/json_iodocs_writer.rb b/lib/rspec_api_documentation/writers/json_iodocs_writer.rb index b9cb24bb..25235d85 100644 --- a/lib/rspec_api_documentation/writers/json_iodocs_writer.rb +++ b/lib/rspec_api_documentation/writers/json_iodocs_writer.rb @@ -2,21 +2,15 @@ module RspecApiDocumentation module Writers - class JsonIodocsWriter - attr_accessor :index, :configuration, :api_key + class JsonIodocsWriter < Writer + attr_accessor :api_key delegate :docs_dir, :to => :configuration def initialize(index, configuration) - self.index = index - self.configuration = configuration + super self.api_key = configuration.api_name.parameterize end - def self.write(index, configuration) - writer = new(index, configuration) - writer.write - end - def write File.open(docs_dir.join("apiconfig.json"), "w+") do |file| file.write Formatter.to_json(ApiConfig.new(configuration)) @@ -34,7 +28,7 @@ def initialize(index, configuration) end def sections - IndexWriter.sections(examples, @configuration) + IndexHelper.sections(examples, @configuration) end def examples diff --git a/lib/rspec_api_documentation/writers/json_writer.rb b/lib/rspec_api_documentation/writers/json_writer.rb index da3d5851..6fbe4d37 100644 --- a/lib/rspec_api_documentation/writers/json_writer.rb +++ b/lib/rspec_api_documentation/writers/json_writer.rb @@ -2,24 +2,17 @@ module RspecApiDocumentation module Writers - class JsonWriter - attr_accessor :index, :configuration + class JsonWriter < Writer delegate :docs_dir, :to => :configuration - def initialize(index, configuration) - self.index = index - self.configuration = configuration - end - - def self.write(index, configuration) - writer = new(index, configuration) - writer.write - end - def write File.open(docs_dir.join("index.json"), "w+") do |f| f.write Formatter.to_json(JsonIndex.new(index, configuration)) end + write_examples + end + + def write_examples index.examples.each do |example| json_example = JsonExample.new(example, configuration) FileUtils.mkdir_p(docs_dir.join(json_example.dirname)) @@ -37,7 +30,7 @@ def initialize(index, configuration) end def sections - IndexWriter.sections(examples, @configuration) + IndexHelper.sections(examples, @configuration) end def examples @@ -46,19 +39,23 @@ def examples def as_json(opts = nil) sections.inject({:resources => []}) do |h, section| - h[:resources].push( - :name => section[:resource_name], - :examples => section[:examples].map { |example| - { - :description => example.description, - :link => "#{example.dirname}/#{example.filename}", - :groups => example.metadata[:document] - } - } - ) + h[:resources].push(section_hash(section)) h end end + + def section_hash(section) + { + :name => section[:resource_name], + :examples => section[:examples].map { |example| + { + :description => example.description, + :link => "#{example.dirname}/#{example.filename}", + :groups => example.metadata[:document] + } + } + } + end end class JsonExample diff --git a/lib/rspec_api_documentation/writers/textile_writer.rb b/lib/rspec_api_documentation/writers/textile_writer.rb index 6b79b209..84526282 100644 --- a/lib/rspec_api_documentation/writers/textile_writer.rb +++ b/lib/rspec_api_documentation/writers/textile_writer.rb @@ -1,8 +1,6 @@ module RspecApiDocumentation module Writers class TextileWriter < GeneralMarkupWriter - attr_accessor :index, :configuration - EXTENSION = 'textile' def markup_index_class diff --git a/lib/rspec_api_documentation/writers/writer.rb b/lib/rspec_api_documentation/writers/writer.rb new file mode 100644 index 00000000..39713c04 --- /dev/null +++ b/lib/rspec_api_documentation/writers/writer.rb @@ -0,0 +1,25 @@ +module RspecApiDocumentation + module Writers + class Writer + attr_accessor :index, :configuration + + def initialize(index, configuration) + self.index = index + self.configuration = configuration + end + + def self.write(index, configuration) + writer = new(index, configuration) + writer.write + end + + def self.clear_docs(docs_dir) + if File.exists?(docs_dir) + FileUtils.rm_rf(docs_dir, :secure => true) + end + FileUtils.mkdir_p(docs_dir) + end + end + end +end + diff --git a/spec/writers/index_writer_spec.rb b/spec/writers/index_writer_spec.rb index 1845be1f..66c48867 100644 --- a/spec/writers/index_writer_spec.rb +++ b/spec/writers/index_writer_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe RspecApiDocumentation::Writers::IndexWriter do +describe RspecApiDocumentation::Writers::IndexHelper do describe "#sections" do let(:example_1) { double(:resource_name => "Order", :description => "Updating an order") } let(:example_2) { double(:resource_name => "Order", :description => "Creating an order") } @@ -9,7 +9,7 @@ context "with default value for keep_source_order" do let(:configuration) { RspecApiDocumentation::Configuration.new } - subject { RspecApiDocumentation::Writers::IndexWriter.sections(examples, configuration) } + subject { RspecApiDocumentation::Writers::IndexHelper.sections(examples, configuration) } it "should order resources by resource name" do subject.map { |resource| resource[:resource_name] }.should == ["Cart", "Order"] @@ -21,7 +21,7 @@ end context "with keep_source_order set to true" do - subject { RspecApiDocumentation::Writers::IndexWriter.sections(examples, double(:keep_source_order => true)) } + subject { RspecApiDocumentation::Writers::IndexHelper.sections(examples, double(:keep_source_order => true)) } it "should order resources by source code declaration" do subject.map { |resource| resource[:resource_name] }.should == ["Order", "Cart"]