Skip to content

Commit c7f8736

Browse files
committed
Add swagger formating
1 parent 03a87b6 commit c7f8736

File tree

8 files changed

+170
-12
lines changed

8 files changed

+170
-12
lines changed

.bundle/config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
BUNDLE_JOBS: 8

example/Gemfile.lock

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ GIT
1010
PATH
1111
remote: ../
1212
specs:
13-
rspec_api_documentation (1.1.0)
13+
rspec_api_documentation (2.0.0)
1414
activesupport (>= 3.0.0)
1515
i18n (>= 0.1.0)
1616
json (>= 1.4.6)
1717
mustache (>= 0.99.4)
1818
rspec (>= 2.14.0)
19-
webmock (>= 1.7.0)
2019

2120
GEM
2221
remote: http://rubygems.org/
@@ -49,7 +48,6 @@ GEM
4948
activesupport (= 3.1.3)
5049
activesupport (3.1.3)
5150
multi_json (~> 1.0)
52-
addressable (2.3.5)
5351
arel (2.2.3)
5452
builder (3.0.4)
5553
coffee-rails (3.1.1)
@@ -59,8 +57,6 @@ GEM
5957
coffee-script-source
6058
execjs
6159
coffee-script-source (1.6.3)
62-
crack (0.4.1)
63-
safe_yaml (~> 0.9.0)
6460
diff-lcs (1.2.4)
6561
erubis (2.7.0)
6662
execjs (1.4.0)
@@ -86,7 +82,7 @@ GEM
8682
treetop (~> 1.4.8)
8783
mime-types (1.23)
8884
multi_json (1.7.9)
89-
mustache (0.99.4)
85+
mustache (0.99.5)
9086
polyglot (0.3.3)
9187
rack (1.3.10)
9288
rack-cache (1.2)
@@ -134,7 +130,6 @@ GEM
134130
rspec-core (~> 2.14.0)
135131
rspec-expectations (~> 2.14.0)
136132
rspec-mocks (~> 2.14.0)
137-
safe_yaml (0.9.5)
138133
sass (3.2.10)
139134
sass-rails (3.1.7)
140135
actionpack (~> 3.1.0)
@@ -159,9 +154,6 @@ GEM
159154
uglifier (2.1.2)
160155
execjs (>= 0.3.0)
161156
multi_json (~> 1.0, >= 1.0.2)
162-
webmock (1.13.0)
163-
addressable (>= 2.2.7)
164-
crack (>= 0.3.2)
165157

166158
PLATFORMS
167159
ruby

example/doc/README_FOR_APP

Lines changed: 0 additions & 2 deletions
This file was deleted.

lib/rspec_api_documentation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module Writers
3333
autoload :HtmlWriter
3434
autoload :TextileWriter
3535
autoload :JsonWriter
36+
autoload :JsonSwaggerWriter
3637
autoload :AppendJsonWriter
3738
autoload :JsonIodocsWriter
3839
autoload :IndexHelper

lib/rspec_api_documentation/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def self.add_setting(name, opts = {})
5656

5757
add_setting :curl_headers_to_filter, :default => nil
5858
add_setting :curl_host, :default => nil
59+
add_setting :base_api_path, :default => nil
60+
add_setting :api_version, :default => nil
5961
add_setting :keep_source_order, :default => false
6062
add_setting :api_name, :default => "API Documentation"
6163
add_setting :io_docs_protocol, :default => "http"

lib/rspec_api_documentation/dsl/resource.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def self.define_action(method)
99
options[:method] = method
1010
options[:route] = args.first
1111
options[:api_doc_dsl] = :endpoint
12+
options[:descriptionf] = "afdsfasdfsdf"
1213
args.push(options)
1314
args[0] = "#{method.to_s.upcase} #{args[0]}"
1415
context(*args, &block)
@@ -45,6 +46,18 @@ def header(name, value)
4546
headers[name] = value
4647
end
4748

