diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index d60c2b1819..53fc9188b5 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -1,9 +1,10 @@ Next Release ============ +* [#248](https://github.com/intridea/grape/pull/248): Fix: API version returns version. - [@narkoz](https://github.com/narkoz). * [#242](https://github.com/intridea/grape/issues/242): Fix: permanent redirect status. - [@adamgotterer](https://github.com/adamgotterer). * [#236](https://github.com/intridea/grape/pull/236): Allow validation of nested parameters. - [@tim-vandecasteele](https://github.com/tim-vandecasteele). -* [#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). +* [#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). * [#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). * [#210](https://github.com/intridea/grape/pull/210): Fix: `Endpoint#body_params` causing undefined method 'size' - [@adamgotterer](https://github.com/adamgotterer). * [#201](https://github.com/intridea/grape/pull/201): Rewritten `params` DSL, including support for coercion and validations - [@schmurfy](https://github.com/schmurfy). diff --git a/lib/grape/api.rb b/lib/grape/api.rb index dbd373b9e8..2b7a5e501a 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -10,7 +10,7 @@ module Grape # class in order to build an API. class API extend Validations::ClassMethods - + class << self attr_reader :route_set attr_reader :versions @@ -28,7 +28,7 @@ def logger(logger = nil) @logger ||= Logger.new($stdout) end end - + def reset! @settings = Grape::Util::HashStack.new @route_set = Rack::Mount::RouteSet.new @@ -106,6 +106,8 @@ def version(*args, &block) set(:version_options, options) end end + + @versions.last unless @versions.nil? end # Add a description to the next namespace or function. @@ -124,7 +126,7 @@ def default_format(new_format = nil) def format(new_format = nil) new_format ? set(:format, new_format.to_sym) : settings[:format] end - + # Specify the format for error messages. # May be `:json` or `:txt` (default). def error_format(new_format = nil) @@ -291,7 +293,7 @@ def route(methods, paths = ['/'], route_options = {}, &block) :route_options => (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {}) } endpoints << Grape::Endpoint.new(settings.clone, endpoint_options, &block) - + @last_description = nil reset_validations! end @@ -299,7 +301,7 @@ def route(methods, paths = ['/'], route_options = {}, &block) def before(&block) imbue(:befores, [block]) end - + def after_validation(&block) imbue(:after_validations, [block]) end diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 2ab1860d01..03aecedb1f 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -21,6 +21,21 @@ def app; subject end end end + describe '.version' do + context 'when defined' do + it 'should return version value' do + subject.version 'v1' + subject.version.should == 'v1' + end + end + + context 'when not defined' do + it 'should return nil' do + subject.version.should be_nil + end + end + end + describe '.version using path' do it_should_behave_like 'versioning' do let(:macro_options) do @@ -313,7 +328,7 @@ def app; subject end get '/' last_response.body.should eql 'first second' end - + it 'should add a after_validation filter' do subject.after_validation { @foo = "first #{params[:id]}:#{params[:id].class}" } subject.after_validation { @bar = 'second' } @@ -687,7 +702,7 @@ def three it 'should not re-raise exceptions of type Grape::Exception::Base' do class CustomError < Grape::Exceptions::Base; end subject.get('/custom_exception'){ raise CustomError } - + lambda{ get '/custom_exception' }.should_not raise_error end @@ -911,6 +926,9 @@ class CommunicationError < RuntimeError; end end end end + it "should return the latest version set" do + subject.version.should == 'v2' + end it "should return versions" do subject.versions.should == [ 'v1', 'v2' ] end