Skip to content

Commit d3a8bcb

Browse files
committed
Merge pull request #1 from SqREL/change-json
Add swagger formating
2 parents a504f94 + ff4cd94 commit d3a8bcb

File tree

138 files changed

+208
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+208
-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

.gitignore

100644100755
File mode changed.

.rspec

100644100755
File mode changed.

.travis.yml

100644100755
File mode changed.

Gemfile

100644100755
File mode changed.

Gemfile.lock

100644100755
File mode changed.

LICENSE.md

100644100755
File mode changed.

README.md

100644100755
File mode changed.

Rakefile

100644100755
File mode changed.

example/.gitignore

100644100755
File mode changed.

example/.rspec

100644100755
File mode changed.

example/Gemfile

100644100755
File mode changed.

example/Gemfile.lock

100644100755
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/README

100644100755
File mode changed.

example/Rakefile

100644100755
File mode changed.

example/app/assets/images/rails.png

100644100755
File mode changed.

example/app/assets/javascripts/application.js

100644100755
File mode changed.

example/app/assets/stylesheets/application.css

100644100755
File mode changed.

example/app/controllers/application_controller.rb

100644100755
File mode changed.

example/app/controllers/orders_controller.rb

100644100755
File mode changed.

example/app/helpers/application_helper.rb

100644100755
File mode changed.

example/app/mailers/.gitkeep

100644100755
File mode changed.

example/app/models/.gitkeep

100644100755
File mode changed.

example/app/models/order.rb

100644100755
File mode changed.

example/app/views/layouts/application.html.erb

100644100755
File mode changed.

example/config.ru

100644100755
File mode changed.

example/config/application.rb

100644100755
File mode changed.

example/config/boot.rb

100644100755
File mode changed.

example/config/database.yml

100644100755
File mode changed.

example/config/environment.rb

100644100755
File mode changed.

example/config/environments/development.rb

100644100755
File mode changed.

example/config/environments/production.rb

100644100755
File mode changed.

example/config/environments/test.rb

100644100755
File mode changed.

example/config/initializers/backtrace_silencers.rb

100644100755
File mode changed.

example/config/initializers/inflections.rb

100644100755
File mode changed.

example/config/initializers/mime_types.rb

100644100755
File mode changed.

example/config/initializers/raddocs.rb

100644100755
File mode changed.

example/config/initializers/secret_token.rb

100644100755
File mode changed.

example/config/initializers/session_store.rb

100644100755
File mode changed.

example/config/initializers/wrap_parameters.rb

100644100755
File mode changed.

example/config/locales/en.yml

100644100755
File mode changed.

example/config/routes.rb

100644100755
File mode changed.

example/db/migrate/20111215005439_create_orders.rb

100644100755
File mode changed.

example/db/schema.rb

100644100755
File mode changed.

example/db/seeds.rb

100644100755
File mode changed.

example/doc/README_FOR_APP

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

example/lib/assets/.gitkeep

100644100755
File mode changed.

example/lib/tasks/.gitkeep

100644100755
File mode changed.

example/log/.gitkeep

100644100755
File mode changed.

example/public/404.html

100644100755
File mode changed.

example/public/422.html

100644100755
File mode changed.

example/public/500.html

100644100755
File mode changed.

example/public/favicon.ico

100644100755
File mode changed.

example/public/index.html

100644100755
File mode changed.

example/public/robots.txt

100644100755
File mode changed.

example/spec/acceptance/orders_spec.rb

100644100755
File mode changed.

example/spec/spec_helper.rb

100644100755
File mode changed.

features/callbacks.feature

100644100755
File mode changed.

features/combined_json.feature

100644100755
File mode changed.

features/combined_text.feature

100644100755
File mode changed.

features/curl.feature

100644100755
File mode changed.

features/example_request.feature

100644100755
File mode changed.

features/fixtures/file.png

100644100755
File mode changed.

features/folder_structure.feature

100644100755
File mode changed.

features/headers.feature

100644100755
File mode changed.

features/html_documentation.feature

100644100755
File mode changed.

features/json_iodocs.feature

100644100755
File mode changed.

features/oauth2_mac_client.feature

100644100755
File mode changed.

features/patch.feature

100644100755
File mode changed.

features/readme.md

100644100755
File mode changed.

features/redefining_client.feature

100644100755
File mode changed.

features/step_definitions/curl_steps.rb

100644100755
File mode changed.

features/step_definitions/html_steps.rb

100644100755
File mode changed.

features/step_definitions/image_steps.rb

100644100755
File mode changed.

features/step_definitions/json_steps.rb

100644100755
File mode changed.

features/support/capybara.rb

100644100755
File mode changed.

features/support/env.rb

100644100755
File mode changed.

features/textile_documentation.feature

100644100755
File mode changed.

features/upload_file.feature

