Skip to content

Commit

Permalink
Really simplistic version support.
Browse files Browse the repository at this point in the history
  • Loading branch information
idyll committed Aug 16, 2012
1 parent 15ab62e commit 832fb0a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def self.setup(options)
routes_array = routes.map do |route|
notes = route.route_notes && @@markdown ? Kramdown::Document.new(route.route_notes.strip_heredoc).to_html : route.route_notes
{
:path => parse_path(route.route_path),
:path => parse_path(route.route_path, api_version),
:operations => [{
:notes => notes,
:summary => route.route_description || '',
Expand Down Expand Up @@ -122,11 +122,13 @@ def parse_params(params, path, method)
end
end

def parse_path(path)
def parse_path(path, version)
# adapt format to swagger format
parsed_path = path.gsub('(.:format)', '.{format}')
# adapt params to swagger format
parsed_path.gsub(/:([a-z]+)/, '{\1}')
parsed_path = parsed_path.gsub(/:([a-z]+)/, '{\1}')
# add the version
parsed_path = parsed_path.gsub('{version}', version) if version
parsed_path
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/non_default_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,32 @@ def app; SimpleApiWithMarkdown end
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=>\"GET-something---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"
end
end

context "versioned API" do
before(:all) do
class VersionedMountedApi < Grape::API
prefix 'api'
version 'v1'

desc 'this gets something'
get '/something' do
{:bla => 'something'}
end
end

class SimpleApiWithVersion < Grape::API
mount VersionedMountedApi
add_swagger_documentation :api_version => "v1"
end
end

def app; SimpleApiWithVersion end

it "parses version and places it in the path" do
get '/swagger_doc/api'
last_response.body.should == "{:apiVersion=>\"v1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/api/v1/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>\"GET-api--version-something---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"
end
end


end

0 comments on commit 832fb0a

Please sign in to comment.