Skip to content

Commit 42aa73a

Browse files
committed
Merge branch 'api-version' of git://github.com/NARKOZ/grape
2 parents d7b2dd5 + ae5d155 commit 42aa73a

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

CHANGELOG.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
Next Release
22
============
33

4+
* [#248](https://github.com/intridea/grape/pull/248): Fix: API version returns version. - [@narkoz](https://github.com/narkoz).
45
* [#242](https://github.com/intridea/grape/issues/242): Fix: permanent redirect status. - [@adamgotterer](https://github.com/adamgotterer).
56
* [#236](https://github.com/intridea/grape/pull/236): Allow validation of nested parameters. - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
6-
* [#201](https://github.com/intridea/grape/pull/201): Added custom exceptions to Grape. Updated validations to use ValidationError that can be rescued. - [@adamgotterer](https://github.com/adamgotterer).
7+
* [#221](https://github.com/intridea/grape/pull/221): Added custom exceptions to Grape. Updated validations to use ValidationError that can be rescued. - [@adamgotterer](https://github.com/adamgotterer).
78
* [#211](https://github.com/intridea/grape/pull/211): Updates to validation and coercion: Fix #211 and force order of operations for presence and coercion - [@adamgotterer](https://github.com/adamgotterer).
89
* [#210](https://github.com/intridea/grape/pull/210): Fix: `Endpoint#body_params` causing undefined method 'size' - [@adamgotterer](https://github.com/adamgotterer).
910
* [#201](https://github.com/intridea/grape/pull/201): Rewritten `params` DSL, including support for coercion and validations - [@schmurfy](https://github.com/schmurfy).

lib/grape/api.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Grape
1010
# class in order to build an API.
1111
class API
1212
extend Validations::ClassMethods
13-
13+
1414
class << self
1515
attr_reader :route_set
1616
attr_reader :versions
@@ -28,7 +28,7 @@ def logger(logger = nil)
2828
@logger ||= Logger.new($stdout)
2929
end
3030
end
31-
31+
3232
def reset!
3333
@settings = Grape::Util::HashStack.new
3434
@route_set = Rack::Mount::RouteSet.new
@@ -106,6 +106,8 @@ def version(*args, &block)
106106
set(:version_options, options)
107107
end
108108
end
109+
110+
@versions.last unless @versions.nil?
109111
end
110112

111113
# Add a description to the next namespace or function.
@@ -124,7 +126,7 @@ def default_format(new_format = nil)
124126
def format(new_format = nil)
125127
new_format ? set(:format, new_format.to_sym) : settings[:format]
126128
end
127-
129+
128130
# Specify the format for error messages.
129131
# May be `:json` or `:txt` (default).
130132
def error_format(new_format = nil)
@@ -291,15 +293,15 @@ def route(methods, paths = ['/'], route_options = {}, &block)
291293
:route_options => (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {})
292294
}
293295
endpoints << Grape::Endpoint.new(settings.clone, endpoint_options, &block)
294-
296+
295297
@last_description = nil
296298
reset_validations!
297299
end
298300

299301
def before(&block)
300302
imbue(:befores, [block])
301303
end
302-
304+
303305
def after_validation(&block)
304306
imbue(:after_validations, [block])
305307
end

spec/grape/api_spec.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ def app; subject end
2121
end
2222
end
2323

24+
describe '.version' do
25+
context 'when defined' do
26+
it 'should return version value' do
27+
subject.version 'v1'
28+
subject.version.should == 'v1'
29+
end
30+
end
31+
32+
context 'when not defined' do
33+
it 'should return nil' do
34+
subject.version.should be_nil
35+
end
36+
end
37+
end
38+
2439
describe '.version using path' do
2540
it_should_behave_like 'versioning' do
2641
let(:macro_options) do
@@ -313,7 +328,7 @@ def app; subject end
313328
get '/'
314329
last_response.body.should eql 'first second'
315330
end
316-
331+
317332
it 'should add a after_validation filter' do
318333
subject.after_validation { @foo = "first #{params[:id]}:#{params[:id].class}" }
319334
subject.after_validation { @bar = 'second' }
@@ -687,7 +702,7 @@ def three
687702
it 'should not re-raise exceptions of type Grape::Exception::Base' do
688703
class CustomError < Grape::Exceptions::Base; end
689704
subject.get('/custom_exception'){ raise CustomError }
690-
705+
691706
lambda{ get '/custom_exception' }.should_not raise_error
692707
end
693708

@@ -911,6 +926,9 @@ class CommunicationError < RuntimeError; end
911926
end
912927
end
913928
end
929+
it "should return the latest version set" do
930+
subject.version.should == 'v2'
931+
end
914932
it "should return versions" do
915933
subject.versions.should == [ 'v1', 'v2' ]
916934
end

0 commit comments

Comments
 (0)