100644100755
File mode changed.

lib/rspec_api_documentation.rb

100644100755
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/api_documentation.rb

100644100755
File mode changed.

lib/rspec_api_documentation/api_formatter.rb

100644100755
File mode changed.

lib/rspec_api_documentation/client_base.rb

100644100755
File mode changed.

lib/rspec_api_documentation/configuration.rb

100644100755
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/curl.rb

100644100755
File mode changed.

lib/rspec_api_documentation/dsl.rb

100644100755
File mode changed.

lib/rspec_api_documentation/dsl/callback.rb

100644100755
File mode changed.

lib/rspec_api_documentation/dsl/endpoint.rb

100644100755
File mode changed.

lib/rspec_api_documentation/dsl/resource.rb

100644100755
Lines changed: 30 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)
@@ -41,10 +42,39 @@ def parameter(name, description, options = {})
4142
parameters.push(options.merge(:name => name.to_s, :description => description))
4243
end
4344

45+
def modified_root_path(path)
46+
metadata[:modified_root_path] = path
47+
if superclass_metadata && metadata[:modified_root_path].equal?(superclass_metadata[:modified_root_path])
48+
metadata[:modified_root_path] = Marshal.load(Marshal.dump(superclass_metadata[:modified_root_path]))
49+
end
50+
metadata[:modified_root_path]
51+
end
52+
4453
def header(name, value)
4554
headers[name] = value
4655
end
4756

57+
def descriptionf(text)
58+
example.metadata[:descriptionf] = text
59+
end
60+
61+
def example(title, params={}, &block)
62+
metadata[:mute] = !!params[:mute]
63+
if superclass_metadata && metadata[:mute].equal?(superclass_metadata[:mute])
64+
metadata[:mute] = Marshal.load(Marshal.dump(superclass_metadata[:mute]))
65+
end
66+
67+
super title, &block
68+
end
69+
70+
def root_path(path)
71+
metadata[:root_path] = path
72+
if superclass_metadata && metadata[:root_path].equal?(superclass_metadata[:root_path])
73+
metadata[:root_path] = Marshal.load(Marshal.dump(superclass_metadata[:root_path]))
74+
end
75+
metadata[:root_path]
76+
end
77+
4878
private
4979
def parameters
5080
metadata[:parameters] ||= []

lib/rspec_api_documentation/example.rb

100644100755
File mode changed.

lib/rspec_api_documentation/headers.rb

100644100755
File mode changed.

lib/rspec_api_documentation/index.rb

100644100755
File mode changed.

lib/rspec_api_documentation/oauth2_mac_client.rb

100644100755
File mode changed.

lib/rspec_api_documentation/rack_test_client.rb

100644100755
File mode changed.

lib/rspec_api_documentation/railtie.rb

100644100755
File mode changed.

lib/rspec_api_documentation/test_server.rb

100644100755
File mode changed.

lib/rspec_api_documentation/views/html_example.rb

100644100755
File mode changed.

lib/rspec_api_documentation/views/html_index.rb

100644100755
File mode changed.

lib/rspec_api_documentation/views/markup_example.rb

100644100755
File mode changed.

lib/rspec_api_documentation/views/markup_index.rb

100644100755
File mode changed.

lib/rspec_api_documentation/views/textile_example.rb

100644100755
File mode changed.

lib/rspec_api_documentation/views/textile_index.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/append_json_writer.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/combined_json_writer.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/combined_text_writer.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/formatter.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/general_markup_writer.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/html_writer.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/index_helper.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/json_iodocs_writer.rb

