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 +2 -0
- data/Gemfile +12 -12
- data/README.md +164 -146
- data/lib/grape-rabl/formatter.rb +53 -53
- data/lib/grape-rabl/tilt.rb +3 -0
- data/lib/grape-rabl/version.rb +1 -1
- data/lib/grape-rabl.rb +6 -5
- data/spec/grape_rabl_spec.rb +57 -48
- metadata +4 -2
data/.rspec
ADDED
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"
|
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
|
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
|
11
|
-
|
12
|
-
```ruby
|
13
|
-
gem 'grape
|
14
|
-
gem 'grape'
|
15
|
-
```
|
16
|
-
|
17
|
-
And then execute:
|
18
|
-
|
19
|
-
$ bundle
|
20
|
-
|
21
|
-
## Usage
|
22
|
-
|
23
|
-
### Require grape-rabl
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
```
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
You can use
|
60
|
-
|
61
|
-
```ruby
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
```
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
```ruby
|
117
|
-
#
|
118
|
-
class
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
```ruby
|
128
|
-
#
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
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
|
data/lib/grape-rabl/formatter.rb
CHANGED
@@ -1,53 +1,53 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
data/lib/grape-rabl/version.rb
CHANGED
data/lib/grape-rabl.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require "rabl"
|
2
|
-
require "grape"
|
3
|
-
require "hashie/hash"
|
4
|
-
require "grape-rabl/
|
5
|
-
require "grape-rabl/
|
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"
|
data/spec/grape_rabl_spec.rb
CHANGED
@@ -1,48 +1,57 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Grape::Rabl do
|
4
|
-
subject
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
subject.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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.
|
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-
|
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
|