Skip to content

Commit 9e0af39

Browse files
committed
Merge pull request zipmark#75 from engineyard/master
JSON.pretty_generate makes better diffs
2 parents 4f20602 + 7c0c121 commit 9e0af39

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

lib/rspec_api_documentation/writers/combined_json_writer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def self.write(index, configuration)
77
File.open(configuration.docs_dir.join("combined.json"), "w+") do |f|
88
examples = []
99
index.examples.each do |rspec_example|
10-
examples << JsonExample.new(rspec_example, configuration).to_json
10+
examples << Formatter.to_json(JsonExample.new(rspec_example, configuration))
1111
end
1212

1313
f.write "["
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module RspecApiDocumentation
2+
module Writers
3+
module Formatter
4+
5+
def self.to_json(object)
6+
JSON.pretty_generate(object.as_json)
7+
end
8+
9+
end
10+
end
11+
end

lib/rspec_api_documentation/writers/json_iodocs_writer.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'rspec_api_documentation/writers/formatter'
2+
13
module RspecApiDocumentation
24
module Writers
35
class JsonIodocsWriter
@@ -17,10 +19,10 @@ def self.write(index, configuration)
1719

1820
def write
1921
File.open(docs_dir.join("apiconfig.json"), "w+") do |file|
20-
file.write ApiConfig.new(configuration).to_json
22+
file.write Formatter.to_json(ApiConfig.new(configuration))
2123
end
2224
File.open(docs_dir.join("#{api_key}.json"), "w+") do |file|
23-
file.write JsonIndex.new(index, configuration).to_json
25+
file.write Formatter.to_json(JsonIndex.new(index, configuration))
2426
end
2527
end
2628
end
@@ -39,16 +41,16 @@ def examples
3941
@index.examples.map { |example| JsonExample.new(example, @configuration) }
4042
end
4143

42-
def to_json
44+
def as_json(opts = nil)
4345
sections.inject({:endpoints => []}) do |h, section|
4446
h[:endpoints].push(
4547
:name => section[:resource_name],
4648
:methods => section[:examples].map do |example|
47-
example.to_json
49+
example.as_json(opts)
4850
end
4951
)
5052
h
51-
end.to_json
53+
end
5254
end
5355
end
5456

@@ -76,7 +78,7 @@ def parameters
7678
params
7779
end
7880

79-
def to_json
81+
def as_json(opts = nil)
8082
{
8183
:MethodName => description,
8284
:Synopsis => explanation,
@@ -94,15 +96,15 @@ def initialize(configuration)
9496
@api_key = configuration.api_name.parameterize
9597
end
9698

97-
def to_json
99+
def as_json(opts = nil)
98100
{
99101
@api_key.to_sym => {
100102
:name => @configuration.api_name,
101103
:protocol => "http",
102104
:publicPath => "",
103105
:baseURL => @configuration.curl_host
104106
}
105-
}.to_json
107+
}
106108
end
107109
end
108110
end

lib/rspec_api_documentation/writers/json_writer.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'rspec_api_documentation/writers/formatter'
2+
13
module RspecApiDocumentation
24
module Writers
35
class JsonWriter
@@ -16,13 +18,13 @@ def self.write(index, configuration)
1618

1719
def write
1820
File.open(docs_dir.join("index.json"), "w+") do |f|
19-
f.write JsonIndex.new(index, configuration).to_json
21+
f.write Formatter.to_json(JsonIndex.new(index, configuration))
2022
end
2123
index.examples.each do |example|
2224
json_example = JsonExample.new(example, configuration)
2325
FileUtils.mkdir_p(docs_dir.join(json_example.dirname))
2426
File.open(docs_dir.join(json_example.dirname, json_example.filename), "w+") do |f|
25-
f.write json_example.to_json
27+
f.write Formatter.to_json(json_example)
2628
end
2729
end
2830
end
@@ -42,7 +44,7 @@ def examples
4244
@index.examples.map { |example| JsonExample.new(example, @configuration) }
4345
end
4446

45-
def to_json
47+
def as_json(opts = nil)
4648
sections.inject({:resources => []}) do |h, section|
4749
h[:resources].push(
4850
:name => section[:resource_name],
@@ -55,7 +57,7 @@ def to_json
5557
}
5658
)
5759
h
58-
end.to_json
60+
end
5961
end
6062
end
6163

@@ -82,7 +84,7 @@ def filename
8284
"#{basename}.json"
8385
end
8486

85-
def as_json
87+
def as_json(opts = nil)
8688
{
8789
:resource => resource_name,
8890
:http_method => http_method,
@@ -94,10 +96,6 @@ def as_json
9496
}
9597
end
9698

97-
def to_json
98-
as_json.to_json
99-
end
100-
10199
def requests
102100
super.map do |hash|
103101
if @host

0 commit comments

Comments
 (0)