Giter Club home page Giter Club logo

grape-jbuilder's Introduction

Grape::Jbuilder

Use Jbuilder templates in Grape!

This gem is completely based on grape-rabl.

Build Status

Installation

Add this line to your application's Gemfile:

gem 'grape-jbuilder'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grape-jbuilder

Usage

Require grape-jbuilder

# config.ru
require 'grape/jbuilder'

Setup view root directory

# config.ru
require 'grape/jbuilder'

use Rack::Config do |env|
  env['api.tilt.root'] = '/path/to/view/root/directory'
end

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

class API < Grape::API
  format :json
  formatter :json, Grape::Formatter::Jbuilder
end

Use Jbuilder templates conditionally

Add the template name to the API options.

get '/user/:id', jbuilder: 'user.jbuilder' do
  @user = User.find(params[:id])
end

You can use instance variables in the Jbuilder template.

json.user do
  json.(@user, :name, :email)
  json.project do
    json.(@project, :name)
  end
end

Use Jbuilder templates dynamically

get ':id' do
  user = User.find_by(id: params[:id])
  if article
    env['api.tilt.template'] = 'users/show'
    env['api.tilt.locals']   = {user: user}
  else
    status 404
    {'status' => 'Not Found', 'errors' => "User id '#{params[:id]}' is not found."}
  end
end

You can omit .jbuilder

The following are identical.

get '/home', jbuilder: 'view'
get '/home', jbuilder: 'view.jbuilder'

Example

# config.ru
require 'grape/jbuilder'

use Rack::Config do |env|
  env['api.tilt.root'] = '/path/to/view/root/directory'
end

class UserAPI < Grape::API
  format :json
  formatter :json, Grape::Formatter::Jbuilder

  # use Jbuilder with 'user.jbuilder' template
  get '/user/:id', jbuilder: 'user' do
    @user = User.find(params[:id])
  end

  # do not use jbuilder, fallback to the defalt Grape JSON formatter
  get '/users' do
    User.all
  end
end
# user.jbuilder
json.user do
  json.(@user, :name)
end

Usage with Rails

Create a grape application.

# app/api/user.rb
class MyAPI < Grape::API
  format :json
  formatter :json, Grape::Formatter::Jbuilder
  get '/user/:id', jbuilder: 'user' do
    @user = User.find(params[:id])
  end
end
# app/views/api/user.jbuilder
json.user do
  json.(@user, :name)
end

Edit your config/application.rb and add view path

# application.rb
class Application < Rails::Application
  config.middleware.use(Rack::Config) do |env|
    env['api.tilt.root'] = Rails.root.join 'app', 'views', 'api'
  end
end

Mount application to Rails router

# routes.rb
GrapeExampleRails::Application.routes.draw do
  mount MyAPI , at: '/api'
end

Rspec

See "Writing Tests" in https://github.com/intridea/grape.

Enjoy :)

Special Thanks

Special thanks to @LTe because this gem is completely based on grape-rabl.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

grape-jbuilder's People

Contributors

pat avatar milkcocoa avatar morshedalam avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.