Skip to content

Commit

Permalink
Merge pull request #75 from engineyard/master
Browse files Browse the repository at this point in the history
JSON.pretty_generate makes better diffs
  • Loading branch information
oestrich committed Jun 24, 2013
2 parents 4f20602 + 7c0c121 commit 9e0af39
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 "["
Expand Down
11 changes: 11 additions & 0 deletions lib/rspec_api_documentation/writers/formatter.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 10 additions & 8 deletions lib/rspec_api_documentation/writers/json_iodocs_writer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rspec_api_documentation/writers/formatter'

module RspecApiDocumentation
module Writers
class JsonIodocsWriter
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -76,7 +78,7 @@ def parameters
params
end

def to_json
def as_json(opts = nil)
{
:MethodName => description,
:Synopsis => explanation,
Expand All @@ -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
Expand Down
16 changes: 7 additions & 9 deletions lib/rspec_api_documentation/writers/json_writer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rspec_api_documentation/writers/formatter'

module RspecApiDocumentation
module Writers
class JsonWriter
Expand All @@ -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
Expand All @@ -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],
Expand All @@ -55,7 +57,7 @@ def to_json
}
)
h
end.to_json
end
end
end

Expand All @@ -82,7 +84,7 @@ def filename
"#{basename}.json"
end

def as_json
def as_json(opts = nil)
{
:resource => resource_name,
:http_method => http_method,
Expand All @@ -94,10 +96,6 @@ def as_json
}
end

def to_json
as_json.to_json
end

def requests
super.map do |hash|
if @host
Expand Down

0 comments on commit 9e0af39

Please sign in to comment.