Skip to content

Commit

Permalink
All writers now inherit from a base writer
Browse files Browse the repository at this point in the history
* the writers are now responsible for cleaning docs
* added an append_json writer that does not delete docs and simply
    modifies index.json
  • Loading branch information
alenia committed Oct 16, 2013
1 parent d72deda commit ec120b2
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 61 deletions.
4 changes: 3 additions & 1 deletion lib/rspec_api_documentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/rspec_api_documentation/api_documentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec_api_documentation/views/markup_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions lib/rspec_api_documentation/writers/append_json_writer.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 1 addition & 13 deletions lib/rspec_api_documentation/writers/general_markup_writer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/rspec_api_documentation/writers/html_writer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module RspecApiDocumentation
module Writers
class HtmlWriter < GeneralMarkupWriter
attr_accessor :index, :configuration

EXTENSION = 'html'

def markup_index_class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 4 additions & 10 deletions lib/rspec_api_documentation/writers/json_iodocs_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -34,7 +28,7 @@ def initialize(index, configuration)
end

def sections
IndexWriter.sections(examples, @configuration)
IndexHelper.sections(examples, @configuration)
end

def examples
Expand Down
43 changes: 20 additions & 23 deletions lib/rspec_api_documentation/writers/json_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -37,7 +30,7 @@ def initialize(index, configuration)
end

def sections
IndexWriter.sections(examples, @configuration)
IndexHelper.sections(examples, @configuration)
end

def examples
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/rspec_api_documentation/writers/textile_writer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module RspecApiDocumentation
module Writers
class TextileWriter < GeneralMarkupWriter
attr_accessor :index, :configuration

EXTENSION = 'textile'

def markup_index_class
Expand Down
25 changes: 25 additions & 0 deletions lib/rspec_api_documentation/writers/writer.rb
Original file line number Diff line number Diff line change
@@ -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

6 changes: 3 additions & 3 deletions spec/writers/index_writer_spec.rb
Original file line number Diff line number Diff line change
@@ -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") }
Expand All @@ -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"]
Expand All @@ -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"]
Expand Down

0 comments on commit ec120b2

Please sign in to comment.