Skip to content

Commit

Permalink
Template without .rabl. Add specs. Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
LTe committed Jan 11, 2012
1 parent c79c9a3 commit cecca03
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,28 @@ end
To *get post put delete options* add **:rabl** options with template name.

```ruby
get "/path", :rabl => "template_name.rabl" do
get "/path", :rabl => "template_name" do
# stuff
@var = "hello"
end

post "/path", :rabl => "template_name_diff.rabl" do
post "/path", :rabl => "template_name_diff" do
# stuff
@user = User.find_user("email@example.com")
end
```

**You can use instance variables in templates!**

## Template name

You can use "**view.rabl**" or just "**view**"

```ruby
get "/home", :rabl => "view"
get "/home", :rabl => "view.rabl"
```

### Example

```ruby
Expand All @@ -66,7 +75,7 @@ end

class UserAPI < Grape::API
# use rabl with 'hello.rabl' template
get '/user', :rabl => 'hello.rabl' do
get '/user', :rabl => 'hello' do
@user = User.first
end

Expand All @@ -84,6 +93,7 @@ object @user => :user
attributes :name
```


Enjoy :)

## Contributing
Expand Down
12 changes: 11 additions & 1 deletion lib/grape-rabl/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ def after
current_endpoint = env['api.endpoint']

rabl(current_endpoint) do |template|
engine = ::Tilt.new(File.join(env['api.tilt.root'], template))
engine = ::Tilt.new(view_path(template))
rendered = engine.render(current_endpoint, {})
Rack::Response.new(rendered, status, headers).to_a
end
end

private

def view_path(template)
if template.split(".")[-1] == "rabl"
File.join(env['api.tilt.root'], template)
else
File.join(env['api.tilt.root'], (template + ".rabl"))
end
end

def rabl(endpoint)
if template = rablable?(endpoint)
yield template
Expand Down
16 changes: 9 additions & 7 deletions spec/grape_rabl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ def app; subject end
lambda{ get "/home" }.should_not raise_error("Use Rack::Config to set 'api.tilt.root' in config.ru")
end

it "should render rabl template" do
subject.get("/home", :rabl => "user.rabl") do
@user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
@project = OpenStruct.new(:name => "First")
end
["user", "user.rabl"].each do |rabl_option|
it "should render rabl template (#{rabl_option})" do
subject.get("/home", :rabl => rabl_option) do
@user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
@project = OpenStruct.new(:name => "First")
end

get "/home"
last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}'
get "/home"
last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}'
end
end
end
end

0 comments on commit cecca03

Please sign in to comment.