Skip to content

Instantly share code, notes, and snippets.

@StephenRoos
Created April 12, 2016 21:28
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 StephenRoos/8abea5187e3c6891ca61f6efc58d48b4 to your computer and use it in GitHub Desktop.
Save StephenRoos/8abea5187e3c6891ca61f6efc58d48b4 to your computer and use it in GitHub Desktop.
Proposed updates to YNS API
class Api::V1::DivisionsController < Api::V1Controller
def index
options = pagination_options
divisions = Division.search(options)
render_paginated(divisions, options[:page], options[:per_page], divisions.total_pages, divisions.total_count)
end
def show
render_success(current_division)
end
def create
division = company.divisions.build(name: params[:name])
if division.save
render_created(division)
else
render_invalid(division)
end
end
def update
if current_division.update(name: params[:name])
render_success(current_division)
else
render_invalid(current_division)
end
end
def destroy
current_division.destroy
render_success(current_division)
end
private
def current_division
@division ||= company.divisions.find(params[:id])
end
def pagination_options
{
company_id: current_company.id,
page: params[:page] || 1,
per_page: params[:per_page] || 10,
status: params[:status].presence || "all"
}
end
end
class Api::V1Controller < ApiController
skip_before_action :verify_authenticity_token
# if we ever get a RecordNotFound exception (and it's
# not locally rescued), log it and respond with the
# API Not Found response...
rescue_from ActiveRecord::RecordNotFound do |exception|
logger.error(exception.message)
render_not_found(exception.message)
end
# all API actions expect a valid company id. the
# find_by! method will raise a RecordNotFound exception
# if the given company id can't be found...
def company
@company ||= Company.find_by!(careerarc_id: params[:company_id])
end
# renders a 201 Created response with the
# JSON-encoded payload as the body...
def render_created(payload)
render_api_response(:created, payload)
end
# renders a 200 OK response with the
# JSON-encoded payload as the body...
def render_success(payload)
render_api_response(:ok, payload)
end
# renders a 404 Not Found response with the
# JSON-encoded error_payload as the body...
def render_not_found(errors=nil)
render_api_response(:not_found, error_payload(errors))
end
# renders a 422 Unprocessable Entry response with the
# JSON-encoded payload as the body...
def render_invalid(payload)
render_api_response(:unprocessable_entity, payload)
end
# renders a 200 OK response with a standardized
# set of pagination metadata alongside the current
# page of the collection...
def render_paginated(entries, page, per_page, total_pages, total_count)
render_success({
page: page,
per_page: per_page,
total_pages: total_pages,
total_count: total_count,
entries: entries
})
end
private
def render_api_response(status, payload)
render json: payload, status: status
end
def error_payload(payload)
result = {}
if payload.respond_to?(:errors)
result[:errors] = payload.errors
result[:messages] = payload.errors.full_messages if payload.errors.respond_to?(:full_messages)
else
result[:errors] = payload
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment