Skip to content

Commit c4d0f6c

Browse files
committed
Allow rbx failures and add array usage specs
As per feedback in PR I had to move things around a little bit in the example representers because using the request url as the self rel only works if the resource will always be presented by itself It's still a useful example to have to demonstrate how using `env` works though, so I made sure to move it into a resource that fits that criteria.
1 parent ce70c06 commit c4d0f6c

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ rvm:
1111
- 1.9.3
1212
- jruby-19mode
1313
- rbx-2.2.10
14+
15+
matrix:
16+
allow_failures:
17+
- rvm: rbx-2.2.10

spec/decorator_spec.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,30 @@ def app
1515
end
1616

1717
context 'decorator' do
18-
before do
19-
subject.get('/user/:id') do
20-
present User.new(name: 'Lonestar', id: params[:id]), with: UserRepresenter
18+
context 'with a single resource' do
19+
before do
20+
subject.get('/user/:id') do
21+
present User.new(name: 'Lonestar', id: params[:id]), with: UserRepresenter
22+
end
23+
end
24+
25+
it 'returns a single hypermedia representation' do
26+
get '/user/666'
27+
expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}'
2128
end
2229
end
2330

24-
it 'returns a hypermedia representation' do
25-
get '/user/666'
26-
expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}'
31+
context 'with an array of resources' do
32+
before do
33+
subject.get('/users') do
34+
present [User.new(name: 'Texassee', id: 1), User.new(name: 'Lonestar', id: 2)], with: UserRepresenter
35+
end
36+
end
37+
38+
it 'returns an array of hypermedia representations' do
39+
get 'users'
40+
expect(last_response.body).to eq '[{"name":"Texassee","id":1,"links":[{"rel":"self","href":"/user/1"}]},{"name":"Lonestar","id":2,"links":[{"rel":"self","href":"/user/2"}]}]'
41+
end
2742
end
2843
end
2944
end

spec/nested_representer_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def app
2626

2727
it 'returns a hypermedia representation' do
2828
get '/order/666'
29-
expect(last_response.body).to eq '{"id":"666","client_id":42,"articles":[{"title":"One","id":1,"links":[{"rel":"self","href":"/article/1"}]},{"title":"Two","id":2,"links":[{"rel":"self","href":"/article/2"}]}],"links":[{"rel":"self","href":"/order/666"},{"rel":"items","href":"/order/666/items"}]}'
29+
expect(last_response.body).to eq '{"id":"666","client_id":42,"articles":[{"title":"One","id":1,"links":[{"rel":"self","href":"/article/1"}]},{"title":"Two","id":2,"links":[{"rel":"self","href":"/article/2"}]}],' \
30+
'"links":[{"rel":"self","href":"http://example.org/order/666"},{"rel":"items","href":"/order/666/items"}]}'
3031
end
3132
end
3233
end

spec/present_with_spec.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,30 @@ def app
1515
end
1616

1717
context 'using present' do
18-
before do
19-
subject.get('/product/:id') do
20-
present Product.new(title: 'Lonestar', id: params[:id]), with: ProductRepresenter
18+
context 'with a single resource' do
19+
before do
20+
subject.get('/product/:id') do
21+
present Product.new(title: 'Lonestar', id: params[:id]), with: ProductRepresenter
22+
end
23+
end
24+
25+
it 'returns a hypermedia representation' do
26+
get '/product/666'
27+
expect(last_response.body).to eq '{"title":"Lonestar","id":"666","links":[{"rel":"self","href":"/product/666"}]}'
2128
end
2229
end
2330

24-
it 'returns a hypermedia representation' do
25-
get '/product/666'
26-
expect(last_response.body).to eq '{"title":"Lonestar","id":"666","links":[{"rel":"self","href":"http://example.org/product/666"}]}'
31+
context 'with an array of resources' do
32+
before do
33+
subject.get('/products') do
34+
present [Product.new(title: 'Texassee', id: 1), Product.new(title: 'Lonestar', id: 2)], with: ProductRepresenter
35+
end
36+
end
37+
38+
it 'returns an array of hypermedia representations' do
39+
get 'products'
40+
expect(last_response.body).to eq '[{"title":"Texassee","id":1,"links":[{"rel":"self","href":"/product/1"}]},{"title":"Lonestar","id":2,"links":[{"rel":"self","href":"/product/2"}]}]'
41+
end
2742
end
2843
end
2944
end

spec/support/order_representer.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ module OrderRepresenter
99

1010
collection :articles, class: Article
1111

12-
link :self do
13-
"/order/#{id}"
12+
link :self do |opts|
13+
request = Grape::Request.new(opts[:env])
14+
"#{request.url}"
1415
end
1516

1617
link :items do

spec/support/product_representer.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ module ProductRepresenter
88
property :title
99
property :id
1010

11-
link :self do |opts|
12-
request = Grape::Request.new(opts[:env])
13-
"#{request.url}"
11+
link :self do
12+
"/product/#{id}"
1413
end
1514
end

0 commit comments

Comments
 (0)