Skip to content

Instantly share code, notes, and snippets.

@amirrajabi
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajabi/14d2069fb504b7e797b7 to your computer and use it in GitHub Desktop.
Save amirrajabi/14d2069fb504b7e797b7 to your computer and use it in GitHub Desktop.
All about REST API on Ruby on Rails
$ rake routs
----- config/routes.rb
resources :subjects
resources :subjects, except: :destroy
resources :sobjects, only: :index
resources :subjects, only: [:index, :show]
resources :subjects, except: [:destroy, :edit, :update]
---- Bad way
resources :subjects, only: :index
resources :teachers, only: :index
resources :students, only: :index
---- Good way
with_options only: :index do |list_only|
list_only.resources :subjects
list_only.resources :teachers
list_only.resources :students
resorces :episodes
constraints subdomain: 'api' do
resources :subjects
resources :teachers
end
C:\Windows\System32\drivers\etc\hosts => for set a hosting
-------------------------------------------------------- 1
namespace :api, path: '/', constraints: {subdomain: 'api'} do
resources :subjects
resources :students
end
resources :pages
app/controllers/api/subjects_controller.rb
module Api
class SubjectsController < ApplicationController
end
end
config/initializers/inflectos.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
end
app/controllers/pages_controller.rb
class PageController < ApplicationController
end
------------------------------------------------------- end 1
Chapter 2
------------------------------------------------------- 2
namespace :api, path: '/', constraints: {subdomain: 'api'} do
resources :subjects
end
test/integration/listing_subject_test.rb
require 'test_helper'
class ListingSubjectsTest < ActionDispatch::IntegrationTest
setup { host! 'api.example.com'}
test 'returns list of all subjects' do
get '/zombies'
assert_equal 200, response.status
refute_empty response.body
end
end
------------------------------------------------------- end 2
------------------------------------------------------- 3
module API
class SubjectsController < ApplicationController
def index
subjects = Subjects.all
render json: subjects, status: 200
end
end
end
/subjects
/subjects/:id
/subjects?id=1
/subjects?weapon=axe Filters
/subjects?keyword=Amir Searches
/subjects?page2&per_page=25 pagination
module API
class SubjectsController < AplicationController
def index
subjects = Subject.all
if weapon = params[:weapon]
subjects = subjects.where(weapon: weapon)
end
render json: subjects, status: 200
end
def show
subject = Subject.find(params[:id])
render json: subject, status: 200
#render json: subject, status: ok
end
end
end
------------------------------------------------------- end 3
$curl : using for GET testing
$curl http://api.cs-subject-dev.com:3000/subject
------------------------------------------------------- 4
class SubjectsController < ApplicationController
def index
subjects = Subjects.all
respond_to do |format|
format.json { render json: subjects, status: 200 }
format.xml { render xml: subjects, status: 200 }
end
end
end
------------------------------------------------------- end 4
------------------------------------------------------- 5
-if we whant to have a respond
class EpisodesController < ApplicationController
def create
episode = Episode.new(episode_params)
if episode.save
render json: episode, status:201, location: episode
end
end
private
def episode_params
params.require(:episode).permit(:title, :description)
end
end
class EpisodesController < ApplicationController
def create
episode = Episode.new(episode.params)
if episode.save
render nothing: true, status: 204, location: episode
emd
end
...
end
class EpisodesController < ApplicationController
def create
episode = Episode.new(episode.params)
if episode.save
head 204, location: episode
emd
end
...
end
------------------------------------------------------- end 5
------------------------------------------------------- 6
class Episode < ActiveRecord::Base
validates :title, presence: true
end
class EpisodesController < ApplicationController
def create
episode = Episode.new(episode_params)
if episode.save
render json: episode, status: :created, location: episode
else
render json: episode.errors, status: 422
end
end
...
end
------------------------------------------------------- end 6
------------------------------------------------------- 7
module V1
class SubjectsControllers < ApplicationControllers
def
render json: "#{@remote_ip} Version One", status: 200
end
end
end
module V2
class SubjectsControllers < ApplicationControllers
def
render json: "#{@remote_ip} Version Two", status: 200
end
end
end
class ApplicationController < ApplicationController::Base
before_action ->{@remote_ip = request.headers['REMOTE_ADDR']}
end
module V2
class VersionController < ApplicationController
abstract!
before_action :audit_logging_for_v2
def audit_logging_for_v2
end
end
------------------------------------------------------- end 7
AUTH
------------------------------------------------------- 8
class EpisodesController < ApplicationController
before_action :authenticate
def index
episodes = Episode.all
render jsone: episodes, status: 200
end
protected
def authenticate
authenticate_or_request_with_http_basic do |username, password|
User.authenticate(username, password)
end
end
end
------------------------------------------------------- end 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment