grape-rabl 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -6
- data/Gemfile +1 -0
- data/README.md +24 -9
- data/lib/grape-rabl/formatter.rb +8 -5
- data/lib/grape-rabl/render.rb +2 -2
- data/lib/grape-rabl/version.rb +1 -1
- data/spec/grape_rabl_layout_spec.rb +6 -4
- data/spec/grape_rabl_partials_spec.rb +4 -2
- data/spec/grape_rabl_spec.rb +29 -6
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d621405f4d7c383ff47dea4a2db866618aaeff12
|
4
|
+
data.tar.gz: 3ec7ec4c04e7bd20a390fa55acba8ac84171e6d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66582082999fe94194742f88ba1d4644dcce4dd83717403ea0202a45544c114f6a1e47c2358205c7d951509bee9771a43501ae8e7ad5684b39f1eadcd0584a42
|
7
|
+
data.tar.gz: 399b670fd1ac71cac4b0b4fe40006274dbe58b878ffe95a031f7b172a354fe2013aea1d725c6348e7e589c96c8128b7b677d5d80a6592b169b2ea1e323576e86
|
data/CHANGELOG.md
CHANGED
@@ -1,14 +1,18 @@
|
|
1
|
+
#### v0.3.1
|
2
|
+
|
3
|
+
* The `render` method will no longer modify endpoint options at runtime. [#35](https://github.com/LTe/grape-rabl/issues/35) [@yesmeck](https://github.com/yesmeck)
|
4
|
+
|
1
5
|
#### v0.3.0
|
2
6
|
|
3
|
-
* Enable using a layout template in Rabl [@koko1000ban](https://github.com/koko1000ban) [view commit](https://github.com/LTe/grape-rabl/commit/1fbfbd58c3fb320be1b52b3247fda2a23cacc9fc)
|
4
|
-
* Implemented Rubocop, Ruby style linter [@dblock](https://github.com/dblock) [view commit](https://github.com/LTe/grape-rabl/commit/1211056de22a5989c063d57b7b37ebb1f1977e83)
|
5
|
-
* Removed JRuby support [@dblock](https://github.com/dblock) [view commit](https://github.com/LTe/grape-rabl/commit/59905c1b09670fe08501e09bad4ec8714839f2d3)
|
6
|
-
* Enable using locals with #render [@hobofan](https://github.com/hobofan) [view commit](https://github.com/LTe/grape-rabl/commit/6c24130f6a670e52e6119c56904b8ed2e6f60b39)
|
7
|
+
* Enable using a layout template in Rabl. [@koko1000ban](https://github.com/koko1000ban) [view commit](https://github.com/LTe/grape-rabl/commit/1fbfbd58c3fb320be1b52b3247fda2a23cacc9fc)
|
8
|
+
* Implemented Rubocop, Ruby style linter. [@dblock](https://github.com/dblock) [view commit](https://github.com/LTe/grape-rabl/commit/1211056de22a5989c063d57b7b37ebb1f1977e83)
|
9
|
+
* Removed JRuby support. [@dblock](https://github.com/dblock) [view commit](https://github.com/LTe/grape-rabl/commit/59905c1b09670fe08501e09bad4ec8714839f2d3)
|
10
|
+
* Enable using locals with #render. [@hobofan](https://github.com/hobofan) [view commit](https://github.com/LTe/grape-rabl/commit/6c24130f6a670e52e6119c56904b8ed2e6f60b39)
|
7
11
|
* Enable support for template caching. [#28](https://github.com/LTe/grape-rabl/pull/28) [@kushkella](https://github.com/kushkella) [view commit](https://github.com/LTe/grape-rabl/commit/79b1e58d767c6286b510af669e718310c0ad25c2)
|
8
12
|
|
9
13
|
#### v0.2.2
|
10
14
|
|
11
|
-
* Relaxed dependency on a specific version of Grape [#20](https://github.com/LTe/grape-rabl/pull/20) [@cheef](https://github.com/cheef) [view commit](https://github.com/LTe/grape-rabl/commit/56da0a5bcecb16501cdd93ac25f3b6ca6d7a86f0)
|
15
|
+
* Relaxed dependency on a specific version of Grape. [#20](https://github.com/LTe/grape-rabl/pull/20) [@cheef](https://github.com/cheef) [view commit](https://github.com/LTe/grape-rabl/commit/56da0a5bcecb16501cdd93ac25f3b6ca6d7a86f0)
|
12
16
|
|
13
17
|
#### v0.2.1
|
14
18
|
|
@@ -16,7 +20,7 @@
|
|
16
20
|
|
17
21
|
#### v0.2.0
|
18
22
|
|
19
|
-
* Allow to use partials in
|
23
|
+
* Allow to use partials in Grape. [#10](https://github.com/LTe/grape-rabl/pull/10) [@ichilton](https://github.com/ichilton) [view commit](http://github.com/LTe/grape-rabl/commit/72c96c5acc9d8000f56ee8400ae0229053fb3e7e)
|
20
24
|
* Stick to gem conventions. [@LTe](https://github.com/lte) [view commit](http://github.com/LTe/grape-rabl/commit/aabd0e2ad72f56a75427eebcc586deed57cf5f58)
|
21
25
|
* Update for Grape 0.3 compatibility. [@alovak](https://github.com/alovak) [view commit](http://github.com/LTe/grape-rabl/commit/78bfdceffbfe90b700868ff1e79ab87e8baded81)
|
22
26
|
* Format fix. [@LTe](https://github.com/lte) [view commit](http://github.com/LTe/grape-rabl/commit/13749cc18d332dcd0050bb32980cc233868a7992)
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Use [Rabl](https://github.com/nesquena/rabl) templates in [Grape](https://github.com/intridea/grape)!
|
4
4
|
|
5
|
-
[](http://badge.fury.io/rb/grape-rabl)
|
6
|
+
[](https://travis-ci.org/LTe/grape-rabl)
|
7
|
+
[](https://gemnasium.com/LTe/grape-rabl)
|
8
|
+
[](https://codeclimate.com/github/LTe/grape-rabl)
|
9
|
+
[](https://coveralls.io/r/LTe/grape-rabl?branch=master)
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
@@ -124,16 +124,31 @@ class UserAPI < Grape::API
|
|
124
124
|
format :json
|
125
125
|
formatter :json, Grape::Formatter::Rabl
|
126
126
|
|
127
|
-
|
128
|
-
get '/user/:id', :rabl => 'user' do
|
127
|
+
get '/user/:id' do
|
129
128
|
@user = User.find(params[:id])
|
130
129
|
|
130
|
+
# use rabl with 'user.rabl' or 'admin.rabl' template
|
131
131
|
if @user.admin?
|
132
|
-
#
|
133
|
-
render rabl: 'admin', locals: { details: 'this user is
|
132
|
+
# pass locals with the #render method
|
133
|
+
render rabl: 'admin', locals: { details: 'this user is an admin' }
|
134
|
+
else
|
135
|
+
render rabl: 'user'
|
134
136
|
end
|
135
137
|
end
|
136
138
|
|
139
|
+
get '/admin/:id', :rabl => 'admin' do
|
140
|
+
@user = User.find(params[:id])
|
141
|
+
|
142
|
+
# use rabl with 'super_admin.rabl'
|
143
|
+
render rabl: 'super_admin' if @user.super_admin?
|
144
|
+
# when render method has not been used use template from endpoint definition
|
145
|
+
end
|
146
|
+
|
147
|
+
# use rabl with 'user_history.rabl' template
|
148
|
+
get '/user/:id/history', :rabl => 'user_history' do
|
149
|
+
@history = User.find(params[:id]).history
|
150
|
+
end
|
151
|
+
|
137
152
|
# do not use rabl, fallback to the defalt Grape JSON formatter
|
138
153
|
get '/users' do
|
139
154
|
User.all
|
data/lib/grape-rabl/formatter.rb
CHANGED
@@ -37,18 +37,21 @@ module Grape
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def rablable?
|
40
|
-
!!
|
40
|
+
!!rabl_template
|
41
41
|
end
|
42
42
|
|
43
43
|
def rabl
|
44
|
-
template
|
45
|
-
fail 'missing rabl template' unless template
|
44
|
+
fail 'missing rabl template' unless rabl_template
|
46
45
|
set_view_root unless env['api.tilt.root']
|
47
|
-
yield
|
46
|
+
yield rabl_template
|
48
47
|
end
|
49
48
|
|
50
49
|
def locals
|
51
|
-
endpoint.options[:route_options][:rabl_locals] || {}
|
50
|
+
env['api.tilt.rabl_locals'] || endpoint.options[:route_options][:rabl_locals] || {}
|
51
|
+
end
|
52
|
+
|
53
|
+
def rabl_template
|
54
|
+
env['api.tilt.rabl'] || endpoint.options[:route_options][:rabl]
|
52
55
|
end
|
53
56
|
|
54
57
|
def set_view_root
|
data/lib/grape-rabl/render.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module GrapeRabl
|
2
2
|
module Render
|
3
3
|
def render(options = {})
|
4
|
-
env['api.
|
5
|
-
env['api.
|
4
|
+
env['api.tilt.rabl'] = options[:rabl]
|
5
|
+
env['api.tilt.rabl_locals'] = options[:locals]
|
6
6
|
end
|
7
7
|
end
|
8
8
|
end
|
data/lib/grape-rabl/version.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'Grape::Rabl layout' do
|
4
|
+
let(:parsed_response) { JSON.parse(last_response.body) }
|
5
|
+
|
4
6
|
subject do
|
5
7
|
Class.new(Grape::API)
|
6
8
|
end
|
@@ -26,8 +28,8 @@ describe 'Grape::Rabl layout' do
|
|
26
28
|
end
|
27
29
|
|
28
30
|
get('/about')
|
29
|
-
|
30
|
-
%Q({"status":200,"result":{"user":{"name":"LTe","project":{"name":"First"}}}})
|
31
|
+
parsed_response.should ==
|
32
|
+
JSON.parse(%Q({"status":200,"result":{"user":{"name":"LTe","project":{"name":"First"}}}}))
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
@@ -45,8 +47,8 @@ describe 'Grape::Rabl layout' do
|
|
45
47
|
|
46
48
|
get('/about')
|
47
49
|
puts last_response.body
|
48
|
-
|
49
|
-
%Q({"result":{"user":{"name":"LTe","project":{"name":"First"}}}})
|
50
|
+
parsed_response.should ==
|
51
|
+
JSON.parse(%Q({"result":{"user":{"name":"LTe","project":{"name":"First"}}}}))
|
50
52
|
end
|
51
53
|
end
|
52
54
|
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'Grape::Rabl partials' do
|
4
|
+
let(:parsed_response) { JSON.parse(last_response.body) }
|
5
|
+
|
4
6
|
subject do
|
5
7
|
Class.new(Grape::API)
|
6
8
|
end
|
@@ -23,7 +25,7 @@ describe 'Grape::Rabl partials' do
|
|
23
25
|
end
|
24
26
|
|
25
27
|
get('/home')
|
26
|
-
|
27
|
-
"{\"project\":{\"name\":\"First\",\"info\":{\"type\":\"paper\"},\"author\":{\"author\":\"LTe\"}}}"
|
28
|
+
parsed_response.should ==
|
29
|
+
JSON.parse("{\"project\":{\"name\":\"First\",\"info\":{\"type\":\"paper\"},\"author\":{\"author\":\"LTe\"}}}")
|
28
30
|
end
|
29
31
|
end
|
data/spec/grape_rabl_spec.rb
CHANGED
@@ -31,6 +31,8 @@ describe Grape::Rabl do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
context 'titl root is setup' do
|
34
|
+
let(:parsed_response) { JSON.parse(last_response.body) }
|
35
|
+
|
34
36
|
before do
|
35
37
|
subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" }
|
36
38
|
end
|
@@ -39,7 +41,7 @@ describe Grape::Rabl do
|
|
39
41
|
it 'should execute helper' do
|
40
42
|
subject.get('/home', rabl: 'helper') { @user = OpenStruct.new }
|
41
43
|
get '/home'
|
42
|
-
|
44
|
+
parsed_response.should == JSON.parse("{\"user\":{\"helper\":\"my_helper\"}}")
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
@@ -50,6 +52,12 @@ describe Grape::Rabl do
|
|
50
52
|
render rabl: 'admin'
|
51
53
|
end
|
52
54
|
|
55
|
+
subject.get('/admin/:id', rabl: 'user') do
|
56
|
+
@user = OpenStruct.new(name: 'LTe')
|
57
|
+
|
58
|
+
render rabl: 'admin' if params[:id] == '1'
|
59
|
+
end
|
60
|
+
|
53
61
|
subject.get('/home-detail', rabl: 'user') do
|
54
62
|
@user = OpenStruct.new(name: 'LTe')
|
55
63
|
render rabl: 'admin', locals: { details: 'amazing detail' }
|
@@ -67,23 +75,38 @@ describe Grape::Rabl do
|
|
67
75
|
|
68
76
|
it 'renders template passed as argument to render method' do
|
69
77
|
get('/home')
|
70
|
-
|
78
|
+
parsed_response.should == JSON.parse('{"admin":{"name":"LTe"}}')
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'renders admin template' do
|
82
|
+
get('/admin/1')
|
83
|
+
parsed_response.should == JSON.parse('{"admin":{"name":"LTe"}}')
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'renders user template' do
|
87
|
+
get('/admin/2')
|
88
|
+
parsed_response.should == JSON.parse('{"user":{"name":"LTe","project":null}}')
|
71
89
|
end
|
72
90
|
|
73
91
|
it 'renders template passed as argument to render method with locals' do
|
74
92
|
get('/home-detail')
|
75
|
-
|
93
|
+
parsed_response.should == JSON.parse('{"admin":{"name":"LTe","details":"amazing detail"}}')
|
76
94
|
end
|
77
95
|
|
78
96
|
it 'renders with locals without overriding template' do
|
79
97
|
get('/about-detail')
|
80
|
-
|
98
|
+
parsed_response.should == JSON.parse('{"user":{"name":"LTe","details":"just a user","project":null}}')
|
81
99
|
end
|
82
100
|
|
83
101
|
it 'does not save rabl options after called #render method' do
|
84
102
|
get('/home')
|
85
103
|
get('/about')
|
86
|
-
|
104
|
+
parsed_response.should == JSON.parse('{"user":{"name":"LTe","project":null}}')
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'does not modify endpoint options' do
|
108
|
+
get '/home'
|
109
|
+
expect(last_request.env['api.endpoint'].options[:route_options][:rabl]).to eq 'user'
|
87
110
|
end
|
88
111
|
end
|
89
112
|
|
@@ -108,7 +131,7 @@ describe Grape::Rabl do
|
|
108
131
|
end
|
109
132
|
|
110
133
|
get '/home'
|
111
|
-
|
134
|
+
parsed_response.should == JSON.parse('{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}')
|
112
135
|
end
|
113
136
|
end
|
114
137
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Niełacny
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
requirements: []
|
128
128
|
rubyforge_project:
|
129
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.4.5
|
130
130
|
signing_key:
|
131
131
|
specification_version: 4
|
132
132
|
summary: Use rabl in grape
|