100644100755
File mode changed.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
FileUtils.rm_rf docs_dir.join('v1')
10+
FileUtils.mkdir_p(docs_dir.join('v1'))
11+
12+
File.open(docs_dir.join('v1', "index.json"), "w+") do |f|
13+
f.write Formatter.to_json(JsonSwaggerIndex.new(index, configuration))
14+
end
15+
write_examples
16+
end
17+
18+
def write_examples
19+
json_examples = {}
20+
a = []
21+
index.examples.each do |example|
22+
unless example.mute
23+
a << example.route
24+
json_example = JsonSwaggerExample.new(example, configuration)
25+
dirname = example.route.split('/').reject(&:blank?)
26+
FileUtils.mkdir_p(docs_dir.join(dirname[0..-2].join('/')))
27+
#(json_examples[docs_dir.join(dirname.join('/') << '.json')] ||= []) << JsonSwaggerExample.new(example, configuration)
28+
(json_examples[docs_dir.join('v1', (example.root_path.gsub(/^\//, "") << '.json'))] ||= []) << JsonSwaggerExample.new(example, configuration)
29+
end
30+
end
31+
32+
json_examples.each do |dirname, examples|
33+
#directory = File.dirname(dirname)
34+
35+
File.open(docs_dir.join(dirname), "a+") do |f|
36+
f.write Formatter.to_json(format_examples(examples))
37+
end
38+
end
39+
end
40+
41+
def format_examples(json_examples)
42+
json_examples_sorted = {}
43+
json_examples.each do |example|
44+
root_path = example.root_path
45+
root_path = example.modified_root_path if example.try(:modified_root_path)
46+
(json_examples_sorted[root_path] ||= []) << example
47+
end
48+
49+
{
50+
basePath: @configuration.base_api_path,
51+
apiVersion: @configuration.api_version,
52+
apis: json_examples_sorted.map { |path, examples| { path: path, operations: examples } }
53+
}
54+
end
55+
end
56+
57+
class JsonSwaggerIndex
58+
def initialize(index, configuration)
59+
@index = index
60+
@configuration = configuration
61+
end
62+
63+
def sections
64+
IndexHelper.sections(examples, @configuration)
65+
end
66+
67+
def examples
68+
@index.examples.map { |example| JsonSwaggerExample.new(example, @configuration) }
69+
end
70+
71+
def as_json(opts = nil)
72+
{
73+
apiVersion: @configuration.api_version,
74+
apis: section_hash
75+
}
76+
end
77+
78+
def section_hash
79+
sections.map do |section|
80+
{
81+
path: section[:examples].first.metadata[:root_path],
82+
description: section[:resource_name]
83+
}
84+
end
85+
end
86+
end
87+
88+
class JsonSwaggerExample
89+
def initialize(example, configuration)
90+
@example = example
91+
@host = configuration.curl_host
92+
@filter_headers = configuration.curl_headers_to_filter
93+
end
94+
95+
def method_missing(method, *args, &block)
96+
@example.send(method, *args, &block)
97+
end
98+
99+
def respond_to?(method, include_private = false)
100+
super || @example.respond_to?(method, include_private)
101+
end
102+
103+
def dirname
104+
resource_name.downcase.gsub(/\s+/, '_')
105+
end
106+
107+
def filename
108+
basename = description.downcase.gsub(/\s+/, '_').gsub(/[^a-z_]/, '')
109+
"#{basename}.json"
110+
end
111+
112+
def as_json(opts = nil)
113+
{
114+
method: http_method,
115+
type: resource_name,
116+
summary: description,
117+
nickname: "#{http_method}#{resource_name}",
118+
parameters: respond_to?(:parameters) ? formatted_parameters : [],
119+
responseMessages: []
120+
}
121+
end
122+
123+
def requests
124+
super.map do |hash|
125+
if @host
126+
if hash[:curl].is_a? RspecApiDocumentation::Curl
127+
hash[:curl] = hash[:curl].output(@host, @filter_headers)
128+
end
129+
else
130+
hash[:curl] = nil
131+
end
132+
hash
133+
end
134+
end
135+
136+
def formatted_parameters
137+
parameters
138+
end
139+
end
140+
end
141+
end

lib/rspec_api_documentation/writers/json_writer.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/textile_writer.rb

100644100755
File mode changed.

lib/rspec_api_documentation/writers/writer.rb

100644100755
File mode changed.

lib/tasks/docs.rake

100644100755
File mode changed.

rspec_api_documentation.gemspec

100644100755
File mode changed.

spec/api_documentation_spec.rb

100644100755
File mode changed.

spec/api_formatter_spec.rb

100644100755
File mode changed.

spec/configuration_spec.rb

100644100755
File mode changed.

spec/curl_spec.rb

100644100755
File mode changed.

spec/dsl_spec.rb

100644100755
File mode changed.

spec/example_spec.rb

100644100755
File mode changed.

spec/index_spec.rb

100644100755
File mode changed.

spec/rack_test_client_spec.rb

100644100755
File mode changed.

spec/rspec_api_documentation_spec.rb

100644100755
File mode changed.

spec/spec_helper.rb

100644100755
File mode changed.

spec/views/html_example_spec.rb

100644100755
File mode changed.

spec/writers/combined_text_example_spec.rb

100644100755
File mode changed.

spec/writers/html_writer_spec.rb

100644100755
File mode changed.

spec/writers/index_helper_spec.rb

100644100755
File mode changed.

spec/writers/json_iodocs_writer_spec.rb

100644100755
File mode changed.

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

spec/writers/json_writer_spec.rb

100644100755
File mode changed.

spec/writers/textile_writer_spec.rb

100644100755
File mode changed.

templates/rspec_api_documentation/html_example.mustache

100644100755
File mode changed.

templates/rspec_api_documentation/html_index.mustache

100644100755
File mode changed.

templates/rspec_api_documentation/textile_example.mustache

100644100755
File mode changed.

templates/rspec_api_documentation/textile_index.mustache

100644100755
File mode changed.

0 commit comments

Comments
 (0)