- Notifications
You must be signed in to change notification settings - Fork27
ruby-grape/grape-rabl
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Add thegrape
andgrape-rabl
gems to Gemfile.
gem'grape'gem'grape-rabl'
And then execute:
$ bundle
SeeUPGRADING.
# config.ruuseRack::Configdo |env|env['api.tilt.root']='/path/to/view/root/directory'end
classAPI <Grape::APIformat:jsonformatter:json,Grape::Formatter::Rablend
Add the template name to the API options.
get"/user/:id",:rabl=>"user.rabl"do@user=User.find(params[:id])end
You can use instance variables in the Rabl template.
object@user=>:userattributes:name,:emailchild@project=>:projectdoattributes:nameend
Gape-rabl first looks for a layout file in#{env['api.tilt.root']}/layouts/application.rabl
.
You can override the default layout conventions:
# config.ruuseRack::Configdo |env|env['api.tilt.root']='/path/to/view/root/directory'env['api.tilt.layout']='layouts/another'end
Grape-rabl allows for template caching after templates are loaded initially.
You can enable template caching:
# config.ruGrape::Rabl.configuredo |config|config.cache_template_loading=true# default: falseend
The following are identical.
get"/home",:rabl=>"view"get"/home",:rabl=>"view.rabl"
# config.ruuseRack::Configdo |env|env['api.tilt.root']='/path/to/view/root/directory'endclassUserAPI <Grape::APIformat:jsonformatter:json,Grape::Formatter::Rablget'/user/:id'do@user=User.find(params[:id])# use rabl with 'user.rabl' or 'admin.rabl' templateif@user.admin?# pass locals with the #render methodrenderrabl:'admin',locals:{details:'this user is an admin'}elserenderrabl:'user'endendget'/admin/:id',:rabl=>'admin'do@user=User.find(params[:id])# use rabl with 'super_admin.rabl'renderrabl:'super_admin'if@user.super_admin?# when render method has not been used use template from endpoint definitionend# use rabl with 'user_history.rabl' templateget'/user/:id/history',:rabl=>'user_history'do@history=User.find(params[:id]).historyend# do not use rabl, fallback to the default Grape response formatterget'/users'doUser.allendend
# user.rablobject@user=>:userattributes:name
Create grape application
# app/api/user.rbclassMyAPI <Grape::APIformat:jsonformatter:json,Grape::Formatter::Rablget'/user/:id',:rabl=>"user"do@user=User.find(params[:id])endend
# app/views/api/user.rablobject@user=>:user
Edit yourconfig/application.rb and add view path
# application.rbclassApplication <Rails::Applicationconfig.middleware.use(Rack::Config)do |env|env['api.tilt.root']=Rails.root.join"app","views","api"endend
Mount application to rails router
# routes.rbGrapeExampleRails::Application.routes.drawdomountMyAPI,:at=>"/api"end
See"Writing Tests" ingrape README.
Enjoy :)
SeeCONTRIBUTING.
About
Use rabl with grape
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors13
Uh oh!
There was an error while loading.Please reload this page.