grape-rabl 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -3
- data/lib/grape-rabl/formatter.rb +11 -1
- data/lib/grape-rabl/version.rb +1 -1
- data/spec/grape_rabl_spec.rb +9 -7
- metadata +8 -8
data/README.md
CHANGED
@@ -41,12 +41,12 @@ end
|
|
41
41
|
To *get post put delete options* add **:rabl** options with template name.
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
get "/path", :rabl => "template_name
|
44
|
+
get "/path", :rabl => "template_name" do
|
45
45
|
# stuff
|
46
46
|
@var = "hello"
|
47
47
|
end
|
48
48
|
|
49
|
-
post "/path", :rabl => "template_name_diff
|
49
|
+
post "/path", :rabl => "template_name_diff" do
|
50
50
|
# stuff
|
51
51
|
@user = User.find_user("email@example.com")
|
52
52
|
end
|
@@ -54,6 +54,15 @@ end
|
|
54
54
|
|
55
55
|
**You can use instance variables in templates!**
|
56
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
|
+
|
57
66
|
### Example
|
58
67
|
|
59
68
|
```ruby
|
@@ -66,7 +75,7 @@ end
|
|
66
75
|
|
67
76
|
class UserAPI < Grape::API
|
68
77
|
# use rabl with 'hello.rabl' template
|
69
|
-
get '/user', :rabl => 'hello
|
78
|
+
get '/user', :rabl => 'hello' do
|
70
79
|
@user = User.first
|
71
80
|
end
|
72
81
|
|
@@ -84,6 +93,7 @@ object @user => :user
|
|
84
93
|
attributes :name
|
85
94
|
```
|
86
95
|
|
96
|
+
|
87
97
|
Enjoy :)
|
88
98
|
|
89
99
|
## Contributing
|
data/lib/grape-rabl/formatter.rb
CHANGED
@@ -11,12 +11,22 @@ module Grape
|
|
11
11
|
current_endpoint = env['api.endpoint']
|
12
12
|
|
13
13
|
rabl(current_endpoint) do |template|
|
14
|
-
engine = ::Tilt.new(
|
14
|
+
engine = ::Tilt.new(view_path(template))
|
15
15
|
rendered = engine.render(current_endpoint, {})
|
16
16
|
Rack::Response.new(rendered, status, headers).to_a
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
private
|
21
|
+
|
22
|
+
def view_path(template)
|
23
|
+
if template.split(".")[-1] == "rabl"
|
24
|
+
File.join(env['api.tilt.root'], template)
|
25
|
+
else
|
26
|
+
File.join(env['api.tilt.root'], (template + ".rabl"))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
20
30
|
def rabl(endpoint)
|
21
31
|
if template = rablable?(endpoint)
|
22
32
|
yield template
|
data/lib/grape-rabl/version.rb
CHANGED
data/spec/grape_rabl_spec.rb
CHANGED
@@ -26,14 +26,16 @@ describe Grape::Rabl do
|
|
26
26
|
lambda{ get "/home" }.should_not raise_error("Use Rack::Config to set 'api.tilt.root' in config.ru")
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
["user", "user.rabl"].each do |rabl_option|
|
30
|
+
it "should render rabl template (#{rabl_option})" do
|
31
|
+
subject.get("/home", :rabl => rabl_option) do
|
32
|
+
@user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
|
33
|
+
@project = OpenStruct.new(:name => "First")
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
36
|
+
get "/home"
|
37
|
+
last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}'
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
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.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rabl
|
16
|
-
requirement: &
|
16
|
+
requirement: &4299880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *4299880
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: tilt
|
27
|
-
requirement: &
|
27
|
+
requirement: &4298300 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *4298300
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: i18n
|
38
|
-
requirement: &
|
38
|
+
requirement: &4294300 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *4294300
|
47
47
|
description: Use rabl in grape
|
48
48
|
email:
|
49
49
|
- piotr.nielacny@gmail.com
|