Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Use Jbuilder templates in Grape!
This gem is completely based on grape-rabl.
Add this line to your application's Gemfile:
gem 'grape-jbuilder'
And then execute:
$ bundle
Or install it yourself as:
$ gem install grape-jbuilder
# config.ru
require 'grape/jbuilder'
# config.ru
require 'grape/jbuilder'
use Rack::Config do |env|
env['api.tilt.root'] = '/path/to/view/root/directory'
end
class API < Grape::API
format :json
formatter :json, Grape::Formatter::Jbuilder
end
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
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
The following are identical.
get '/home', jbuilder: 'view'
get '/home', jbuilder: 'view.jbuilder'
# 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
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
See "Writing Tests" in https://github.com/intridea/grape.
Enjoy :)
Special thanks to @LTe because this gem is completely based on grape-rabl.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that grape-jbuilder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.