From c4d0f6c0e01bd26124628db44cbf86b326312b21 Mon Sep 17 00:00:00 2001 From: John Wilkinson Date: Fri, 10 Jun 2016 01:23:28 +0800 Subject: [PATCH] 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. --- .travis.yml | 4 ++++ spec/decorator_spec.rb | 27 +++++++++++++++++++++------ spec/nested_representer_spec.rb | 3 ++- spec/present_with_spec.rb | 27 +++++++++++++++++++++------ spec/support/order_representer.rb | 5 +++-- spec/support/product_representer.rb | 5 ++--- 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 80e020f..e4881a3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,3 +11,7 @@ rvm: - 1.9.3 - jruby-19mode - rbx-2.2.10 + +matrix: + allow_failures: + - rvm: rbx-2.2.10 diff --git a/spec/decorator_spec.rb b/spec/decorator_spec.rb index 24f9290..1b88bed 100644 --- a/spec/decorator_spec.rb +++ b/spec/decorator_spec.rb @@ -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 diff --git a/spec/nested_representer_spec.rb b/spec/nested_representer_spec.rb index f256596..90ed4fe 100644 --- a/spec/nested_representer_spec.rb +++ b/spec/nested_representer_spec.rb @@ -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 diff --git a/spec/present_with_spec.rb b/spec/present_with_spec.rb index 2143190..2ce246a 100644 --- a/spec/present_with_spec.rb +++ b/spec/present_with_spec.rb @@ -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 diff --git a/spec/support/order_representer.rb b/spec/support/order_representer.rb index 28b2496..ed24f31 100644 --- a/spec/support/order_representer.rb +++ b/spec/support/order_representer.rb @@ -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 diff --git a/spec/support/product_representer.rb b/spec/support/product_representer.rb index 3b558e4..c341737 100644 --- a/spec/support/product_representer.rb +++ b/spec/support/product_representer.rb @@ -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