Created
December 22, 2012 15:57
-
-
Save seyhunak/4359591 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::ApplicationController < ApplicationController | |
before_filter :authenticate_user! | |
respond_to :json | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::UsersController < Api::V1::ApplicationController | |
def index | |
@users = User.all | |
logger.info "API v1: Listed all users" | |
end | |
def show | |
@user = User.find_by_id(params[:id]) | |
logger.info "API v1: Listed individual user: #{@user.authentication_token}" | |
end | |
def create | |
if request.format != :json | |
render :status => 406, :json => {:message => "The request must be json"} | |
return | |
end | |
if params[:email].nil? or params[:password].nil? | |
render :status => 400, :json => {:message => "The request must contain the user email and password."} | |
return | |
end | |
@user = User.find_by_email(params[:email]) | |
if @user.nil? | |
@user = User.create(:email => params[:email], :password => params[:password]) | |
logger.info "API v1: Created user id: #{@user.id} successfully" | |
render :status => 200, :json => {:id => @user.authentication_token} | |
end | |
if @user.email and @user.valid_password?(params[:password]) | |
logger.info "API v1: Listed individual user: #{@user.id}" | |
render :status => 200, :json => {:id => @user.authentication_token} | |
else | |
logger.info("User #{@user.id} failed signin, password \"#{params[:password]}\" is invalid") | |
render :status => 401, :json => {:message => "Invalid email or password."} | |
end | |
end | |
def update | |
@user = User.find_by_id(params[:id]) | |
if @user.nil? | |
logger.info("User not found.") | |
render :status => 404, :json => {:message => "Invalid user."} | |
else | |
@user.update_attributes(:name => params[:name], :email => params[:email], :password => params[:password]) | |
logger.info "API v1: Updated user #{@user.id} successfully" | |
render :status => 200, :json => {:email => @user.authentication_token} | |
end | |
end | |
def destroy | |
@user = User.find_by_id(params[:id]) | |
if @user.nil? | |
logger.info("User not found.") | |
render :status => 404, :json => {:message => "Invalid user."} | |
else | |
@user.destroy | |
logger.info "API v1: Destroyed user #{@user.authentication_token} successfully" | |
render :status => 200, :json => {:id => params[:id]} | |
end | |
end | |
def email | |
@user = User.find_by_id(params[:id]) | |
@user.update_attributes(:email => params[:email]) | |
logger.info "API v1: Saved individual user: #{@user.authentication_token}'s email" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment