Skip to content

Commit a1c329a

Browse files
Update spec for markdown.
1 parent f51d4d9 commit a1c329a

File tree

7 files changed

+122
-135
lines changed

7 files changed

+122
-135
lines changed

Gemfile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ group :development do
1313
gem "rdoc", "~> 3.12"
1414
gem "bundler", "> 1.0.0"
1515
gem "jeweler", "~> 1.8.4"
16-
# jquery-rails is used by the dummy application
17-
gem "jquery-rails"
18-
gem "rails", "~> 3.2"
19-
gem "sqlite3"
16+
17+
gem "pry"
2018

2119
gem "rack-test"
2220

23-
gem "rspec-rails"
2421
gem "rspec"
2522
end

Gemfile.lock

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
GEM
22
remote: http://rubygems.org/
33
specs:
4-
actionmailer (3.2.6)
5-
actionpack (= 3.2.6)
6-
mail (~> 2.4.4)
7-
actionpack (3.2.6)
8-
activemodel (= 3.2.6)
9-
activesupport (= 3.2.6)
10-
builder (~> 3.0.0)
11-
erubis (~> 2.7.0)
12-
journey (~> 1.0.1)
13-
rack (~> 1.4.0)
14-
rack-cache (~> 1.2)
15-
rack-test (~> 0.6.1)
16-
sprockets (~> 2.1.3)
17-
activemodel (3.2.6)
18-
activesupport (= 3.2.6)
19-
builder (~> 3.0.0)
20-
activerecord (3.2.6)
21-
activemodel (= 3.2.6)
22-
activesupport (= 3.2.6)
23-
arel (~> 3.0.2)
24-
tzinfo (~> 0.3.29)
25-
activeresource (3.2.6)
26-
activemodel (= 3.2.6)
27-
activesupport (= 3.2.6)
284
activesupport (3.2.6)
295
i18n (~> 0.6)
306
multi_json (~> 1.0)
31-
arel (3.0.2)
32-
builder (3.0.0)
7+
coderay (1.0.7)
338
diff-lcs (1.1.3)
34-
erubis (2.7.0)
359
git (1.2.5)
3610
grape (0.2.1)
3711
hashie (~> 1.2)
@@ -40,51 +14,26 @@ GEM
4014
rack
4115
rack-mount
4216
hashie (1.2.0)
43-
hike (1.2.1)
4417
i18n (0.6.0)
4518
jeweler (1.8.4)
4619
bundler (~> 1.0)
4720
git (>= 1.2.5)
4821
rake
4922
rdoc
50-
journey (1.0.4)
51-
jquery-rails (2.0.2)
52-
railties (>= 3.2.0, < 5.0)
53-
thor (~> 0.14)
5423
json (1.7.3)
5524
kramdown (0.13.7)
56-
mail (2.4.4)
57-
i18n (>= 0.4.0)
58-
mime-types (~> 1.16)
59-
treetop (~> 1.4.8)
60-
mime-types (1.19)
25+
method_source (0.8)
6126
multi_json (1.3.6)
6227
multi_xml (0.5.1)
63-
polyglot (0.3.3)
28+
pry (0.9.10)
29+
coderay (~> 1.0.5)
30+
method_source (~> 0.8)
31+
slop (~> 3.3.1)
6432
rack (1.4.1)
65-
rack-cache (1.2)
66-
rack (>= 0.4)
6733
rack-mount (0.8.3)
6834
rack (>= 1.0.0)
69-
rack-ssl (1.3.2)
70-
rack
7135
rack-test (0.6.1)
7236
rack (>= 1.0)
73-
rails (3.2.6)
74-
actionmailer (= 3.2.6)
75-
actionpack (= 3.2.6)
76-
activerecord (= 3.2.6)
77-
activeresource (= 3.2.6)
78-
activesupport (= 3.2.6)
79-
bundler (~> 1.0)
80-
railties (= 3.2.6)
81-
railties (3.2.6)
82-
actionpack (= 3.2.6)
83-
activesupport (= 3.2.6)
84-
rack-ssl (~> 1.3.2)
85-
rake (>= 0.8.7)
86-
rdoc (~> 3.4)
87-
thor (>= 0.14.6, < 2.0)
8837
rake (0.9.2.2)
8938
rdoc (3.12)
9039
json (~> 1.4)
@@ -96,28 +45,13 @@ GEM
9645
rspec-expectations (2.11.1)
9746
diff-lcs (~> 1.1.3)
9847
rspec-mocks (2.11.1)
99-
rspec-rails (2.11.0)
100-
actionpack (>= 3.0)
101-
activesupport (>= 3.0)
102-
railties (>= 3.0)
103-
rspec (~> 2.11.0)
10448
shoulda (3.1.1)
10549
shoulda-context (~> 1.0)
10650
shoulda-matchers (~> 1.2)
10751
shoulda-context (1.0.0)
10852
shoulda-matchers (1.2.0)
10953
activesupport (>= 3.0.0)
110-
sprockets (2.1.3)
111-
hike (~> 1.2)
112-
rack (~> 1.0)
113-
tilt (~> 1.1, != 1.3.0)
114-
sqlite3 (1.3.6)
115-
thor (0.15.4)
116-
tilt (1.3.3)
117-
treetop (1.4.10)
118-
polyglot
119-
polyglot (>= 0.3.1)
120-
tzinfo (0.3.33)
54+
slop (3.3.2)
12155

