Skip to content

Commit

Permalink
Allow rbx failures and add array usage specs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jcwilk committed Jun 9, 2016
1 parent ce70c06 commit c4d0f6c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ rvm:
- 1.9.3
- jruby-19mode
- rbx-2.2.10

matrix:
allow_failures:
- rvm: rbx-2.2.10
27 changes: 21 additions & 6 deletions spec/decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@ def app
end

context 'decorator' do
before do
subject.get('/user/:id') do
present User.new(name: 'Lonestar', id: params[:id]), with: UserRepresenter
context 'with a single resource' do
before do
subject.get('/user/:id') do
present User.new(name: 'Lonestar', id: params[:id]), with: UserRepresenter
end
end

it 'returns a single hypermedia representation' do
get '/user/666'
expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}'
end
end

it 'returns a hypermedia representation' do
get '/user/666'
expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}'
context 'with an array of resources' do
before do
subject.get('/users') do
present [User.new(name: 'Texassee', id: 1), User.new(name: 'Lonestar', id: 2)], with: UserRepresenter
end
end

it 'returns an array of hypermedia representations' do
get 'users'
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"}]}]'
end
end
end
end
3 changes: 2 additions & 1 deletion spec/nested_representer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def app

it 'returns a hypermedia representation' do
get '/order/666'
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"}]}'
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":"http://example.org/order/666"},{"rel":"items","href":"/order/666/items"}]}'
end
end
end
27 changes: 21 additions & 6 deletions spec/present_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@ def app
end

context 'using present' do
before do
subject.get('/product/:id') do
present Product.new(title: 'Lonestar', id: params[:id]), with: ProductRepresenter
context 'with a single resource' do
before do
subject.get('/product/:id') do
present Product.new(title: 'Lonestar', id: params[:id]), with: ProductRepresenter
end
end

it 'returns a hypermedia representation' do
get '/product/666'
expect(last_response.body).to eq '{"title":"Lonestar","id":"666","links":[{"rel":"self","href":"/product/666"}]}'
end
end

it 'returns a hypermedia representation' do
get '/product/666'
expect(last_response.body).to eq '{"title":"Lonestar","id":"666","links":[{"rel":"self","href":"http://example.org/product/666"}]}'
context 'with an array of resources' do
before do
subject.get('/products') do
present [Product.new(title: 'Texassee', id: 1), Product.new(title: 'Lonestar', id: 2)], with: ProductRepresenter
end
end

it 'returns an array of hypermedia representations' do
get 'products'
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"}]}]'
end
end
end
end
5 changes: 3 additions & 2 deletions spec/support/order_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module OrderRepresenter

collection :articles, class: Article

link :self do
"/order/#{id}"
link :self do |opts|
request = Grape::Request.new(opts[:env])
"#{request.url}"
end

link :items do
Expand Down
5 changes: 2 additions & 3 deletions spec/support/product_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module ProductRepresenter
property :title
property :id

link :self do |opts|
request = Grape::Request.new(opts[:env])
"#{request.url}"
link :self do
"/product/#{id}"
end
end

0 comments on commit c4d0f6c

Please sign in to comment.