grape-rabl 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/Gemfile CHANGED
@@ -1,12 +1,12 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in grape-rabl.gemspec
4
- gemspec
5
-
6
- gem "grape", :git => "git://github.com/intridea/grape.git", :branch => "frontier"
7
-
8
- group :test do
9
- gem "rspec", "~> 2.8.0"
10
- gem "rack-test"
11
- gem "rake"
12
- end
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grape-rabl.gemspec
4
+ gemspec
5
+
6
+ gem "grape", :git => "git://github.com/intridea/grape.git"
7
+
8
+ group :test do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rack-test"
11
+ gem "rake"
12
+ end
data/README.md CHANGED
@@ -1,146 +1,164 @@
1
- # Grape::Rabl
2
-
3
- Use rabl templates in grape!
4
-
5
- [![Build Status](https://secure.travis-ci.org/LTe/grape-rabl.png)](http://travis-ci.org/LTe/grape-rabl) [![Dependency Status](https://gemnasium.com/LTe/grape-rabl.png)](https://gemnasium.com/LTe/grape-rabl)
6
-
7
-
8
- ## Installation
9
-
10
- Add this line to your application's Gemfile:
11
-
12
- ```ruby
13
- gem 'grape-rabl'
14
- gem 'grape', :git => "git://github.com/intridea/grape.git", :branch => "frontier"
15
- ```
16
-
17
- And then execute:
18
-
19
- $ bundle
20
-
21
- ## Usage
22
-
23
- ### Require grape-rabl
24
- ```ruby
25
- # config.ru
26
- require 'grape/rabl'
27
- ```
28
-
29
- ### Setup view root directory
30
- ```ruby
31
- # config.ru
32
- require 'grape/rabl'
33
-
34
- use Rack::Config do |env|
35
- env['api.tilt.root'] = '/path/to/view/root/directory'
36
- end
37
- ```
38
-
39
- ### Create grape application
40
-
41
- To *get post put delete options* add **:rabl** options with template name.
42
-
43
- ```ruby
44
- get "/path", :rabl => "template_name" do
45
- # stuff
46
- @var = "hello"
47
- end
48
-
49
- post "/path", :rabl => "template_name_diff" do
50
- # stuff
51
- @user = User.find_user("email@example.com")
52
- end
53
- ```
54
-
55
- **You can use instance variables in templates!**
56
-
57
- ## Template name
58
-
59
- You can use "**view.rabl**" or just "**view**"
60
-
61
- ```ruby
62
- get "/home", :rabl => "view"
63
- get "/home", :rabl => "view.rabl"
64
- ```
65
-
66
- ### Example
67
-
68
- ```ruby
69
- # config.ru
70
- require 'grape/rabl'
71
-
72
- use Rack::Config do |env|
73
- env['api.tilt.root'] = '/path/to/view/root/directory'
74
- end
75
-
76
- class UserAPI < Grape::API
77
- # use rabl with 'hello.rabl' template
78
- get '/user', :rabl => 'hello' do
79
- @user = User.first
80
- end
81
-
82
- # do not use rabl, normal usage
83
- get '/user2' do
84
- { :some => :hash }
85
- end
86
- end
87
- ```
88
-
89
- ```ruby
90
- # hello.rabl
91
- object @user => :user
92
-
93
- attributes :name
94
- ```
95
-
96
- ## Usage with rails
97
-
98
- Create grape application
99
-
100
- ```ruby
101
- # app/api/user.rb
102
- class MyAPI < Grape::API
103
- get '/user', :rabl => "user" do
104
- @user = User.first
105
- end
106
- end
107
- ```
108
-
109
- ```ruby
110
- # app/views/api/user.rabl
111
- object @user => :user
112
- ```
113
-
114
- Edit your **config/application.rb** and add view path
115
-
116
- ```ruby
117
- # application.rb
118
- class Application < Rails::Application
119
- config.middleware.use(Rack::Config) do |env|
120
- env['api.tilt.root'] = Rails.root.join "app", "views", "api"
121
- end
122
- end
123
- ```
124
-
125
- Mount application to rails router
126
-
127
- ```ruby
128
- # routes.rb
129
- GrapeExampleRails::Application.routes.draw do
130
- mount MyAPI , :at => "/api"
131
- end
132
- ```
133
-
134
- ## Rspec
135
-
136
- Writing Tests part at https://github.com/intridea/grape
137
-
138
- Enjoy :)
139
-
140
- ## Contributing
141
-
142
- 1. Fork it
143
- 2. Create your feature branch (`git checkout -b my-new-feature`)
144
- 3. Commit your changes (`git commit -am 'Added some feature'`)
145
- 4. Push to the branch (`git push origin my-new-feature`)
146
- 5. Create new Pull Request
1
+ # Grape::Rabl
2
+
3
+ Use Rabl templates in [Grape](https://github.com/intridea/grape)!
4
+
5
+ [![Build Status](https://secure.travis-ci.org/LTe/grape-rabl.png)](http://travis-ci.org/LTe/grape-rabl) [![Dependency Status](https://gemnasium.com/LTe/grape-rabl.png)](https://gemnasium.com/LTe/grape-rabl)
6
+
7
+
8
+ ## Installation
9
+
10
+ Add the `grape` and `grape-rabl` gems to Gemfile. Currently requires HEAD of Grape.
11
+
12
+ ```ruby
13
+ gem 'grape', :git => "https://github.com/intridea/grape.git"
14
+ gem 'grape-rabl'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ ### Require grape-rabl
24
+
25
+ ```ruby
26
+ # config.ru
27
+ require 'grape/rabl'
28
+ ```
29
+
30
+ ### Setup view root directory
31
+ ```ruby
32
+ # config.ru
33
+ require 'grape/rabl'
34
+
35
+ use Rack::Config do |env|
36
+ env['api.tilt.root'] = '/path/to/view/root/directory'
37
+ end
38
+ ```
39
+
40
+ ### Tell your API to use Grape::Formatter::Rabl
41
+
42
+ ```ruby
43
+ class API < Grape::API
44
+ format :json
45
+ formatter :json, Grape::Formatter::Rabl
46
+ end
47
+ ```
48
+
49
+ ### Use rabl templates conditionally
50
+
51
+ Add the template name to the API options.
52
+
53
+ ```ruby
54
+ get "/user/:id", :rabl => "user.rabl" do
55
+ @user = User.find(params[:id])
56
+ end
57
+ ```
58
+
59
+ You can use instance variables in the Rabl template.
60
+
61
+ ```ruby
62
+ object @user => :user
63
+ attributes :name, :email
64
+
65
+ child @project => :project do
66
+ attributes :name
67
+ end
68
+ ```
69
+
70
+ ## You can omit .rabl
71
+
72
+ The following are identical.
73
+
74
+ ```ruby
75
+ get "/home", :rabl => "view"
76
+ get "/home", :rabl => "view.rabl"
77
+ ```
78
+
79
+ ### Example
80
+
81
+ ```ruby
82
+ # config.ru
83
+ require 'grape/rabl'
84
+
85
+ use Rack::Config do |env|
86
+ env['api.tilt.root'] = '/path/to/view/root/directory'
87
+ end
88
+
89
+ class UserAPI < Grape::API
90
+ format :json
91
+ formatter :json, Grape::Formatter::Rabl
92
+
93
+ # use rabl with 'user.rabl' template
94
+ get '/user/:id', :rabl => 'user' do
95
+ @user = User.find(params[:id])
96
+ end
97
+
98
+ # do not use rabl, fallback to the defalt Grape JSON formatter
99
+ get '/users' do
100
+ User.all
101
+ end
102
+ end
103
+ ```
104
+
105
+ ```ruby
106
+ # user.rabl
107
+ object @user => :user
108
+
109
+ attributes :name
110
+ ```
111
+
112
+ ## Usage with rails
113
+
114
+ Create grape application
115
+
116
+ ```ruby
117
+ # app/api/user.rb
118
+ class MyAPI < Grape::API
119
+ format :json
120
+ formatter :json, Grape::Formatter::Rabl
121
+ get '/user/:id', :rabl => "user" do
122
+ @user = User.find(params[:id])
123
+ end
124
+ end
125
+ ```
126
+
127
+ ```ruby
128
+ # app/views/api/user.rabl
129
+ object @user => :user
130
+ ```
131
+
132
+ Edit your **config/application.rb** and add view path
133
+
134
+ ```ruby
135
+ # application.rb
136
+ class Application < Rails::Application
137
+ config.middleware.use(Rack::Config) do |env|
138
+ env['api.tilt.root'] = Rails.root.join "app", "views", "api"
139
+ end
140
+ end
141
+ ```
142
+
143
+ Mount application to rails router
144
+
145
+ ```ruby
146
+ # routes.rb
147
+ GrapeExampleRails::Application.routes.draw do
148
+ mount MyAPI , :at => "/api"
149
+ end
150
+ ```
151
+
152
+ ## Rspec
153
+
154
+ See "Writing Tests" in https://github.com/intridea/grape.
155
+
156
+ Enjoy :)
157
+
158
+ ## Contributing
159
+
160
+ 1. Fork it
161
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
162
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
163
+ 4. Push to the branch (`git push origin my-new-feature`)
164
+ 5. Create new Pull Request
@@ -1,53 +1,53 @@
1
- require 'tilt'
2
- Rabl.register!
3
-
4
- module Grape
5
- module Middleware
6
- class Formatter
7
- alias :old_after :after
8
-
9
- def after
10
- status, headers, bodies = *@app_response
11
- current_endpoint = env['api.endpoint']
12
-
13
- rabl(current_endpoint) do |template|
14
- engine = ::Tilt.new(view_path(template))
15
- rendered = engine.render(current_endpoint, {})
16
- headers['Content-Type'] = content_types[env['api.format']]
17
- Rack::Response.new(rendered, status, headers).to_a
18
- end
19
- end
20
-
21
- private
22
-
23
- def view_path(template)
24
- if template.split(".")[-1] == "rabl"
25
- File.join(env['api.tilt.root'], template)
26
- else
27
- File.join(env['api.tilt.root'], (template + ".rabl"))
28
- end
29
- end
30
-
31
- def rabl(endpoint)
32
- if template = rablable?(endpoint)
33
- yield template
34
- else
35
- old_after
36
- end
37
- end
38
-
39
- def rablable?(endpoint)
40
- if template = endpoint.options[:route_options][:rabl]
41
- set_view_root unless env['api.tilt.root']
42
- template
43
- else
44
- false
45
- end
46
- end
47
-
48
- def set_view_root
49
- raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
50
- end
51
- end
52
- end
53
- end
1
+ module Grape
2
+ module Formatter
3
+ module Rabl
4
+ class << self
5
+
6
+ attr_reader :env
7
+ attr_reader :endpoint
8
+
9
+ def call(object, env)
10
+
11
+ @env = env
12
+ @endpoint = env['api.endpoint']
13
+
14
+ if rablable?
15
+ rabl do |template|
16
+ engine = ::Tilt.new(view_path(template))
17
+ engine.render endpoint, {}
18
+ end
19
+ else
20
+ Grape::Formatter::Json.call object, env
21
+ end
22
+
23
+ end
24
+
25
+ private
26
+
27
+ def view_path(template)
28
+ if template.split(".")[-1] == "rabl"
29
+ File.join(env['api.tilt.root'], template)
30
+ else
31
+ File.join(env['api.tilt.root'], (template + ".rabl"))
32
+ end
33
+ end
34
+
35
+ def rablable?
36
+ !! endpoint.options[:route_options][:rabl]
37
+ end
38
+
39
+ def rabl
40
+ template = endpoint.options[:route_options][:rabl]
41
+ raise "missing rabl template" unless template
42
+ set_view_root unless env['api.tilt.root']
43
+ yield template
44
+ end
45
+
46
+ def set_view_root
47
+ raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
48
+ end
49
+
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ require 'tilt'
2
+
3
+ Rabl.register!
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module Rabl
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
data/lib/grape-rabl.rb CHANGED
@@ -1,5 +1,6 @@
1
- require "rabl"
2
- require "grape"
3
- require "hashie/hash"
4
- require "grape-rabl/version"
5
- require "grape-rabl/formatter"
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"
@@ -1,48 +1,57 @@
1
- require 'spec_helper'
2
-
3
- describe Grape::Rabl do
4
- subject { Class.new(Grape::API) }
5
- before { subject.default_format :json }
6
- def app; subject end
7
-
8
- it 'should work without rabl template' do
9
- subject.get("/home") {"Hello World"}
10
- get "/home"
11
- last_response.body.should == "Hello World"
12
- end
13
-
14
- it "should raise error about root directory" do
15
- subject.get("/home", :rabl => true){}
16
- lambda{ get "/home" }.should raise_error("Use Rack::Config to set 'api.tilt.root' in config.ru")
17
- end
18
-
19
-
20
- context "titl root is setup" do
21
- before do
22
- subject.before { env["api.tilt.root"] = "#{File.dirname(__FILE__)}/views" }
23
- end
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/json"
29
- end
30
-
31
- it "should not raise error about root directory" do
32
- subject.get("/home", :rabl => true){}
33
- lambda{ get "/home" }.should_not raise_error("Use Rack::Config to set 'api.tilt.root' in config.ru")
34
- end
35
-
36
- ["user", "user.rabl"].each do |rabl_option|
37
- it "should render rabl template (#{rabl_option})" do
38
- subject.get("/home", :rabl => rabl_option) do
39
- @user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
40
- @project = OpenStruct.new(:name => "First")
41
- end
42
-
43
- get "/home"
44
- last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}'
45
- end
46
- end
47
- end
48
- end
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Rabl 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
+ end
12
+
13
+ def app
14
+ subject
15
+ end
16
+
17
+ it 'should work without rabl 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", :rabl => true){}
25
+ lambda{ get "/home" }.should raise_error("Use Rack::Config to set 'api.tilt.root' in config.ru")
26
+ end
27
+
28
+
29
+ context "titl root is setup" do
30
+ before do
31
+ subject.before { env["api.tilt.root"] = "#{File.dirname(__FILE__)}/views" }
32
+ end
33
+
34
+ it "should respond with proper content-type" do
35
+ subject.get("/home", :rabl => "user"){}
36
+ get("/home")
37
+ last_response.headers["Content-Type"].should == "application/json"
38
+ end
39
+
40
+ it "should not raise error about root directory" do
41
+ subject.get("/home", :rabl => true){}
42
+ lambda{ get "/home" }.should_not raise_error("Use Rack::Config to set 'api.tilt.root' in config.ru")
43
+ end
44
+
45
+ ["user", "user.rabl"].each do |rabl_option|
46
+ it "should render rabl template (#{rabl_option})" do
47
+ subject.get("/home", :rabl => rabl_option) do
48
+ @user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
49
+ @project = OpenStruct.new(:name => "First")
50
+ end
51
+
52
+ get "/home"
53
+ last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}'
54
+ end
55
+ end
56
+ end
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-rabl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-03 00:00:00.000000000 Z
12
+ date: 2012-12-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rabl
@@ -67,6 +67,7 @@ extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
69
  - .gitignore
70
+ - .rspec
70
71
  - .travis.yml
71
72
  - Gemfile
72
73
  - LICENSE
@@ -75,6 +76,7 @@ files:
75
76
  - grape-rabl.gemspec
76
77
  - lib/grape-rabl.rb
77
78
  - lib/grape-rabl/formatter.rb
79
+ - lib/grape-rabl/tilt.rb
78
80
  - lib/grape-rabl/version.rb
79
81
  - lib/grape/rabl.rb
80
82
  - spec/grape_rabl_spec.rb