12256
PLATFORMS
12357
ruby
@@ -126,12 +60,9 @@ DEPENDENCIES
12660
bundler (> 1.0.0)
12761
grape
12862
jeweler (~> 1.8.4)
129-
jquery-rails
13063
kramdown
64+
pry
13165
rack-test
132-
rails (~> 3.2)
13366
rdoc (~> 3.12)
13467
rspec
135-
rspec-rails
13668
shoulda
137-
sqlite3

README.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ You can pass a hash with some configuration possibilities to ```add_swagger_docu
4040
* ```:mount_path``` The path were the API documentation is loaded, default '/swagger_doc'
4141
* ```:api_version``` Version of the API that's being exposed
4242
* ```:base_path``` Basepath of the API that's being exposed
43+
* ```:markdown``` Allow markdown in `notes`, default `false`
4344

4445
## Swagger additions
4546
grape-swagger allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Swagger UI. The default Swagger UI doesn't allow HTML in the notes field, so you need to use an adapted version of Swagger UI (you can find one at https://github.com/tim-vandecasteele/swagger-ui/tree/vasco).
4647

4748
We're using [kramdown](http://kramdown.rubyforge.org) for parsing the markdown, specific syntax can be found [here](http://kramdown.rubyforge.org/syntax.html).
4849

50+
Be sure to enable markdown in the `add_swagger_documentation` call: ':markdown => true'
4951

5052
``` ruby
5153
desc "Reserve a virgin in heaven", {

lib/grape-swagger.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ def self.setup(options)
4141
:mount_path => '/swagger_doc',
4242
:base_path => nil,
4343
:api_version => '0.1',
44+
:markdown => false
4445
}
4546
options = defaults.merge(options)
4647

4748
@@target_class = options[:target_class]
4849
@@mount_path = options[:mount_path]
4950
@@class_name = options[:class_name] || options[:mount_path].gsub('/','')
51+
@@markdown = options[:markdown]
5052
api_version = options[:api_version]
5153
base_path = options[:base_path]
5254

