grape-jbuilder 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +2 -6
- data/CHANGELOG.md +12 -0
- data/README.md +16 -1
- data/grape-jbuilder.gemspec +1 -1
- data/lib/grape/formatter/jbuilder.rb +38 -0
- data/lib/grape/jbuilder.rb +3 -1
- data/lib/grape/jbuilder/renderer.rb +30 -0
- data/lib/grape/jbuilder/version.rb +1 -1
- data/spec/{grape_jbuilder_partials_spec.rb → acceptance/partials_spec.rb} +11 -14
- data/spec/acceptance/templates_spec.rb +60 -0
- data/spec/grape/formatter/jbuilder_spec.rb +55 -0
- data/spec/grape/jbuilder/renderer_spec.rb +47 -0
- data/spec/views/test.jbuilder +1 -0
- metadata +44 -58
- data/lib/grape/jbuilder/formatter.rb +0 -50
- data/spec/grape_jbuilder_spec.rb +0 -71
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6ba4826d37afc59462d6f7fe3b6ed36ee801e0b9
|
4
|
+
data.tar.gz: 5656897b9154ae71e56bee5ab1cb458f932dbb16
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a0967132a6118476b59d6673befe9704a1f81a44062d7ad0d0244ddaaa1d79ddd9b6884281e0671fe66499c3b8d410e303e934d15498b29736b332e97265a11d
|
7
|
+
data.tar.gz: 34ec154af6909af5a2db3d43b1624de6c280ac3573759f3219665be04094a0ab96a288fdc40b15c11afb2ca4e9bb6b9097ee6d707a203a8ca2472d6bdf33ff9d
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
0.2.0
|
2
|
+
=====
|
3
|
+
|
4
|
+
* [#4](https://github.com/milkcocoa/grape-jbuilder/pull/4) Shifting rendering into a separate class, to support dynamic changing of templates within the logic of a Grape endpoint.
|
5
|
+
* [#4](https://github.com/milkcocoa/grape-jbuilder/pull/4) Drop support MRI 1.9.2.
|
6
|
+
* Support MRI 2.1.1.
|
7
|
+
|
8
|
+
0.1.0
|
9
|
+
=====
|
10
|
+
|
11
|
+
* Initial commit.
|
12
|
+
|
data/README.md
CHANGED
@@ -53,7 +53,7 @@ end
|
|
53
53
|
Add the template name to the API options.
|
54
54
|
|
55
55
|
```ruby
|
56
|
-
get '/user/:id', :
|
56
|
+
get '/user/:id', jbuilder: 'user.jbuilder' do
|
57
57
|
@user = User.find(params[:id])
|
58
58
|
end
|
59
59
|
```
|
@@ -69,6 +69,21 @@ json.user do
|
|
69
69
|
end
|
70
70
|
```
|
71
71
|
|
72
|
+
### Use Jbuilder templates dynamically
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
get ':id' do
|
76
|
+
user = User.find_by(id: params[:id])
|
77
|
+
if article
|
78
|
+
env['api.tilt.template'] = 'users/show'
|
79
|
+
env['api.tilt.locals'] = {user: user}
|
80
|
+
else
|
81
|
+
status 404
|
82
|
+
{'status' => 'Not Found', 'errors' => "User id '#{params[:id]}' is not found."}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
72
87
|
## You can omit .jbuilder
|
73
88
|
|
74
89
|
The following are identical.
|
data/grape-jbuilder.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["masushu@gmail.com"]
|
11
11
|
spec.description = %q{Use Jbuilder in Grape}
|
12
12
|
spec.summary = %q{Use Jbuilder in Grape}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/milkcocoa/grape-jbuilder"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Grape
|
2
|
+
module Formatter
|
3
|
+
class Jbuilder
|
4
|
+
attr_reader :env, :endpoint, :object
|
5
|
+
|
6
|
+
def self.call(object, env)
|
7
|
+
new(object, env).call
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(object, env)
|
11
|
+
@object, @env = object, env
|
12
|
+
@endpoint = env['api.endpoint']
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
return Grape::Formatter::Json.call(object, env) unless template?
|
17
|
+
|
18
|
+
Grape::Jbuilder::Renderer.new(env['api.tilt.root'], template).
|
19
|
+
render(endpoint, locals)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def locals
|
25
|
+
env['api.tilt.locals'] || {}
|
26
|
+
end
|
27
|
+
|
28
|
+
def template
|
29
|
+
env['api.tilt.template'] ||
|
30
|
+
endpoint.options.fetch(:route_options, {})[:jbuilder]
|
31
|
+
end
|
32
|
+
|
33
|
+
def template?
|
34
|
+
!!template
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/grape/jbuilder.rb
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Grape
|
2
|
+
module Jbuilder
|
3
|
+
class Renderer
|
4
|
+
def initialize(view_path, template)
|
5
|
+
@view_path, @template = view_path, template
|
6
|
+
end
|
7
|
+
|
8
|
+
def render(scope, locals = {})
|
9
|
+
unless view_path
|
10
|
+
raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
|
11
|
+
end
|
12
|
+
|
13
|
+
engine = ::Tilt.new file, nil, view_path: view_path
|
14
|
+
engine.render scope, locals
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :view_path, :template
|
20
|
+
|
21
|
+
def file
|
22
|
+
File.join view_path, template_with_extension
|
23
|
+
end
|
24
|
+
|
25
|
+
def template_with_extension
|
26
|
+
template[/\.jbuilder$/] ? template : "#{template}.jbuilder"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,22 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'Grape::Jbuilder partials' do
|
4
|
-
|
5
|
-
Class.new(Grape::API)
|
6
|
-
end
|
7
|
-
|
8
|
-
before do
|
9
|
-
subject.format :json
|
10
|
-
subject.formatter :json, Grape::Formatter::Jbuilder
|
11
|
-
subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" }
|
12
|
-
end
|
4
|
+
let(:app) { Class.new(Grape::API) }
|
13
5
|
|
14
|
-
|
15
|
-
|
6
|
+
before :each do
|
7
|
+
app.format :json
|
8
|
+
app.formatter :json, Grape::Formatter::Jbuilder
|
9
|
+
app.before do
|
10
|
+
env['api.tilt.root'] = "#{File.dirname(__FILE__)}/../views"
|
11
|
+
end
|
16
12
|
end
|
17
13
|
|
18
14
|
it 'proper render partials' do
|
19
|
-
|
15
|
+
app.get('/home', jbuilder: 'project') do
|
20
16
|
@author = OpenStruct.new(author: 'LTe')
|
21
17
|
@type = OpenStruct.new(type: 'paper')
|
22
18
|
@project = OpenStruct.new(name: 'First', type: @type, author: @author)
|
@@ -35,8 +31,9 @@ describe 'Grape::Jbuilder partials' do
|
|
35
31
|
}
|
36
32
|
|
37
33
|
get('/home')
|
38
|
-
last_response.body.
|
39
|
-
last_response.body.
|
34
|
+
expect(last_response.body).to match_json_expression(pattern)
|
35
|
+
expect(last_response.body).to eq(
|
40
36
|
"{\"project\":{\"name\":\"First\",\"info\":{\"type\":\"paper\"},\"author\":{\"author\":\"LTe\"}}}"
|
37
|
+
)
|
41
38
|
end
|
42
39
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Grape::Jbuilder do
|
4
|
+
let(:app) { Class.new(Grape::API) }
|
5
|
+
|
6
|
+
before do
|
7
|
+
app.format :json
|
8
|
+
app.formatter :json, Grape::Formatter::Jbuilder
|
9
|
+
app.before do
|
10
|
+
env['api.tilt.root'] = "#{File.dirname(__FILE__)}/../views"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should work without jbuilder template' do
|
15
|
+
app.get('/home') { 'Hello World' }
|
16
|
+
|
17
|
+
get '/home'
|
18
|
+
|
19
|
+
expect(last_response.body).to eq('"Hello World"')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should work with dynamically set templates' do
|
23
|
+
app.get('/home') { env['api.tilt.template'] = 'test' }
|
24
|
+
|
25
|
+
get '/home'
|
26
|
+
|
27
|
+
expect(last_response.body).to eq({'foo' => 'bar'}.to_json)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should respond with proper content-type' do
|
31
|
+
app.get('/home', jbuilder: 'user') do
|
32
|
+
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
|
33
|
+
@project = OpenStruct.new(name: 'First')
|
34
|
+
end
|
35
|
+
|
36
|
+
get('/home')
|
37
|
+
|
38
|
+
expect(last_response.headers['Content-Type']).to eq('application/json')
|
39
|
+
end
|
40
|
+
|
41
|
+
it "renders the template's content" do
|
42
|
+
app.get('/home', jbuilder: 'user') do
|
43
|
+
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
|
44
|
+
@project = OpenStruct.new(name: 'First')
|
45
|
+
end
|
46
|
+
|
47
|
+
get('/home')
|
48
|
+
|
49
|
+
pattern = {
|
50
|
+
user: {
|
51
|
+
name: 'LTe',
|
52
|
+
email: 'email@example.com',
|
53
|
+
project: {
|
54
|
+
name: 'First'
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
expect(last_response.body).to match_json_expression(pattern)
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Grape::Formatter::Jbuilder do
|
4
|
+
let(:object) { double }
|
5
|
+
let(:env) { {'api.endpoint' => endpoint, 'api.tilt.root' => '/tmp'} }
|
6
|
+
let(:endpoint) { double :options => {} }
|
7
|
+
let(:renderer) { double :render => '' }
|
8
|
+
|
9
|
+
before :each do
|
10
|
+
Grape::Jbuilder::Renderer.stub :new => renderer
|
11
|
+
|
12
|
+
endpoint.options[:route_options] = {:jbuilder => 'user'}
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.call' do
|
16
|
+
it "passes the call to the Json render if no template is provided" do
|
17
|
+
endpoint.options.clear
|
18
|
+
|
19
|
+
expect(Grape::Formatter::Json).to receive(:call).with(object, env)
|
20
|
+
|
21
|
+
Grape::Formatter::Jbuilder.call object, env
|
22
|
+
end
|
23
|
+
|
24
|
+
it "creates a renderer with the root and template" do
|
25
|
+
expect(Grape::Jbuilder::Renderer).to receive(:new).with('/tmp', 'user').
|
26
|
+
and_return(renderer)
|
27
|
+
|
28
|
+
Grape::Formatter::Jbuilder.call object, env
|
29
|
+
end
|
30
|
+
|
31
|
+
it "accepts templates in the env hash" do
|
32
|
+
env['api.tilt.template'] = 'foo'
|
33
|
+
|
34
|
+
expect(Grape::Jbuilder::Renderer).to receive(:new).with('/tmp', 'foo').
|
35
|
+
and_return(renderer)
|
36
|
+
|
37
|
+
Grape::Formatter::Jbuilder.call object, env
|
38
|
+
end
|
39
|
+
|
40
|
+
it "passes the endpoint in as the render scope" do
|
41
|
+
expect(renderer).to receive(:render).with(endpoint, {}).and_return('')
|
42
|
+
|
43
|
+
Grape::Formatter::Jbuilder.call object, env
|
44
|
+
end
|
45
|
+
|
46
|
+
it "accepts custom locals for the render call" do
|
47
|
+
env['api.tilt.locals'] = {:foo => :bar}
|
48
|
+
|
49
|
+
expect(renderer).to receive(:render).with(endpoint, {:foo => :bar}).
|
50
|
+
and_return('')
|
51
|
+
|
52
|
+
Grape::Formatter::Jbuilder.call object, env
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Grape::Jbuilder::Renderer do
|
4
|
+
let(:engine) { double :render => '' }
|
5
|
+
let(:scope) { double }
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
Tilt.stub :new => engine
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#render' do
|
12
|
+
it "raises an error if the view path is nil" do
|
13
|
+
renderer = Grape::Jbuilder::Renderer.new(nil, 'file')
|
14
|
+
|
15
|
+
expect { renderer.render scope }.to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it "creates a new Tilt engine with the file and view path" do
|
19
|
+
renderer = Grape::Jbuilder::Renderer.new('/tmp', 'file.jbuilder')
|
20
|
+
|
21
|
+
expect(Tilt).to receive(:new).
|
22
|
+
with('/tmp/file.jbuilder', nil, :view_path => '/tmp').
|
23
|
+
and_return(engine)
|
24
|
+
|
25
|
+
renderer.render scope
|
26
|
+
end
|
27
|
+
|
28
|
+
it "appends jbuilder extension to the file if required" do
|
29
|
+
renderer = Grape::Jbuilder::Renderer.new('/tmp', 'file')
|
30
|
+
|
31
|
+
expect(Tilt).to receive(:new).
|
32
|
+
with('/tmp/file.jbuilder', nil, :view_path => '/tmp').
|
33
|
+
and_return(engine)
|
34
|
+
|
35
|
+
renderer.render scope
|
36
|
+
end
|
37
|
+
|
38
|
+
it "renders with the provided scope and locals" do
|
39
|
+
renderer = Grape::Jbuilder::Renderer.new('/tmp', 'file')
|
40
|
+
|
41
|
+
expect(engine).to receive(:render).with(scope, :foo => :bar).
|
42
|
+
and_return('')
|
43
|
+
|
44
|
+
renderer.render scope, :foo => :bar
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
json.foo 'bar'
|
metadata
CHANGED
@@ -1,174 +1,153 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-jbuilder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Shu Masuda
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-03-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: grape
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0.3'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0.3'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: jbuilder
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: tilt
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: tilt-jbuilder
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - ">="
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: 0.4.0
|
70
62
|
type: :runtime
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - ">="
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: 0.4.0
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: i18n
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - ">="
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :runtime
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - ">="
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
84
|
name: bundler
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
|
-
- -
|
87
|
+
- - ">="
|
100
88
|
- !ruby/object:Gem::Version
|
101
89
|
version: '0'
|
102
90
|
type: :development
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
93
|
requirements:
|
107
|
-
- -
|
94
|
+
- - ">="
|
108
95
|
- !ruby/object:Gem::Version
|
109
96
|
version: '0'
|
110
97
|
- !ruby/object:Gem::Dependency
|
111
98
|
name: json_expressions
|
112
99
|
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
100
|
requirements:
|
115
|
-
- -
|
101
|
+
- - ">="
|
116
102
|
- !ruby/object:Gem::Version
|
117
103
|
version: '0'
|
118
104
|
type: :development
|
119
105
|
prerelease: false
|
120
106
|
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
107
|
requirements:
|
123
|
-
- -
|
108
|
+
- - ">="
|
124
109
|
- !ruby/object:Gem::Version
|
125
110
|
version: '0'
|
126
111
|
- !ruby/object:Gem::Dependency
|
127
112
|
name: rack-test
|
128
113
|
requirement: !ruby/object:Gem::Requirement
|
129
|
-
none: false
|
130
114
|
requirements:
|
131
|
-
- -
|
115
|
+
- - ">="
|
132
116
|
- !ruby/object:Gem::Version
|
133
117
|
version: '0'
|
134
118
|
type: :development
|
135
119
|
prerelease: false
|
136
120
|
version_requirements: !ruby/object:Gem::Requirement
|
137
|
-
none: false
|
138
121
|
requirements:
|
139
|
-
- -
|
122
|
+
- - ">="
|
140
123
|
- !ruby/object:Gem::Version
|
141
124
|
version: '0'
|
142
125
|
- !ruby/object:Gem::Dependency
|
143
126
|
name: rake
|
144
127
|
requirement: !ruby/object:Gem::Requirement
|
145
|
-
none: false
|
146
128
|
requirements:
|
147
|
-
- -
|
129
|
+
- - ">="
|
148
130
|
- !ruby/object:Gem::Version
|
149
131
|
version: '0'
|
150
132
|
type: :development
|
151
133
|
prerelease: false
|
152
134
|
version_requirements: !ruby/object:Gem::Requirement
|
153
|
-
none: false
|
154
135
|
requirements:
|
155
|
-
- -
|
136
|
+
- - ">="
|
156
137
|
- !ruby/object:Gem::Version
|
157
138
|
version: '0'
|
158
139
|
- !ruby/object:Gem::Dependency
|
159
140
|
name: rspec
|
160
141
|
requirement: !ruby/object:Gem::Requirement
|
161
|
-
none: false
|
162
142
|
requirements:
|
163
|
-
- -
|
143
|
+
- - ">="
|
164
144
|
- !ruby/object:Gem::Version
|
165
145
|
version: '0'
|
166
146
|
type: :development
|
167
147
|
prerelease: false
|
168
148
|
version_requirements: !ruby/object:Gem::Requirement
|
169
|
-
none: false
|
170
149
|
requirements:
|
171
|
-
- -
|
150
|
+
- - ">="
|
172
151
|
- !ruby/object:Gem::Version
|
173
152
|
version: '0'
|
174
153
|
description: Use Jbuilder in Grape
|
@@ -178,52 +157,59 @@ executables: []
|
|
178
157
|
extensions: []
|
179
158
|
extra_rdoc_files: []
|
180
159
|
files:
|
181
|
-
- .gitignore
|
182
|
-
- .rspec
|
183
|
-
- .travis.yml
|
160
|
+
- ".gitignore"
|
161
|
+
- ".rspec"
|
162
|
+
- ".travis.yml"
|
163
|
+
- CHANGELOG.md
|
184
164
|
- Gemfile
|
185
165
|
- LICENSE.txt
|
186
166
|
- README.md
|
187
167
|
- Rakefile
|
188
168
|
- grape-jbuilder.gemspec
|
169
|
+
- lib/grape/formatter/jbuilder.rb
|
189
170
|
- lib/grape/jbuilder.rb
|
190
|
-
- lib/grape/jbuilder/
|
171
|
+
- lib/grape/jbuilder/renderer.rb
|
191
172
|
- lib/grape/jbuilder/version.rb
|
192
|
-
- spec/
|
193
|
-
- spec/
|
173
|
+
- spec/acceptance/partials_spec.rb
|
174
|
+
- spec/acceptance/templates_spec.rb
|
175
|
+
- spec/grape/formatter/jbuilder_spec.rb
|
176
|
+
- spec/grape/jbuilder/renderer_spec.rb
|
194
177
|
- spec/spec_helper.rb
|
195
178
|
- spec/views/_partial.jbuilder
|
196
179
|
- spec/views/project.jbuilder
|
180
|
+
- spec/views/test.jbuilder
|
197
181
|
- spec/views/user.jbuilder
|
198
|
-
homepage:
|
182
|
+
homepage: https://github.com/milkcocoa/grape-jbuilder
|
199
183
|
licenses:
|
200
184
|
- MIT
|
185
|
+
metadata: {}
|
201
186
|
post_install_message:
|
202
187
|
rdoc_options: []
|
203
188
|
require_paths:
|
204
189
|
- lib
|
205
190
|
required_ruby_version: !ruby/object:Gem::Requirement
|
206
|
-
none: false
|
207
191
|
requirements:
|
208
|
-
- -
|
192
|
+
- - ">="
|
209
193
|
- !ruby/object:Gem::Version
|
210
194
|
version: '0'
|
211
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
212
|
-
none: false
|
213
196
|
requirements:
|
214
|
-
- -
|
197
|
+
- - ">="
|
215
198
|
- !ruby/object:Gem::Version
|
216
199
|
version: '0'
|
217
200
|
requirements: []
|
218
201
|
rubyforge_project:
|
219
|
-
rubygems_version:
|
202
|
+
rubygems_version: 2.2.2
|
220
203
|
signing_key:
|
221
|
-
specification_version:
|
204
|
+
specification_version: 4
|
222
205
|
summary: Use Jbuilder in Grape
|
223
206
|
test_files:
|
224
|
-
- spec/
|
225
|
-
- spec/
|
207
|
+
- spec/acceptance/partials_spec.rb
|
208
|
+
- spec/acceptance/templates_spec.rb
|
209
|
+
- spec/grape/formatter/jbuilder_spec.rb
|
210
|
+
- spec/grape/jbuilder/renderer_spec.rb
|
226
211
|
- spec/spec_helper.rb
|
227
212
|
- spec/views/_partial.jbuilder
|
228
213
|
- spec/views/project.jbuilder
|
214
|
+
- spec/views/test.jbuilder
|
229
215
|
- spec/views/user.jbuilder
|
@@ -1,50 +0,0 @@
|
|
1
|
-
module Grape
|
2
|
-
module Formatter
|
3
|
-
module Jbuilder
|
4
|
-
class << self
|
5
|
-
|
6
|
-
attr_reader :env
|
7
|
-
attr_reader :endpoint
|
8
|
-
|
9
|
-
def call(object, env)
|
10
|
-
@env = env
|
11
|
-
@endpoint = env['api.endpoint']
|
12
|
-
|
13
|
-
if jbuilderable?
|
14
|
-
jbuilder do |template|
|
15
|
-
engine = ::Tilt.new(view_path(template), nil, view_path: env['api.tilt.root'])
|
16
|
-
engine.render(endpoint, {})
|
17
|
-
end
|
18
|
-
else
|
19
|
-
Grape::Formatter::Json.call object, env
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def view_path(template)
|
26
|
-
if template.split('.')[-1] == 'jbuilder'
|
27
|
-
File.join(env['api.tilt.root'], template)
|
28
|
-
else
|
29
|
-
File.join(env['api.tilt.root'], (template + '.jbuilder'))
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def jbuilderable?
|
34
|
-
!! endpoint.options[:route_options][:jbuilder]
|
35
|
-
end
|
36
|
-
|
37
|
-
def jbuilder
|
38
|
-
template = endpoint.options[:route_options][:jbuilder]
|
39
|
-
raise 'missing jbuilder template' unless template
|
40
|
-
set_view_root unless env['api.tilt.root']
|
41
|
-
yield template
|
42
|
-
end
|
43
|
-
|
44
|
-
def set_view_root
|
45
|
-
raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
data/spec/grape_jbuilder_spec.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Grape::Jbuilder do
|
4
|
-
subject do
|
5
|
-
Class.new(Grape::API)
|
6
|
-
end
|
7
|
-
|
8
|
-
before do
|
9
|
-
subject.format :json
|
10
|
-
subject.formatter :json, Grape::Formatter::Jbuilder
|
11
|
-
end
|
12
|
-
|
13
|
-
def app
|
14
|
-
subject
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'should work without jbuilder template' do
|
18
|
-
subject.get('/home') { 'Hello World' }
|
19
|
-
get '/home'
|
20
|
-
last_response.body.should == 'Hello World'
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should raise error about root directory' do
|
24
|
-
subject.get('/home', jbuilder: true){}
|
25
|
-
get '/home'
|
26
|
-
last_response.status.should == 500
|
27
|
-
last_response.body.should include "Use Rack::Config to set 'api.tilt.root' in config.ru"
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
context 'titl root is setup' do
|
32
|
-
before do
|
33
|
-
subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" }
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'should respond with proper content-type' do
|
37
|
-
subject.get('/home', jbuilder: 'user'){}
|
38
|
-
get('/home')
|
39
|
-
last_response.headers['Content-Type'].should == 'application/json'
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should not raise error about root directory' do
|
43
|
-
subject.get('/home', jbuilder: true){}
|
44
|
-
get '/home'
|
45
|
-
last_response.status.should == 500
|
46
|
-
last_response.body.should_not include "Use Rack::Config to set 'api.tilt.root' in config.ru"
|
47
|
-
end
|
48
|
-
|
49
|
-
['user', 'user.jbuilder'].each do |jbuilder_option|
|
50
|
-
it 'should render jbuilder template (#{jbuilder_option})' do
|
51
|
-
subject.get('/home', jbuilder: jbuilder_option) do
|
52
|
-
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
|
53
|
-
@project = OpenStruct.new(name: 'First')
|
54
|
-
end
|
55
|
-
|
56
|
-
pattern = {
|
57
|
-
user: {
|
58
|
-
name: 'LTe',
|
59
|
-
email: 'email@example.com',
|
60
|
-
project: {
|
61
|
-
name: 'First'
|
62
|
-
}
|
63
|
-
}
|
64
|
-
}
|
65
|
-
|
66
|
-
get '/home'
|
67
|
-
last_response.body.should match_json_expression(pattern)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|