Permalink
Browse files

use a 'formatter' for all json writting, JSON.pretty_generate to make…

… diffs prettier
  • Loading branch information...
1 parent 4f20602 commit 7c0c121993f9fe6a9aec36e2d27727213633d7f6 @jacobo jacobo committed Jun 18, 2013
@@ -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 "["
@@ -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
@@ -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,15 +96,15 @@ 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,
:protocol => "http",
:publicPath => "",
:baseURL => @configuration.curl_host
}
- }.to_json
+ }
end
end
end
@@ -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

1 comment on commit 7c0c121

Neat idea.

Please sign in to comment.