Grape::Rabl
Use rabl templates in grape!
Installation
Add this line to your application's Gemfile:
gem 'grape-rabl'
gem 'grape', :git => "git://github.com/intridea/grape.git", :branch => "frontier"
And then execute:
$ bundle
Usage
Require grape-rabl
# config.ru
require 'grape/rabl'
Setup view root directory
# config.ru
require 'grape/rabl'
use Rack::Config do |env|
env['api.tilt.root'] = '/path/to/view/root/directory'
end
Create grape application
To get post put delete options add :rabl options with template name.
get "/path", :rabl => "template_name" do
# stuff
@var = "hello"
end
post "/path", :rabl => "template_name_diff" do
# stuff
@user = User.find_user("[email protected]")
end
You can use instance variables in templates!
Template name
You can use "view.rabl" or just "view"
get "/home", :rabl => "view"
get "/home", :rabl => "view.rabl"
Example
# config.ru
require 'grape/rabl'
use Rack::Config do |env|
env['api.tilt.root'] = '/path/to/view/root/directory'
end
class UserAPI < Grape::API
# use rabl with 'hello.rabl' template
get '/user', :rabl => 'hello' do
@user = User.first
end
# do not use rabl, normal usage
get '/user2' do
{ :some => :hash }
end
end
# hello.rabl
object @user => :user
attributes :name
Enjoy :)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request