Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON.pretty_generate makes better diffs #75

Merged
merged 1 commit into from
Jun 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure I like the switch from #to_json to #as_json. I don't like assuming we're in a active support environment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, it's for the JSON.pretty_generate. Scratch my previous comment.

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