Creating RESTful API using Grape and RABL in Rails 4
We can create a RESTful API using Grape and RABL in Rails4.
Step 1: Add this GEMS in your Gem file, And do Bundle install.
gem 'grape'
gem 'rabl-rails'
gem 'grape-rabl'
gem 'rack-cors', :require => 'rack/cors'
Step 2: Create a file under lib directory called api.rb, and create a folder inside the view named as api.
# lib/api/v1/root.rb class API < Grape::API
end
Step 3: Add this line to your application.rb file
config.paths.add "app/api", glob: "**/*.rb"
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/api/*)
config.middleware.use Rack::Cors do
allow do
origins '*'
# location of your API
resource '/*', :headers => :any, :methods => [:get, :post, :options, :put, :delete]
end
end
config.middleware.use(Rack::Config) do |env|
env['api.tilt.root'] = Rails.root.join "app", "views", "api"
end
Step 4. Now a days every we have to send and json response. We will use rabl gem for it.Now we will modify our API a bit.
class API < Grape::API
version 'v1', using: :header, vendor: "Application_name"
format :json
formatter :json, Grape::Formatter::Rabl
end
Step 5. Now we have to mount our api in the routes.
mount API => "/api", :at => '/'
Step 6. Now we can create a sample api
desc "Sample Api"
get '/all_users' , :rabl => 'all_users' do
@users = User.all
end
Step 7. For the Api response we have to create a rabl file for this case all_users.rabl inside the app/api folder
Let say our user table has id, username and email then in the all_users.rabl file we will have to write.
object @users
attributes :id,
:username,
Step 8. Now we can call the api from console.
curl http://localhost:3000/api/all_users
Step 9. Now we can add some basic http authentication if we want.
http_basic do |email, password| user = User.find_by_email(email) user && user.valid_password?(password) end
And we can call the api from the console.
curl http://localhost:3000/api/users/all_users -u "user:secret".
Step 10: Create API which will accept parameter.
let just say we want to pass a parameter to our api like id
get '/:id', :rabl => 'user' do
@requests = User.where('id =?', params[:id]).first
end
Now call it like
curl http://localhost:3000/api/users/10 -u "user:secret".
Step 11: You want to pass number of random parameter to a api then make the api call like this. Which may not be a REST approach but some times we need that.
curl "http://localhost:3000/api/users/get_user?dob=yyyy-mm-dd&id=10"
get '/get_user', :rabl => 'user' do
@requests = User.where('dob =? AND id =?', params[:dob], params[:id]).first
end
So Finally our Api looks Like.
class API < Grape::API
version 'v1', using: :header, vendor: "app_name"
format :json
formatter :json, Grape::Formatter::Rabl
http_basic do |uname, password|
user = User.find_by_username(uname)
user && user.valid_password?(password)
end
helpers do
end
resource :users do
get '/all_users' , :rabl => 'all_users' do
@users = User.all
end
get '/:id', :rabl => 'user' do
@requests = User.where('id =?', params[:id]).first
end
get '/get_user', :rabl => 'user' do
@requests = User.where('dob =? AND id =?', params[:dob], params[:id]).first
end
end
end
I will discuss on rabl in my next blog post.
For all the gem documentation please check the below links.
https://github.com/intridea/grape
https://github.com/LTe/grape-rabl
https://github.com/ccocchi/rabl-rails
Great work. Add some more info about how to send parameter in api call.
ReplyDeleteYep I will update it soon.
DeleteCheck the updated Blog post.
Delete