Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use rabl with grape

License

NotificationsYou must be signed in to change notification settings

ruby-grape/grape-rabl

Repository files navigation

UseRabl templates inGrape!

Gem VersionBuild StatusCode ClimateCoverage Status

Installation

Add thegrape andgrape-rabl gems to Gemfile.

gem'grape'gem'grape-rabl'

And then execute:

$ bundle

Upgrading

SeeUPGRADING.

Usage

Setup view root directory

# config.ruuseRack::Configdo |env|env['api.tilt.root']='/path/to/view/root/directory'end

Tell your API to use Grape::Formatter::Rabl

classAPI <Grape::APIformat:jsonformatter:json,Grape::Formatter::Rablend

Use rabl templates conditionally

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

Use rabl layout

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

Enable template caching

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

You can omit .rabl

The following are identical.

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

Example

# 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

Usage with rails

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

Specs

See"Writing Tests" ingrape README.

Enjoy :)

Contributing

SeeCONTRIBUTING.

About

Use rabl with grape

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors13

Languages


[8]ページ先頭

©2009-2025 Movatter.jp