49+
def descriptionf(text)
50+
example.metadata[:descriptionf] = text
51+
end
52+
53+
def root_path(path)
54+
metadata[:root_path] = path
55+
if superclass_metadata && metadata[:root_path].equal?(superclass_metadata[:root_path])
56+
metadata[:root_path] = Marshal.load(Marshal.dump(superclass_metadata[:root_path]))
57+
end
58+
metadata[:root_path]
59+
end
60+
4861
private
4962
def parameters
5063
metadata[:parameters] ||= []
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
require 'rspec_api_documentation/writers/formatter'
2+
3+
module RspecApiDocumentation
4+
module Writers
5+
class JsonSwaggerWriter < Writer
6+
delegate :docs_dir, :to => :configuration
7+
8+
def write
9+
File.open(docs_dir.join("index.json"), "w+") do |f|
10+
f.write Formatter.to_json(JsonSwaggerIndex.new(index, configuration))
11+
end
12+
write_examples
13+
end
14+
15+
def write_examples
16+
json_examples = {}
17+
index.examples.each do |example|
18+
json_example = JsonSwaggerExample.new(example, configuration)
19+
(json_examples[json_example.dirname] ||= []) << JsonSwaggerExample.new(example, configuration)
20+
end
21+
22+
json_examples.each do |dirname, examples|
23+
File.open(docs_dir.join(dirname + ".json"), "a+") do |f|
24+
f.write Formatter.to_json(format_examples(examples))
25+
end
26+
end
27+
end
28+
29+
def format_examples(json_examples)
30+
{
31+
basePath: @configuration.base_api_path,
32+
apiVersion: @configuration.api_version,
33+
apis: []
34+
}
35+
end
36+
end
37+
38+
class JsonSwaggerIndex
39+
def initialize(index, configuration)
40+
@index = index
41+
@configuration = configuration
42+
end
43+
44+
def sections
45+
IndexHelper.sections(examples, @configuration)
46+
end
47+
48+
def examples
49+
@index.examples.map { |example| JsonSwaggerExample.new(example, @configuration) }
50+
end
51+
52+
def as_json(opts = nil)
53+
{
54+
basePath: @configuration.base_api_path,
55+
apiVersion: @configuration.api_version,
56+
apis: section_hash
57+
}
58+
end
59+
60+
def section_hash
61+
sections.map do |section|
62+
{
63+
path: section[:examples].first.metadata[:root_path],
64+
description: section[:resource_name]
65+
}
66+
end
67+
end
68+
end
69+
70+
class JsonSwaggerExample
71+
def initialize(example, configuration)
72+
@example = example
73+
@host = configuration.curl_host
74+
@filter_headers = configuration.curl_headers_to_filter
75+
end
76+
77+
def method_missing(method, *args, &block)
78+
@example.send(method, *args, &block)
79+
end
80+
81+
def respond_to?(method, include_private = false)
82+
super || @example.respond_to?(method, include_private)
83+
end
84+
85+
def dirname
86+
resource_name.downcase.gsub(/\s+/, '_')
87+
end
88+
89+
def filename
90+
basename = description.downcase.gsub(/\s+/, '_').gsub(/[^a-z_]/, '')
91+
"#{basename}.json"
92+
end
93+
94+
def as_json(opts = nil)
95+
{
96+
method: method,
97+
operations: [],
98+
resource: resource_name,
99+
:http_method => http_method,
100+
:description => description,
101+
:explanation => explanation,
102+
:parameters => respond_to?(:parameters) ? parameters : [],
103+
}
104+
end
105+
106+
def requests
107+
super.map do |hash|
108+
if @host
109+
if hash[:curl].is_a? RspecApiDocumentation::Curl
110+
hash[:curl] = hash[:curl].output(@host, @filter_headers)
111+
end
112+
else
113+
hash[:curl] = nil
114+
end
115+
hash
116+
end
117+
end
118+
end
119+
end
120+
end

spec/writers/json_swagger_writer.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
3+
describe RspecApiDocumentation::Writers::JsonWriter do
4+
let(:index) { RspecApiDocumentation::Index.new }
5+
let(:configuration) { RspecApiDocumentation::Configuration.new }
6+
7+
describe ".write" do
8+
let(:writer) { double(:writer) }
9+
10+
it "should build a new writer and write the docs" do
11+
described_class.stub(:new).with(index, configuration).and_return(writer)
12+
writer.should_receive(:write)
13+
described_class.write(index, configuration)
14+
end
15+
end
16+
17+
describe "#write" do
18+
let(:writer) { described_class.new(index, configuration) }
19+
20+
before do
21+
FileUtils.mkdir_p(configuration.docs_dir)
22+
end
23+
24+
it "should write the index" do
25+
writer.write
26+
index_file = File.join(configuration.docs_dir, "index.json")
27+
File.exists?(index_file).should be_true
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)