grape-rabl 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +34 -0
- data/.travis.yml +7 -3
- data/CHANGELOG.md +8 -0
- data/Gemfile +8 -0
- data/README.md +36 -0
- data/Rakefile +4 -1
- data/lib/grape-rabl/configuration.rb +11 -0
- data/lib/grape-rabl/formatter.rb +52 -22
- data/lib/grape-rabl/render.rb +2 -1
- data/lib/grape-rabl/version.rb +1 -1
- data/lib/grape-rabl.rb +27 -7
- data/spec/grape_rabl_configuration.rb +18 -0
- data/spec/grape_rabl_layout_spec.rb +96 -0
- data/spec/grape_rabl_partials_spec.rb +9 -9
- data/spec/grape_rabl_spec.rb +98 -36
- data/spec/grape_rabl_xml_spec.rb +24 -16
- data/spec/spec_helper.rb +20 -21
- data/spec/support/my_helper.rb +2 -2
- data/spec/views/admin.rabl +4 -0
- data/spec/views/layout_test/layouts/another.rabl +3 -0
- data/spec/views/layout_test/layouts/application.rabl +5 -0
- data/spec/views/layout_test/user.rabl +6 -0
- data/spec/views/user.rabl +4 -0
- metadata +27 -28
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ebef1acc9ea4453a55850d6af0a663f497681f23
|
4
|
+
data.tar.gz: ccb43ab0335200f0c3194325e3b1de87d711be62
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2dee5455c625c89ec8c2c86858b6d4ba27c6f6b6d63b47e6ad23d6b824d089cd23958de9e1bd1a3731364cd60170d7b4dd701902edcf149530b1f88e9227c48
|
7
|
+
data.tar.gz: 129d37a3b51bd384bcaeda98b092eba9b94d6adacb494c9731279ca398e5876cbf14136d1ab8edd459f757060ff63b172a97962f9c3866c50ed8f0e71713c090
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- vendor/**
|
4
|
+
- .bundle
|
5
|
+
|
6
|
+
LineLength:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
MethodLength:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
ClassLength:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Documentation:
|
16
|
+
# don't require classes to be documented
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Encoding:
|
20
|
+
# no need to always specify encoding
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
CollectionMethods:
|
24
|
+
# don't prefer map to collect, recuce to inject
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
RescueException:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
DoubleNegation:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
FileName:
|
34
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
#### v0.3.0
|
2
|
+
|
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 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
|
+
|
1
9
|
#### v0.2.2
|
2
10
|
|
3
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)
|
data/Gemfile
CHANGED
@@ -2,10 +2,18 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
+
group :development do
|
6
|
+
gem "rubocop", "0.20.1"
|
7
|
+
end
|
8
|
+
|
5
9
|
group :test do
|
6
10
|
gem "json", '~> 1.7.7'
|
7
11
|
gem "rspec", "~> 2.12.0"
|
8
12
|
gem "rack-test"
|
9
13
|
gem "rake"
|
10
14
|
gem "coveralls", require: false
|
15
|
+
|
16
|
+
platforms :rbx do
|
17
|
+
gem "iconv"
|
18
|
+
end
|
11
19
|
end
|
data/README.md
CHANGED
@@ -70,6 +70,37 @@ child @project => :project do
|
|
70
70
|
end
|
71
71
|
```
|
72
72
|
|
73
|
+
### Use rabl layout
|
74
|
+
|
75
|
+
Gape-rabl first looks for a layout file in `#{env['api.tilt.root']}/layouts/application.rabl`.
|
76
|
+
|
77
|
+
You can override the default layout conventions:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
# config.ru
|
81
|
+
require 'grape/rabl'
|
82
|
+
|
83
|
+
use Rack::Config do |env|
|
84
|
+
env['api.tilt.root'] = '/path/to/view/root/directory'
|
85
|
+
env['api.tilt.layout'] = 'layouts/another'
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
### Enable template caching
|
90
|
+
|
91
|
+
Gape-rabl allows for template caching after templates are loaded initially.
|
92
|
+
|
93
|
+
You can enable template caching:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
# config.ru
|
97
|
+
require 'grape/rabl'
|
98
|
+
|
99
|
+
Grape::Rabl.configure do |config|
|
100
|
+
config.cache_template_loading = true # default: false
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
73
104
|
## You can omit .rabl
|
74
105
|
|
75
106
|
The following are identical.
|
@@ -96,6 +127,11 @@ class UserAPI < Grape::API
|
|
96
127
|
# use rabl with 'user.rabl' template
|
97
128
|
get '/user/:id', :rabl => 'user' do
|
98
129
|
@user = User.find(params[:id])
|
130
|
+
|
131
|
+
if @user.admin?
|
132
|
+
# overwrite the template (and pass locals) with the #render method
|
133
|
+
render rabl: 'admin', locals: { details: 'this user is a admin' }
|
134
|
+
end
|
99
135
|
end
|
100
136
|
|
101
137
|
# do not use rabl, fallback to the defalt Grape JSON formatter
|
data/Rakefile
CHANGED
data/lib/grape-rabl/formatter.rb
CHANGED
@@ -4,51 +4,81 @@ module Grape
|
|
4
4
|
module Formatter
|
5
5
|
module Rabl
|
6
6
|
class << self
|
7
|
-
|
8
7
|
attr_reader :env
|
9
8
|
attr_reader :endpoint
|
10
9
|
|
11
10
|
def call(object, env)
|
12
|
-
|
13
11
|
@env = env
|
14
12
|
@endpoint = env['api.endpoint']
|
15
13
|
|
16
14
|
if rablable?
|
17
15
|
rabl do |template|
|
18
|
-
engine =
|
19
|
-
engine.render endpoint,
|
16
|
+
engine = tilt_template(template)
|
17
|
+
output = engine.render endpoint, locals
|
18
|
+
if layout_template
|
19
|
+
layout_template.render(endpoint) { output }
|
20
|
+
else
|
21
|
+
output
|
22
|
+
end
|
20
23
|
end
|
21
24
|
else
|
22
25
|
Grape::Formatter::Json.call object, env
|
23
26
|
end
|
24
|
-
|
25
27
|
end
|
26
28
|
|
27
29
|
private
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
31
|
+
def view_path(template)
|
32
|
+
if template.split('.')[-1] == 'rabl'
|
33
|
+
File.join(env['api.tilt.root'], template)
|
34
|
+
else
|
35
|
+
File.join(env['api.tilt.root'], (template + '.rabl'))
|
35
36
|
end
|
37
|
+
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
39
|
+
def rablable?
|
40
|
+
!!endpoint.options[:route_options][:rabl]
|
41
|
+
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
def rabl
|
44
|
+
template = endpoint.options[:route_options][:rabl]
|
45
|
+
fail 'missing rabl template' unless template
|
46
|
+
set_view_root unless env['api.tilt.root']
|
47
|
+
yield template
|
48
|
+
end
|
49
|
+
|
50
|
+
def locals
|
51
|
+
endpoint.options[:route_options][:rabl_locals] || {}
|
52
|
+
end
|
47
53
|
|
48
|
-
|
49
|
-
|
54
|
+
def set_view_root
|
55
|
+
fail "Use Rack::Config to set 'api.tilt.root' in config.ru"
|
56
|
+
end
|
57
|
+
|
58
|
+
def tilt_template(template)
|
59
|
+
if Grape::Rabl.configuration.cache_template_loading
|
60
|
+
tilt_cache.fetch(template) { ::Tilt.new(view_path(template), tilt_options) }
|
61
|
+
else
|
62
|
+
::Tilt.new(view_path(template), tilt_options)
|
50
63
|
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def tilt_cache
|
67
|
+
@tilt_cache ||= ::Tilt::Cache.new
|
68
|
+
end
|
51
69
|
|
70
|
+
def tilt_options
|
71
|
+
{ format: env['api.format'], view_path: env['api.tilt.root'] }
|
72
|
+
end
|
73
|
+
|
74
|
+
def layout_template
|
75
|
+
layout_path = view_path(env['api.tilt.layout'] || 'layouts/application')
|
76
|
+
if Grape::Rabl.configuration.cache_template_loading
|
77
|
+
tilt_cache.fetch(layout_path) { ::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path) }
|
78
|
+
else
|
79
|
+
::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path)
|
80
|
+
end
|
81
|
+
end
|
52
82
|
end
|
53
83
|
end
|
54
84
|
end
|
data/lib/grape-rabl/render.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module GrapeRabl
|
2
2
|
module Render
|
3
3
|
def render(options = {})
|
4
|
-
env['api.endpoint'].options[:route_options][:rabl] = options.delete(:rabl)
|
4
|
+
env['api.endpoint'].options[:route_options][:rabl] = options.delete(:rabl) if options.include?(:rabl)
|
5
|
+
env['api.endpoint'].options[:route_options][:rabl_locals] = options.delete(:locals)
|
5
6
|
end
|
6
7
|
end
|
7
8
|
end
|
data/lib/grape-rabl/version.rb
CHANGED
data/lib/grape-rabl.rb
CHANGED
@@ -1,7 +1,27 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
1
|
+
require 'rabl'
|
2
|
+
require 'grape'
|
3
|
+
require 'hashie/hash'
|
4
|
+
require 'grape-rabl/tilt'
|
5
|
+
require 'grape-rabl/version'
|
6
|
+
require 'grape-rabl/formatter'
|
7
|
+
require 'grape-rabl/render'
|
8
|
+
require 'grape-rabl/configuration'
|
9
|
+
|
10
|
+
module Grape
|
11
|
+
module Rabl
|
12
|
+
class << self
|
13
|
+
def configure(&block)
|
14
|
+
yield(configuration)
|
15
|
+
configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
def configuration
|
19
|
+
@configuration ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset_configuration!
|
23
|
+
@configuration = nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Grape::Rabl configuration' do
|
4
|
+
context 'configuration' do
|
5
|
+
it 'returns default values' do
|
6
|
+
Grape::Rabl.configuration.cache_template_loading.should == false
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should set and reset configuration' do
|
10
|
+
Grape::Rabl.configure do |config|
|
11
|
+
config.cache_template_loading = true
|
12
|
+
end
|
13
|
+
Grape::Rabl.configuration.cache_template_loading.should be == true
|
14
|
+
Grape::Rabl.reset_configuration!
|
15
|
+
Grape::Rabl.configuration.cache_template_loading.should == false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Grape::Rabl layout' 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::Rabl
|
11
|
+
subject.before do
|
12
|
+
env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views/layout_test"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def app
|
17
|
+
subject
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'default' do
|
21
|
+
it 'proper render with default layout' do
|
22
|
+
subject.get('/about', rabl: 'user') do
|
23
|
+
@user = OpenStruct.new(name: 'LTe')
|
24
|
+
@project = OpenStruct.new(name: 'First')
|
25
|
+
@status = 200
|
26
|
+
end
|
27
|
+
|
28
|
+
get('/about')
|
29
|
+
last_response.body.should ==
|
30
|
+
%Q({"status":200,"result":{"user":{"name":"LTe","project":{"name":"First"}}}})
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'tilt layout is setup' do
|
35
|
+
before do
|
36
|
+
subject.before { env['api.tilt.layout'] = 'layouts/another' }
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'proper render with specified layout' do
|
40
|
+
subject.get('/about', rabl: 'user') do
|
41
|
+
@user = OpenStruct.new(name: 'LTe')
|
42
|
+
@project = OpenStruct.new(name: 'First')
|
43
|
+
@status = 200
|
44
|
+
end
|
45
|
+
|
46
|
+
get('/about')
|
47
|
+
puts last_response.body
|
48
|
+
last_response.body.should ==
|
49
|
+
%Q({"result":{"user":{"name":"LTe","project":{"name":"First"}}}})
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'layout cache' do
|
54
|
+
before do
|
55
|
+
@views_dir = FileUtils.mkdir_p("#{File.expand_path("..", File.dirname(__FILE__))}/tmp")[0]
|
56
|
+
@layout = "#{@views_dir}/layouts/application.rabl"
|
57
|
+
FileUtils.cp_r("#{File.dirname(__FILE__)}/views/layout_test/.", @views_dir)
|
58
|
+
subject.before { env['api.tilt.root'] = "#{File.expand_path("..", File.dirname(__FILE__))}/tmp" }
|
59
|
+
subject.get('/home', rabl: 'user') do
|
60
|
+
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
|
61
|
+
@project = OpenStruct.new(name: 'First')
|
62
|
+
@status = 200
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
after do
|
67
|
+
Grape::Rabl.reset_configuration!
|
68
|
+
FileUtils.rm_f(@views_dir)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should serve from cache if cache_template_loading' do
|
72
|
+
Grape::Rabl.configure do |config|
|
73
|
+
config.cache_template_loading = true
|
74
|
+
end
|
75
|
+
get '/home'
|
76
|
+
last_response.status.should be == 200
|
77
|
+
old_response = last_response.body
|
78
|
+
open(@layout, 'a') { |f| f << 'node(:test) { "test" }' }
|
79
|
+
get '/home'
|
80
|
+
last_response.status.should be == 200
|
81
|
+
new_response = last_response.body
|
82
|
+
old_response.should == new_response
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should serve new template if cache_template_loading' do
|
86
|
+
get '/home'
|
87
|
+
last_response.status.should be == 200
|
88
|
+
old_response = last_response.body
|
89
|
+
open(@layout, 'a') { |f| f << 'node(:test) { "test" }' }
|
90
|
+
get '/home'
|
91
|
+
last_response.status.should be == 200
|
92
|
+
new_response = last_response.body
|
93
|
+
old_response.should_not == new_response
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Grape::Rabl partials' do
|
4
4
|
subject do
|
5
5
|
Class.new(Grape::API)
|
6
6
|
end
|
@@ -8,22 +8,22 @@ describe "Grape::Rabl partials" do
|
|
8
8
|
before do
|
9
9
|
subject.format :json
|
10
10
|
subject.formatter :json, Grape::Formatter::Rabl
|
11
|
-
subject.before { env[
|
11
|
+
subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" }
|
12
12
|
end
|
13
13
|
|
14
14
|
def app
|
15
15
|
subject
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
19
|
-
subject.get(
|
20
|
-
@author = OpenStruct.new(:
|
21
|
-
@type = OpenStruct.new(:
|
22
|
-
@project = OpenStruct.new(:
|
18
|
+
it 'proper render partials' do
|
19
|
+
subject.get('/home', rabl: 'project') do
|
20
|
+
@author = OpenStruct.new(author: 'LTe')
|
21
|
+
@type = OpenStruct.new(type: 'paper')
|
22
|
+
@project = OpenStruct.new(name: 'First', type: @type, author: @author)
|
23
23
|
end
|
24
24
|
|
25
|
-
get(
|
25
|
+
get('/home')
|
26
26
|
last_response.body.should ==
|
27
27
|
"{\"project\":{\"name\":\"First\",\"info\":{\"type\":\"paper\"},\"author\":{\"author\":\"LTe\"}}}"
|
28
28
|
end
|
29
|
-
end
|
29
|
+
end
|
data/spec/grape_rabl_spec.rb
CHANGED
@@ -16,81 +16,143 @@ describe Grape::Rabl do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'should work without rabl template' do
|
19
|
-
subject.get(
|
20
|
-
get
|
19
|
+
subject.get('/home') { 'Hello World' }
|
20
|
+
get '/home'
|
21
21
|
last_response.body.should == "\"Hello World\""
|
22
22
|
end
|
23
23
|
|
24
|
-
it
|
24
|
+
it 'should raise error about root directory' do
|
25
25
|
begin
|
26
|
-
subject.get(
|
27
|
-
get
|
26
|
+
subject.get('/home', rabl: true) {}
|
27
|
+
get '/home'
|
28
28
|
rescue Exception => e
|
29
29
|
e.message.should include "Use Rack::Config to set 'api.tilt.root' in config.ru"
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
context
|
33
|
+
context 'titl root is setup' do
|
34
34
|
before do
|
35
|
-
subject.before { env[
|
35
|
+
subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" }
|
36
36
|
end
|
37
37
|
|
38
|
-
describe
|
39
|
-
it
|
40
|
-
subject.get(
|
41
|
-
get
|
38
|
+
describe 'helpers' do
|
39
|
+
it 'should execute helper' do
|
40
|
+
subject.get('/home', rabl: 'helper') { @user = OpenStruct.new }
|
41
|
+
get '/home'
|
42
42
|
last_response.body.should == "{\"user\":{\"helper\":\"my_helper\"}}"
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
describe
|
46
|
+
describe '#render' do
|
47
47
|
before do
|
48
|
-
subject.get(
|
49
|
-
@user = OpenStruct.new(:
|
50
|
-
render :
|
48
|
+
subject.get('/home', rabl: 'user') do
|
49
|
+
@user = OpenStruct.new(name: 'LTe')
|
50
|
+
render rabl: 'admin'
|
51
51
|
end
|
52
52
|
|
53
|
-
subject.get(
|
54
|
-
@user = OpenStruct.new(:
|
53
|
+
subject.get('/home-detail', rabl: 'user') do
|
54
|
+
@user = OpenStruct.new(name: 'LTe')
|
55
|
+
render rabl: 'admin', locals: { details: 'amazing detail' }
|
56
|
+
end
|
57
|
+
|
58
|
+
subject.get('/about', rabl: 'user') do
|
59
|
+
@user = OpenStruct.new(name: 'LTe')
|
60
|
+
end
|
61
|
+
|
62
|
+
subject.get('/about-detail', rabl: 'user') do
|
63
|
+
@user = OpenStruct.new(name: 'LTe')
|
64
|
+
render locals: { details: 'just a user' }
|
55
65
|
end
|
56
66
|
end
|
57
67
|
|
58
|
-
it
|
59
|
-
get(
|
68
|
+
it 'renders template passed as argument to render method' do
|
69
|
+
get('/home')
|
60
70
|
last_response.body.should == '{"admin":{"name":"LTe"}}'
|
61
71
|
end
|
62
72
|
|
63
|
-
it
|
64
|
-
get(
|
65
|
-
|
73
|
+
it 'renders template passed as argument to render method with locals' do
|
74
|
+
get('/home-detail')
|
75
|
+
last_response.body.should == '{"admin":{"name":"LTe","details":"amazing detail"}}'
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'renders with locals without overriding template' do
|
79
|
+
get('/about-detail')
|
80
|
+
last_response.body.should == '{"user":{"name":"LTe","details":"just a user","project":null}}'
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'does not save rabl options after called #render method' do
|
84
|
+
get('/home')
|
85
|
+
get('/about')
|
66
86
|
last_response.body.should == '{"user":{"name":"LTe","project":null}}'
|
67
87
|
end
|
68
88
|
end
|
69
89
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
last_response.headers["Content-Type"].should == "application/json"
|
90
|
+
it 'should respond with proper content-type' do
|
91
|
+
subject.get('/home', rabl: 'user') {}
|
92
|
+
get('/home')
|
93
|
+
last_response.headers['Content-Type'].should == 'application/json'
|
75
94
|
end
|
76
95
|
|
77
|
-
it
|
78
|
-
subject.get(
|
79
|
-
get
|
80
|
-
last_response.status.should
|
96
|
+
it 'should not raise error about root directory' do
|
97
|
+
subject.get('/home', rabl: 'user') {}
|
98
|
+
get '/home'
|
99
|
+
last_response.status.should eq 200
|
81
100
|
last_response.body.should_not include "Use Rack::Config to set 'api.tilt.root' in config.ru"
|
82
101
|
end
|
83
102
|
|
84
|
-
[
|
103
|
+
['user', 'user.rabl'].each do |rabl_option|
|
85
104
|
it "should render rabl template (#{rabl_option})" do
|
86
|
-
subject.get(
|
87
|
-
@user = OpenStruct.new(:
|
88
|
-
@project = OpenStruct.new(:
|
105
|
+
subject.get('/home', rabl: rabl_option) do
|
106
|
+
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
|
107
|
+
@project = OpenStruct.new(name: 'First')
|
89
108
|
end
|
90
109
|
|
91
|
-
get
|
110
|
+
get '/home'
|
92
111
|
last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}'
|
93
112
|
end
|
94
113
|
end
|
114
|
+
|
115
|
+
describe 'template cache' do
|
116
|
+
before do
|
117
|
+
@views_dir = FileUtils.mkdir_p("#{File.expand_path("..", File.dirname(__FILE__))}/tmp")[0]
|
118
|
+
@template = "#{@views_dir}/user.rabl"
|
119
|
+
FileUtils.cp("#{File.dirname(__FILE__)}/views/user.rabl", @template)
|
120
|
+
subject.before { env['api.tilt.root'] = "#{File.expand_path("..", File.dirname(__FILE__))}/tmp" }
|
121
|
+
subject.get('/home', rabl: 'user') do
|
122
|
+
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
|
123
|
+
@project = OpenStruct.new(name: 'First')
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
after do
|
128
|
+
Grape::Rabl.reset_configuration!
|
129
|
+
FileUtils.rm_r(@views_dir)
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'should serve from cache if cache_template_loading' do
|
133
|
+
Grape::Rabl.configure do |config|
|
134
|
+
config.cache_template_loading = true
|
135
|
+
end
|
136
|
+
get '/home'
|
137
|
+
last_response.status.should be == 200
|
138
|
+
old_response = last_response.body
|
139
|
+
open(@template, 'a') { |f| f << 'node(:test) { "test" }' }
|
140
|
+
get '/home'
|
141
|
+
last_response.status.should be == 200
|
142
|
+
new_response = last_response.body
|
143
|
+
old_response.should == new_response
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'should serve new template if cache_template_loading' do
|
147
|
+
get '/home'
|
148
|
+
last_response.status.should be == 200
|
149
|
+
old_response = last_response.body
|
150
|
+
open(@template, 'a') { |f| f << 'node(:test) { "test" }' }
|
151
|
+
get '/home'
|
152
|
+
last_response.status.should be == 200
|
153
|
+
new_response = last_response.body
|
154
|
+
old_response.should_not == new_response
|
155
|
+
end
|
156
|
+
end
|
95
157
|
end
|
96
158
|
end
|
data/spec/grape_rabl_xml_spec.rb
CHANGED
@@ -14,30 +14,38 @@ describe Grape::Rabl do
|
|
14
14
|
subject
|
15
15
|
end
|
16
16
|
|
17
|
-
context
|
17
|
+
context 'with xml format' do
|
18
18
|
before do
|
19
|
-
subject.before
|
20
|
-
env[
|
21
|
-
env[
|
22
|
-
|
19
|
+
subject.before do
|
20
|
+
env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views"
|
21
|
+
env['api.format'] = :xml
|
22
|
+
end
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
26
|
-
subject.get(
|
27
|
-
get(
|
28
|
-
last_response.headers[
|
25
|
+
it 'should respond with proper content-type' do
|
26
|
+
subject.get('/home', rabl: 'user') {}
|
27
|
+
get('/home')
|
28
|
+
last_response.headers['Content-Type'].should == 'application/xml'
|
29
29
|
end
|
30
30
|
|
31
|
-
[
|
31
|
+
['user', 'user.rabl'].each do |rabl_option|
|
32
32
|
it "should render rabl template (#{rabl_option})" do
|
33
|
-
subject.get(
|
34
|
-
@user = OpenStruct.new(:
|
35
|
-
@project = OpenStruct.new(:
|
33
|
+
subject.get('/home', rabl: rabl_option) do
|
34
|
+
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
|
35
|
+
@project = OpenStruct.new(name: 'First')
|
36
36
|
end
|
37
37
|
|
38
|
-
get
|
39
|
-
|
40
|
-
last_response.body.should == %Q
|
38
|
+
get '/home'
|
39
|
+
|
40
|
+
last_response.body.should == %Q(<?xml version="1.0" encoding="UTF-8"?>
|
41
|
+
<user>
|
42
|
+
<name>LTe</name>
|
43
|
+
<email>email@example.com</email>
|
44
|
+
<project>
|
45
|
+
<name>First</name>
|
46
|
+
</project>
|
47
|
+
</user>
|
48
|
+
)
|
41
49
|
end
|
42
50
|
end
|
43
51
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,21 +1,20 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
-
|
4
|
-
require 'bundler'
|
5
|
-
Bundler.setup :default, :test
|
6
|
-
|
7
|
-
require 'coveralls'
|
8
|
-
Coveralls.wear!
|
9
|
-
|
10
|
-
require 'active_support/core_ext/hash/conversions'
|
11
|
-
require 'grape/rabl'
|
12
|
-
require 'rspec'
|
13
|
-
require 'rack/test'
|
14
|
-
require 'ostruct'
|
15
|
-
|
16
|
-
RSpec.configure do |config|
|
17
|
-
config.include Rack::Test::Methods
|
18
|
-
end
|
19
|
-
|
20
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
21
|
-
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'bundler'
|
5
|
+
Bundler.setup :default, :test
|
6
|
+
|
7
|
+
require 'coveralls'
|
8
|
+
Coveralls.wear!
|
9
|
+
|
10
|
+
require 'active_support/core_ext/hash/conversions'
|
11
|
+
require 'grape/rabl'
|
12
|
+
require 'rspec'
|
13
|
+
require 'rack/test'
|
14
|
+
require 'ostruct'
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
config.include Rack::Test::Methods
|
18
|
+
end
|
19
|
+
|
20
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
data/spec/support/my_helper.rb
CHANGED
data/spec/views/admin.rabl
CHANGED
data/spec/views/user.rabl
CHANGED
metadata
CHANGED
@@ -1,78 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Piotr Niełacny
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-08-26 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'
|
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'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rabl
|
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: i18n
|
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'
|
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'
|
78
69
|
description: Use rabl in grape
|
@@ -85,6 +76,7 @@ files:
|
|
85
76
|
- .coveralls.yml
|
86
77
|
- .gitignore
|
87
78
|
- .rspec
|
79
|
+
- .rubocop.yml
|
88
80
|
- .travis.yml
|
89
81
|
- CHANGELOG.md
|
90
82
|
- Gemfile
|
@@ -93,11 +85,14 @@ files:
|
|
93
85
|
- Rakefile
|
94
86
|
- grape-rabl.gemspec
|
95
87
|
- lib/grape-rabl.rb
|
88
|
+
- lib/grape-rabl/configuration.rb
|
96
89
|
- lib/grape-rabl/formatter.rb
|
97
90
|
- lib/grape-rabl/render.rb
|
98
91
|
- lib/grape-rabl/tilt.rb
|
99
92
|
- lib/grape-rabl/version.rb
|
100
93
|
- lib/grape/rabl.rb
|
94
|
+
- spec/grape_rabl_configuration.rb
|
95
|
+
- spec/grape_rabl_layout_spec.rb
|
101
96
|
- spec/grape_rabl_partials_spec.rb
|
102
97
|
- spec/grape_rabl_spec.rb
|
103
98
|
- spec/grape_rabl_xml_spec.rb
|
@@ -107,36 +102,37 @@ files:
|
|
107
102
|
- spec/views/admin.rabl
|
108
103
|
- spec/views/helper.rabl
|
109
104
|
- spec/views/info.rabl
|
105
|
+
- spec/views/layout_test/layouts/another.rabl
|
106
|
+
- spec/views/layout_test/layouts/application.rabl
|
107
|
+
- spec/views/layout_test/user.rabl
|
110
108
|
- spec/views/project.rabl
|
111
109
|
- spec/views/user.rabl
|
112
110
|
homepage: https://github.com/LTe/grape-rabl
|
113
111
|
licenses: []
|
112
|
+
metadata: {}
|
114
113
|
post_install_message:
|
115
114
|
rdoc_options: []
|
116
115
|
require_paths:
|
117
116
|
- lib
|
118
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
-
none: false
|
120
118
|
requirements:
|
121
|
-
- -
|
119
|
+
- - '>='
|
122
120
|
- !ruby/object:Gem::Version
|
123
121
|
version: 1.9.3
|
124
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
-
none: false
|
126
123
|
requirements:
|
127
|
-
- -
|
124
|
+
- - '>='
|
128
125
|
- !ruby/object:Gem::Version
|
129
126
|
version: '0'
|
130
|
-
segments:
|
131
|
-
- 0
|
132
|
-
hash: -1720124762415562213
|
133
127
|
requirements: []
|
134
128
|
rubyforge_project:
|
135
|
-
rubygems_version: 1.
|
129
|
+
rubygems_version: 2.1.11
|
136
130
|
signing_key:
|
137
|
-
specification_version:
|
131
|
+
specification_version: 4
|
138
132
|
summary: Use rabl in grape
|
139
133
|
test_files:
|
134
|
+
- spec/grape_rabl_configuration.rb
|
135
|
+
- spec/grape_rabl_layout_spec.rb
|
140
136
|
- spec/grape_rabl_partials_spec.rb
|
141
137
|
- spec/grape_rabl_spec.rb
|
142
138
|
- spec/grape_rabl_xml_spec.rb
|
@@ -146,5 +142,8 @@ test_files:
|
|
146
142
|
- spec/views/admin.rabl
|
147
143
|
- spec/views/helper.rabl
|
148
144
|
- spec/views/info.rabl
|
145
|
+
- spec/views/layout_test/layouts/another.rabl
|
146
|
+
- spec/views/layout_test/layouts/application.rabl
|
147
|
+
- spec/views/layout_test/user.rabl
|
149
148
|
- spec/views/project.rabl
|
150
149
|
- spec/views/user.rabl
|