grape-rabl 0.2.2 → 0.3.0

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 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
@@ -1,10 +1,14 @@
1
1
  rvm:
2
2
  - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.0
3
5
  - ruby-head
4
- - rbx-19mode
5
6
  - jruby-19mode
6
- env:
7
- - JRUBY_OPTS="--1.9"
7
+ - jruby-head
8
+ - rbx-2.2.10
9
+
8
10
  matrix:
9
11
  allow_failures:
12
+ - rvm: jruby-19mode
13
+ - rvm: jruby-head
10
14
  - rvm: ruby-head
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
@@ -8,4 +8,7 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
8
8
  spec.pattern = FileList['spec/**/*_spec.rb']
9
9
  end
10
10
 
11
- task :default => :spec
11
+ require 'rubocop/rake_task'
12
+ Rubocop::RakeTask.new(:rubocop)
13
+
14
+ task default: [:rubocop, :spec]
@@ -0,0 +1,11 @@
1
+ module Grape
2
+ module Rabl
3
+ class Configuration
4
+ attr_accessor :cache_template_loading
5
+
6
+ def initialize
7
+ @cache_template_loading = false
8
+ end
9
+ end
10
+ end
11
+ end
@@ -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 = ::Tilt.new(view_path(template), {format: env['api.format'], view_path: env['api.tilt.root']})
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
- def view_path(template)
30
- if template.split(".")[-1] == "rabl"
31
- File.join(env['api.tilt.root'], template)
32
- else
33
- File.join(env['api.tilt.root'], (template + ".rabl"))
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
- def rablable?
38
- !! endpoint.options[:route_options][:rabl]
39
- end
39
+ def rablable?
40
+ !!endpoint.options[:route_options][:rabl]
41
+ end
40
42
 
41
- def rabl
42
- template = endpoint.options[:route_options][:rabl]
43
- raise "missing rabl template" unless template
44
- set_view_root unless env['api.tilt.root']
45
- yield template
46
- end
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
- def set_view_root
49
- raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module Rabl
3
- VERSION = "0.2.2"
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
data/lib/grape-rabl.rb CHANGED
@@ -1,7 +1,27 @@
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"
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 "Grape::Rabl partials" do
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["api.tilt.root"] = "#{File.dirname(__FILE__)}/views" }
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 "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)
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("/home")
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
@@ -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("/home") {"Hello World"}
20
- get "/home"
19
+ subject.get('/home') { 'Hello World' }
20
+ get '/home'
21
21
  last_response.body.should == "\"Hello World\""
22
22
  end
23
23
 
24
- it "should raise error about root directory" do
24
+ it 'should raise error about root directory' do
25
25
  begin
26
- subject.get("/home", :rabl => true){}
27
- get "/home"
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 "titl root is setup" do
33
+ context 'titl root is setup' do
34
34
  before do
35
- subject.before { env["api.tilt.root"] = "#{File.dirname(__FILE__)}/views" }
35
+ subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" }
36
36
  end
37
37
 
38
- describe "helpers" do
39
- it "should execute helper" do
40
- subject.get("/home", :rabl => "helper") { @user = OpenStruct.new }
41
- get "/home"
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 "#render" do
46
+ describe '#render' do
47
47
  before do
48
- subject.get("/home", :rabl => "user") do
49
- @user = OpenStruct.new(:name => "LTe")
50
- render :rabl => "admin"
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("/about", :rabl => "user") do
54
- @user = OpenStruct.new(:name => "LTe")
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 "renders template passed as argument to reneder method" do
59
- get("/home")
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 "does not save rabl options after called #render method" do
64
- get("/home")
65
- get("/about")
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
- it "should respond with proper content-type" do
72
- subject.get("/home", :rabl => "user"){}
73
- get("/home")
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 "should not raise error about root directory" do
78
- subject.get("/home", :rabl => "user"){}
79
- get "/home"
80
- last_response.status.should == 200
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
- ["user", "user.rabl"].each do |rabl_option|
103
+ ['user', 'user.rabl'].each do |rabl_option|
85
104
  it "should render rabl template (#{rabl_option})" do
86
- subject.get("/home", :rabl => rabl_option) do
87
- @user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
88
- @project = OpenStruct.new(:name => "First")
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 "/home"
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
@@ -14,30 +14,38 @@ describe Grape::Rabl do
14
14
  subject
15
15
  end
16
16
 
17
- context "with xml format" do
17
+ context 'with xml format' do
18
18
  before do
19
- subject.before {
20
- env["api.tilt.root"] = "#{File.dirname(__FILE__)}/views"
21
- env["api.format"] = :xml
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 "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"
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
- ["user", "user.rabl"].each do |rabl_option|
31
+ ['user', 'user.rabl'].each do |rabl_option|
32
32
  it "should render rabl template (#{rabl_option})" do
33
- subject.get("/home", :rabl => rabl_option) do
34
- @user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
35
- @project = OpenStruct.new(:name => "First")
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 "/home"
39
-
40
- last_response.body.should == %Q{<?xml version="1.0" encoding="UTF-8"?>\n<user>\n <name>LTe</name>\n <email>email@example.com</email>\n <project>\n <name>First</name>\n </project>\n</user>\n}
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 }
@@ -1,5 +1,5 @@
1
1
  module MyHelper
2
2
  def my_helper
3
- "my_helper"
3
+ 'my_helper'
4
4
  end
5
- end
5
+ end
@@ -1,2 +1,6 @@
1
1
  object @user => :admin
2
2
  attributes :name
3
+
4
+ node :details, :unless => lambda { |n| locals[:details].nil? } do |m|
5
+ locals[:details]
6
+ end
@@ -0,0 +1,3 @@
1
+ node(:result) do
2
+ JSON.parse(yield)
3
+ end
@@ -0,0 +1,5 @@
1
+ node(:status) { @status }
2
+
3
+ node(:result) do
4
+ JSON.parse(yield)
5
+ end
@@ -0,0 +1,6 @@
1
+ object @user => :user
2
+ attributes :name, :email
3
+
4
+ child @project => :project do
5
+ attributes :name
6
+ end
data/spec/views/user.rabl CHANGED
@@ -4,3 +4,7 @@ attributes :name, :email
4
4
  child @project => :project do
5
5
  attributes :name
6
6
  end
7
+
8
+ node :details, :unless => lambda { |n| locals[:details].nil? } do |m|
9
+ locals[:details]
10
+ end
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.2.2
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: 2013-11-19 00:00:00.000000000 Z
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.8.25
129
+ rubygems_version: 2.1.11
136
130
  signing_key:
137
- specification_version: 3
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