diff --git a/lib/rspec_api_documentation/writers/combined_json_writer.rb b/lib/rspec_api_documentation/writers/combined_json_writer.rb index 1eea5f5e..a77f0c1a 100644 --- a/lib/rspec_api_documentation/writers/combined_json_writer.rb +++ b/lib/rspec_api_documentation/writers/combined_json_writer.rb @@ -7,7 +7,7 @@ def self.write(index, configuration) File.open(configuration.docs_dir.join("combined.json"), "w+") do |f| examples = [] index.examples.each do |rspec_example| - examples << JsonExample.new(rspec_example, configuration).to_json + examples << Formatter.to_json(JsonExample.new(rspec_example, configuration)) end f.write "[" diff --git a/lib/rspec_api_documentation/writers/formatter.rb b/lib/rspec_api_documentation/writers/formatter.rb new file mode 100644 index 00000000..11c70dd8 --- /dev/null +++ b/lib/rspec_api_documentation/writers/formatter.rb @@ -0,0 +1,11 @@ +module RspecApiDocumentation + module Writers + module Formatter + + def self.to_json(object) + JSON.pretty_generate(object.as_json) + end + + end + end +end \ No newline at end of file diff --git a/lib/rspec_api_documentation/writers/json_iodocs_writer.rb b/lib/rspec_api_documentation/writers/json_iodocs_writer.rb index ca3e28ba..072e1a8b 100644 --- a/lib/rspec_api_documentation/writers/json_iodocs_writer.rb +++ b/lib/rspec_api_documentation/writers/json_iodocs_writer.rb @@ -1,3 +1,5 @@ +require 'rspec_api_documentation/writers/formatter' + module RspecApiDocumentation module Writers class JsonIodocsWriter @@ -17,10 +19,10 @@ def self.write(index, configuration) def write File.open(docs_dir.join("apiconfig.json"), "w+") do |file| - file.write ApiConfig.new(configuration).to_json + file.write Formatter.to_json(ApiConfig.new(configuration)) end File.open(docs_dir.join("#{api_key}.json"), "w+") do |file| - file.write JsonIndex.new(index, configuration).to_json + file.write Formatter.to_json(JsonIndex.new(index, configuration)) end end end @@ -39,16 +41,16 @@ def examples @index.examples.map { |example| JsonExample.new(example, @configuration) } end - def to_json + def as_json(opts = nil) sections.inject({:endpoints => []}) do |h, section| h[:endpoints].push( :name => section[:resource_name], :methods => section[:examples].map do |example| - example.to_json + example.as_json(opts) end ) h - end.to_json + end end end @@ -76,7 +78,7 @@ def parameters params end - def to_json + def as_json(opts = nil) { :MethodName => description, :Synopsis => explanation, @@ -94,7 +96,7 @@ def initialize(configuration) @api_key = configuration.api_name.parameterize end - def to_json + def as_json(opts = nil) { @api_key.to_sym => { :name => @configuration.api_name, @@ -102,7 +104,7 @@ def to_json :publicPath => "", :baseURL => @configuration.curl_host } - }.to_json + } end end end diff --git a/lib/rspec_api_documentation/writers/json_writer.rb b/lib/rspec_api_documentation/writers/json_writer.rb index 6cd81eb9..da3d5851 100644 --- a/lib/rspec_api_documentation/writers/json_writer.rb +++ b/lib/rspec_api_documentation/writers/json_writer.rb @@ -1,3 +1,5 @@ +require 'rspec_api_documentation/writers/formatter' + module RspecApiDocumentation module Writers class JsonWriter @@ -16,13 +18,13 @@ def self.write(index, configuration) def write File.open(docs_dir.join("index.json"), "w+") do |f| - f.write JsonIndex.new(index, configuration).to_json + f.write Formatter.to_json(JsonIndex.new(index, configuration)) end index.examples.each do |example| json_example = JsonExample.new(example, configuration) FileUtils.mkdir_p(docs_dir.join(json_example.dirname)) File.open(docs_dir.join(json_example.dirname, json_example.filename), "w+") do |f| - f.write json_example.to_json + f.write Formatter.to_json(json_example) end end end @@ -42,7 +44,7 @@ def examples @index.examples.map { |example| JsonExample.new(example, @configuration) } end - def to_json + def as_json(opts = nil) sections.inject({:resources => []}) do |h, section| h[:resources].push( :name => section[:resource_name], @@ -55,7 +57,7 @@ def to_json } ) h - end.to_json + end end end @@ -82,7 +84,7 @@ def filename "#{basename}.json" end - def as_json + def as_json(opts = nil) { :resource => resource_name, :http_method => http_method, @@ -94,10 +96,6 @@ def as_json } end - def to_json - as_json.to_json - end - def requests super.map do |hash| if @host