Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make API version to return it's value #248

Merged
merged 3 commits into from
Sep 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
12 changes: 7 additions & 5 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -291,15 +293,15 @@ 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

def before(&block)
imbue(:befores, [block])
end

def after_validation(&block)
imbue(:after_validations, [block])
end
Expand Down
22 changes: 20 additions & 2 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' }
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down