Skip to content

Commit 0fd1cf7

Browse files
committed
Fixed Ruby 1.8.7 specs.
1 parent 022cc38 commit 0fd1cf7

File tree

8 files changed

+135
-121
lines changed

8 files changed

+135
-121
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Next Release
22
============
33

4+
* [#201](https://github.com/intridea/grape/pull/201): Rewritten `params` DSL, including support for coercion and validations - [@schmurfy](https://github.com/schmurfy).
45
* [#181](https://github.com/intridea/grape/pull/181): Fix: Corrected JSON serialization of nested hashes containing `Grape::Entity` instances - [@benrosenblum](https://github.com/benrosenblum).
56
* [#203](https://github.com/intridea/grape/pull/203): Added a check to `Entity#serializable_hash` that verifies an entity exists on an object - [@adamgotterer](https://github.com/adamgotterer).
67

README.markdown

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Grape [![Build Status](http://travis-ci.org/intridea/grape.png?branch=frontier)](http://travis-ci.org/intridea/grape)
1+
# Grape [![Build Status](http://travis-ci.org/intridea/grape.png?branch=master)](http://travis-ci.org/intridea/grape)
22

33
## What is Grape?
44

@@ -13,6 +13,11 @@ versioning.
1313
* [Grape Google Group](http://groups.google.com/group/ruby-grape)
1414
* [Grape Wiki](https://github.com/intridea/grape/wiki)
1515

16+
## Stable Release
17+
18+
You're reading the documentation for the next release of Grape.
19+
The current stable release is [0.2.1](https://github.com/intridea/grape/blob/v0.2.1/README.markdown).
20+
1621
## Installation
1722

1823
Grape is available as a gem, to install it just install the gem:
@@ -192,27 +197,32 @@ post '/json_endpoint' do
192197
end
193198
```
194199

195-
## Validations
200+
## Parameter Validation and Coercion
196201

197-
You can define validations and coercion option for your attributes:
202+
You can define validations and coercion options for your parameters:
198203

199204
```ruby
200205
params do
201206
required :id, type: Integer
202207
optional :name, type: String, regexp: /^[a-z]+$/
203208
end
204-
205209
get ':id' do
206210
# params[:id] is an Integer
207211
end
208212
```
209213

210-
When a type is specified an implicit validation is done after the coercion to ensure the output type is what you asked.
211-
214+
When a type is specified an implicit validation is done after the coercion to ensure the output type is the one declared.
212215

213216
## Headers
214217

215-
Headers are available through the `env` hash object.
218+
Headers are available through the `header` helper or the `env` hash object.
219+
220+
```ruby
221+
get do
222+
content_type = header['Content-type']
223+
...
224+
end
225+
```
216226

217227
```ruby
218228
get do

lib/grape/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def logger(logger = nil)
2727
@logger ||= Logger.new($stdout)
2828
end
2929
end
30-
30+
3131
def reset!
3232
@settings = Grape::Util::HashStack.new
3333
@route_set = Rack::Mount::RouteSet.new

spec/grape/api_spec.rb

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -358,63 +358,65 @@ def app; subject end
358358
end
359359

360360
context 'custom middleware' do
361-
class PhonyMiddleware
362-
def initialize(app, *args)
363-
@args = args
364-
@app = app
365-
@block = true if block_given?
366-
end
361+
module ApiSpec
362+
class PhonyMiddleware
363+
def initialize(app, *args)
364+
@args = args
365+
@app = app
366+
@block = true if block_given?
367+
end
367368

368-
def call(env)
369-
env['phony.args'] ||= []
370-
env['phony.args'] << @args
371-
env['phony.block'] = true if @block
372-
@app.call(env)
369+
def call(env)
370+
env['phony.args'] ||= []
371+
env['phony.args'] << @args
372+
env['phony.block'] = true if @block
373+
@app.call(env)
374+
end
373375
end
374376
end
375377

376378
describe '.middleware' do
377379
it 'should include middleware arguments from settings' do
378380
settings = Grape::Util::HashStack.new
379-
settings.stub!(:stack).and_return([{:middleware => [[PhonyMiddleware, 'abc', 123]]}])
381+
settings.stub!(:stack).and_return([{:middleware => [[ApiSpec::PhonyMiddleware, 'abc', 123]]}])
380382
subject.stub!(:settings).and_return(settings)
381-
subject.middleware.should eql [[PhonyMiddleware, 'abc', 123]]
383+
subject.middleware.should eql [[ApiSpec::PhonyMiddleware, 'abc', 123]]
382384
end
383385

384386
it 'should include all middleware from stacked settings' do
385387
settings = Grape::Util::HashStack.new
386388
settings.stub!(:stack).and_return [
387-
{:middleware => [[PhonyMiddleware, 123],[PhonyMiddleware, 'abc']]},
388-
{:middleware => [[PhonyMiddleware, 'foo']]}
389+
{:middleware => [[ApiSpec::PhonyMiddleware, 123],[ApiSpec::PhonyMiddleware, 'abc']]},
390+
{:middleware => [[ApiSpec::PhonyMiddleware, 'foo']]}
389391
]
390392
subject.stub!(:settings).and_return(settings)
391393

392394
subject.middleware.should eql [
393-
[PhonyMiddleware, 123],
394-
[PhonyMiddleware, 'abc'],
395-
[PhonyMiddleware, 'foo']
395+
[ApiSpec::PhonyMiddleware, 123],
396+
[ApiSpec::PhonyMiddleware, 'abc'],
397+
[ApiSpec::PhonyMiddleware, 'foo']
396398
]
397399
end
398400
end
399401

400402
describe '.use' do
401403
it 'should add middleware' do
402-
subject.use PhonyMiddleware, 123
403-
subject.middleware.should eql [[PhonyMiddleware, 123]]
404+
subject.use ApiSpec::PhonyMiddleware, 123
405+
subject.middleware.should eql [[ApiSpec::PhonyMiddleware, 123]]
404406
end
405407

406408
it 'should not show up outside the namespace' do
407-
subject.use PhonyMiddleware, 123
409+
subject.use ApiSpec::PhonyMiddleware, 123
408410
subject.namespace :awesome do
409-
use PhonyMiddleware, 'abc'
410-
middleware.should == [[PhonyMiddleware, 123],[PhonyMiddleware, 'abc']]
411+
use ApiSpec::PhonyMiddleware, 'abc'
412+
middleware.should == [[ApiSpec::PhonyMiddleware, 123],[ApiSpec::PhonyMiddleware, 'abc']]
411413
end
412414

413-
subject.middleware.should eql [[PhonyMiddleware, 123]]
415+
subject.middleware.should eql [[ApiSpec::PhonyMiddleware, 123]]
414416
end
415417

416418
it 'should actually call the middleware' do
417-
subject.use PhonyMiddleware, 'hello'
419+
subject.use ApiSpec::PhonyMiddleware, 'hello'
418420
subject.get '/' do
419421
env['phony.args'].first.first
420422
end
@@ -425,13 +427,13 @@ def call(env)
425427

426428
it 'should add a block if one is given' do
427429
block = lambda{ }
428-
subject.use PhonyMiddleware, &block
429-
subject.middleware.should eql [[PhonyMiddleware, block]]
430+
subject.use ApiSpec::PhonyMiddleware, &block
431+
subject.middleware.should eql [[ApiSpec::PhonyMiddleware, block]]
430432
end
431433

432434
it 'should use a block if one is given' do
433435
block = lambda{ }
434-
subject.use PhonyMiddleware, &block
436+
subject.use ApiSpec::PhonyMiddleware, &block
435437
subject.get '/' do
436438
env['phony.block'].inspect
437439
end
@@ -442,7 +444,7 @@ def call(env)
442444

443445
it 'should not destroy the middleware settings on multiple runs' do
444446
block = lambda{ }
445-
subject.use PhonyMiddleware, &block
447+
subject.use ApiSpec::PhonyMiddleware, &block
446448
subject.get '/' do
447449
env['phony.block'].inspect
448450
end

spec/grape/validations/coerce_spec.rb

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,53 @@
11
require 'spec_helper'
22

3-
require 'virtus'
4-
5-
module CoerceTest
6-
class User
7-
include Virtus
3+
describe Grape::Validations::CoerceValidator do
4+
module ValidationsSpec
5+
module CoerceValidatorSpec
6+
class User
7+
include Virtus
8+
attribute :id, Integer
9+
attribute :name, String
10+
end
811

9-
attribute :id, Integer
10-
attribute :name, String
11-
end
12-
13-
class API < Grape::API
14-
default_format :json
12+
class API < Grape::API
13+
default_format :json
1514

15+
params do
16+
requires :int, :coerce => Integer
17+
end
18+
get '/single' do
19+
end
1620

17-
params do
18-
requires :int, :coerce => Integer
19-
end
20-
get '/single' do
21-
end
22-
23-
params do
24-
requires :ids, type: Array[Integer]
25-
end
26-
get '/arr' do
27-
end
21+
params do
22+
requires :ids, :type => Array[Integer]
23+
end
24+
get '/arr' do
25+
end
2826

29-
params do
30-
requires :user, type: CoerceTest::User
31-
end
32-
get '/user' do
33-
end
27+
params do
28+
requires :user, :type => ValidationsSpec::CoerceValidatorSpec::User
29+
end
30+
get '/user' do
31+
end
3432

35-
params do
36-
requires :int, :coerce => Integer
37-
optional :arr, :coerce => Array[Integer]
38-
optional :bool, :coerce => Array[Boolean]
39-
end
40-
get '/coerce' do
41-
{
42-
:int => params[:int].class,
43-
:arr => params[:arr] ? params[:arr][0].class : nil,
44-
:bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil
45-
}
33+
params do
34+
requires :int, :coerce => Integer
35+
optional :arr, :coerce => Array[Integer]
36+
optional :bool, :coerce => Array[Boolean]
37+
end
38+
get '/coerce' do
39+
{
40+
:int => params[:int].class,
41+
:arr => params[:arr] ? params[:arr][0].class : nil,
42+
:bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil
43+
}
44+
end
45+
end
4646
end
4747
end
48-
end
49-
50-
describe Grape::Validations::CoerceValidator do
48+
5149
def app
52-
CoerceTest::API
50+
ValidationsSpec::CoerceValidatorSpec::API
5351
end
5452

5553
it "should return an error on malformed input" do
@@ -73,7 +71,7 @@ def app
7371
get '/user', :user => "32"
7472
last_response.status.should == 400
7573

76-
get '/user', :user => {id: 32, name: "Bob"}
74+
get '/user', :user => { :id => 32, :name => "Bob"}
7775
last_response.status.should == 200
7876
end
7977

spec/grape/validations/presence_spec.rb

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
require 'spec_helper'
22

33
describe Grape::Validations::PresenceValidator do
4-
def app; @app; end
5-
6-
before do
7-
@app = Class.new(Grape::API) do
8-
default_format :json
9-
10-
params do
11-
requires :id, :regexp => /^[0-9]+$/
12-
end
13-
14-
post do
15-
{:ret => params[:id]}
16-
end
17-
18-
params do
19-
requires :name, :company
20-
end
21-
22-
get do
23-
"Hello"
4+
5+
module ValidationsSpec
6+
module PresenceValidatorSpec
7+
class API < Grape::API
8+
default_format :json
9+
10+
params do
11+
requires :id, :regexp => /^[0-9]+$/
12+
end
13+
post do
14+
{:ret => params[:id]}
15+
end
16+
17+
params do
18+
requires :name, :company
19+
end
20+
get do
21+
"Hello"
22+
end
2423
end
25-
2624
end
27-
25+
end
26+
27+
def app
28+
ValidationsSpec::PresenceValidatorSpec::API
2829
end
2930

3031
it 'validates id' do

spec/grape/validations/regexp_spec.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
require 'spec_helper'
22

33
describe Grape::Validations::RegexpValidator do
4-
def app; @app; end
5-
6-
before do
7-
@app = Class.new(Grape::API) do
8-
default_format :json
9-
10-
params do
11-
requires :name, :regexp => /^[a-z]+$/
12-
end
13-
get do
4+
module ValidationsSpec
5+
module RegexpValidatorSpec
6+
class API < Grape::API
7+
default_format :json
148

9+
params do
10+
requires :name, :regexp => /^[a-z]+$/
11+
end
12+
get do
13+
14+
end
1515
end
16-
1716
end
18-
17+
end
18+
19+
def app
20+
ValidationsSpec::RegexpValidatorSpec::API
1921
end
2022

2123
it 'should refuse invalid input' do

0 commit comments

Comments
 (0)