From 354439a754b44f9197b245443f812e57c8d412fa Mon Sep 17 00:00:00 2001 From: dblock Date: Sat, 9 Feb 2013 16:27:58 -0500 Subject: [PATCH] Fix #328: Version can now be specified as both String and Symbol. --- CHANGELOG.markdown | 1 + lib/grape/middleware/versioner/param.rb | 2 +- lib/grape/middleware/versioner/path.rb | 2 +- spec/grape/endpoint_spec.rb | 14 ++++---- .../grape/middleware/versioner/header_spec.rb | 32 ++++++++++--------- spec/grape/middleware/versioner/path_spec.rb | 20 +++++++----- 6 files changed, 40 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 8b1afbc8e3..c249028e0c 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -8,6 +8,7 @@ Next Release * A formatter that raises an exception will cause the API to return a 500 error - [@dblock](http://github.com/dblock). * [#131](https://github.com/intridea/grape/issues/131): Added instructions for Grape API reloading in Rails - [@jyn](http://github.com/jyn), [@dblock](http://github.com/dblock). * [#317](https://github.com/intridea/grape/issues/317): Added `headers` that returns a hash of parsed HTTP request headers - [@dblock](http://github.com/dblock). +* [#328](https://github.com/intridea/grape/issues/328): Version can now be specified as both String and Symbol - [@dblock](http://github.com/dblock). * Your contribution here. 0.2.6 (01/11/2013) diff --git a/lib/grape/middleware/versioner/param.rb b/lib/grape/middleware/versioner/param.rb index eb8f0d3570..9254e38faa 100644 --- a/lib/grape/middleware/versioner/param.rb +++ b/lib/grape/middleware/versioner/param.rb @@ -30,7 +30,7 @@ def before potential_version = request.params[paramkey] unless potential_version.nil? - if options[:versions] && !options[:versions].include?(potential_version) + if options[:versions] && ! options[:versions].find { |v| v.to_s == potential_version } throw :error, :status => 404, :message => "404 API Version Not Found", :headers => {'X-Cascade' => 'pass'} end env['api.version'] = potential_version diff --git a/lib/grape/middleware/versioner/path.rb b/lib/grape/middleware/versioner/path.rb index a5739ae70f..1780cc662d 100644 --- a/lib/grape/middleware/versioner/path.rb +++ b/lib/grape/middleware/versioner/path.rb @@ -34,7 +34,7 @@ def before pieces = path.split('/') potential_version = pieces[1] if potential_version =~ options[:pattern] - if options[:versions] && !options[:versions].include?(potential_version) + if options[:versions] && ! options[:versions].find { |v| v.to_s == potential_version } throw :error, :status => 404, :message => "404 API Version Not Found" end diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 8b713f59e2..16a41ea15b 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -513,13 +513,15 @@ def memoized get '/url' last_response.body.should == "http://example.org/url" end - it 'should include version' do - subject.version 'v1', :using => :path - subject.get('/url') do - request.url + [ 'v1', :v1 ].each do |version| + it 'should include version #{version}' do + subject.version version, :using => :path + subject.get('/url') do + request.url + end + get "/#{version}/url" + last_response.body.should == "http://example.org/#{version}/url" end - get '/v1/url' - last_response.body.should == "http://example.org/v1/url" end it 'should include prefix' do subject.version 'v1', :using => :path diff --git a/spec/grape/middleware/versioner/header_spec.rb b/spec/grape/middleware/versioner/header_spec.rb index 7b5c016f31..8cdb35088d 100644 --- a/spec/grape/middleware/versioner/header_spec.rb +++ b/spec/grape/middleware/versioner/header_spec.rb @@ -49,21 +49,23 @@ status.should == 200 end - context 'when version is set' do - before do - @options[:versions] = ['v1'] - end - - it 'is set' do - status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json') - env['api.format'].should eql 'json' - status.should == 200 - end - - it 'is nil if not provided' do - status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1') - env['api.format'].should eql nil - status.should == 200 + [ 'v1', :v1 ].each do |version| + context 'when version is set to #{version{' do + before do + @options[:versions] = [ version ] + end + + it 'is set' do + status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json') + env['api.format'].should eql 'json' + status.should == 200 + end + + it 'is nil if not provided' do + status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1') + env['api.format'].should eql nil + status.should == 200 + end end end end diff --git a/spec/grape/middleware/versioner/path_spec.rb b/spec/grape/middleware/versioner/path_spec.rb index 222a418866..4361cb0dd9 100644 --- a/spec/grape/middleware/versioner/path_spec.rb +++ b/spec/grape/middleware/versioner/path_spec.rb @@ -17,7 +17,7 @@ end context 'with a pattern' do - before{ @options = {:pattern => /v./i} } + before { @options = {:pattern => /v./i} } it 'sets the version if it matches' do subject.call('PATH_INFO' => '/v1/awesome').last.should == 'v1' end @@ -27,14 +27,18 @@ end end - context 'with specified versions' do - before{ @options = {:versions => ['v1', 'v2']}} - it 'throws an error if a non-allowed version is specified' do - catch(:error){subject.call('PATH_INFO' => '/v3/awesome')}[:status].should == 404 - end + [ [ 'v1', 'v2'], [ :v1, :v2 ], [ :v1, 'v2' ], [ 'v1', :v2 ] ].each do |versions| + context 'with specified versions as #{versions}' do + before { @options = { :versions => versions } } + + it 'throws an error if a non-allowed version is specified' do + catch(:error){subject.call('PATH_INFO' => '/v3/awesome')}[:status].should == 404 + end - it 'allows versions that have been specified' do - subject.call('PATH_INFO' => '/v1/asoasd').last.should == 'v1' + it 'allows versions that have been specified' do + subject.call('PATH_INFO' => '/v1/asoasd').last.should == 'v1' + end end end + end \ No newline at end of file