@@ -77,7 +79,7 @@ def self.setup(options)
7779
header['Access-Control-Request-Method'] = '*'
7880
routes = @@target_class::combined_routes[params[:name]]
7981
routes_array = routes.map do |route|
80-
notes = route.route_notes ? Kramdown::Document.new(route.route_notes.strip_heredoc).to_html : nil
82+
notes = route.route_notes && @@markdown ? Kramdown::Document.new(route.route_notes.strip_heredoc).to_html : route.route_notes
8183
{
8284
:path => parse_path(route.route_path),
8385
:operations => [{
@@ -133,6 +135,16 @@ def parse_path(path)
133135
end
134136
end
135137

138+
class Object
139+
##
140+
# @person ? @person.name : nil
141+
# vs
142+
# @person.try(:name)
143+
def try(method)
144+
send method if respond_to? method
145+
end
146+
end
147+
136148
class String
137149
# strip_heredoc from rails
138150
# File activesupport/lib/active_support/core_ext/string/strip.rb, line 22

spec/non_default_api_spec.rb

Lines changed: 78 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
require 'spec_helper'
22

3-
describe "overruling the defaults for the api documentation generation" do
4-
5-
class MountedApi < Grape::API
6-
desc 'this gets something'
7-
get '/something' do
8-
{:bla => 'something'}
9-
end
10-
end
11-
12-
3+
describe "options: " do
134
context "overruling the basepath" do
14-
class SimpleApiWithBasePath < Grape::API
15-
NON_DEFAULT_BASE_PATH= "http://www.breakcoregivesmewood.com"
16-
17-
mount MountedApi
18-
add_swagger_documentation :base_path => NON_DEFAULT_BASE_PATH
5+
before(:all) do
6+
class BasePathMountedApi < Grape::API
7+
desc 'this gets something'
8+
get '/something' do
9+
{:bla => 'something'}
10+
end
11+
end
12+
13+
class SimpleApiWithBasePath < Grape::API
14+
NON_DEFAULT_BASE_PATH= "http://www.breakcoregivesmewood.com"
15+
16+
mount BasePathMountedApi
17+
add_swagger_documentation :base_path => NON_DEFAULT_BASE_PATH
18+
end
1919
end
2020

21-
subject { SimpleApiWithBasePath.new }
22-
def app; subject end
21+
def app; SimpleApiWithBasePath end
2322

2423
it "retrieves the given base-path on /swagger_doc" do
2524
get '/swagger_doc'
@@ -33,43 +32,59 @@ def app; subject end
3332
end
3433
end
3534

36-
context "overruling the basepath" do
37-
class SimpleApiWithAPiVersion < Grape::API
38-
API_VERSION = "101"
39-
40-
mount MountedApi
41-
add_swagger_documentation :api_version => API_VERSION
35+
context "overruling the version" do
36+
before(:all) do
37+
class ApiVersionMountedApi < Grape::API
38+
desc 'this gets something'
39+
get '/something' do
40+
{:bla => 'something'}
41+
end
42+
end
43+
44+
class SimpleApiWithApiVersion < Grape::API
45+
API_VERSION = "101"
46+
47+
mount ApiVersionMountedApi
48+
add_swagger_documentation :api_version => API_VERSION
49+
end
4250
end
4351

44-
subject { SimpleApiWithAPiVersion.new }
45-
def app; subject end
52+
def app; SimpleApiWithApiVersion end
4653

47-
it "retrieves the given base-path on /swagger_doc" do
54+
it "retrieves the api version on /swagger_doc" do
4855
get '/swagger_doc'
49-
last_response.body.should == "{:apiVersion=>\"#{SimpleApiWithAPiVersion::API_VERSION}\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/something.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
56+
last_response.body.should == "{:apiVersion=>\"#{SimpleApiWithApiVersion::API_VERSION}\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/something.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
5057
end
5158

52-
it "retrieves the same given base-path for mounted-api" do
59+
it "retrieves the same api version for mounted-api" do
5360
Random.stub(:rand) { 0 }
5461
get '/swagger_doc/something'
55-
last_response.body.should == "{:apiVersion=>\"#{SimpleApiWithAPiVersion::API_VERSION}\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
62+
last_response.body.should == "{:apiVersion=>\"#{SimpleApiWithApiVersion::API_VERSION}\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
5663
end
5764
end
5865

5966
context "overruling the mount-path" do
60-
class SimpleApiWithDifferentMount < Grape::API
61-
MOUNT_PATH = "api_doc"
62-
63-
mount MountedApi
64-
add_swagger_documentation :mount_path => MOUNT_PATH
67+
before(:all) do
68+
class DifferentMountMountedApi < Grape::API
69+
desc 'this gets something'
70+
get '/something' do
71+
{:bla => 'something'}
72+
end
73+
end
74+
75+
class SimpleApiWithDifferentMount < Grape::API
76+
MOUNT_PATH = "/api_doc"
77+
78+
mount DifferentMountMountedApi
79+
add_swagger_documentation :mount_path => MOUNT_PATH
80+
end
6581
end
6682

67-
subject { SimpleApiWithDifferentMount.new }
68-
def app; subject end
83+
def app; SimpleApiWithDifferentMount end
6984

70-
it "retrieves the given base-path on /swagger_doc" do
85+
it "retrieves the given base-path on /api_doc" do
7186
get '/api_doc'
72-
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/something.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
87+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/api_doc/something.{format}\"}, {:path=>\"/api_doc/api_doc.{format}\"}]}"
7388
end
7489

7590
it "retrieves the same given base-path for mounted-api" do
@@ -84,5 +99,30 @@ def app; subject end
8499
end
85100
end
86101

102+
context "overruling the markdown" do
103+
before(:all) do
104+
class MarkDownMountedApi < Grape::API
105+
desc 'this gets something', {
106+
:notes => '_test_'
107+
}
108+
get '/something' do
109+
{:bla => 'something'}
110+
end
111+
end
112+
113+
class SimpleApiWithMarkdown < Grape::API
114+
mount MarkDownMountedApi
115+
add_swagger_documentation :markdown => true
116+
end
117+
end
118+
119+
def app; SimpleApiWithMarkdown end
120+
121+
it "parses markdown for a mounted-api" do
122+
Random.stub(:rand) { 0 }
123+
get '/swagger_doc/something'
124+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>\"<p><em>test</em></p>\\n\", :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
125+
end
126+
end
87127

88128
end

0 commit comments

Comments
 (0)