Created
November 1, 2012 22:55
-
-
Save aaronmcadam/3997278 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Admin < APIWrapper | |
custom_post :login | |
collection_path 'admins' | |
resource_path 'admins/get/:id' | |
def self.get_admin(superadmin_id, admin_id) | |
get("admins/get/#{superadmin_id}/#{admin_id}") | |
end | |
def self.login(email, password) | |
post_raw('admins/login', | |
{ email: email, password: password }) { |data| parse_data(data) } | |
end | |
end |
This file contains hidden or 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 APIWrapper | |
include Her::Model | |
uses_api $primary_api | |
def self.parse_data(data) | |
if data[:errors] | |
parsed_data = data[:errors] | |
else | |
parsed_data = data[:data] | |
end | |
if parsed_data.is_a?(Array) | |
new_collection(parsed_data) | |
else | |
new(parsed_data) | |
end | |
end | |
end |
This file contains hidden or 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 SessionsController < ApplicationController | |
def new | |
redirect_to projects_path if signed_in? | |
end | |
def create | |
api_response = Admin.login(params[:session][:email].downcase, params[:session][:password]) | |
begin | |
errors = api_response.code | |
rescue NoMethodError | |
errors = nil | |
end | |
if !errors.nil? | |
flash.now[:error] = "Invalid email/password combination" | |
render 'new' | |
else | |
sign_in api_response | |
redirect_back_or projects_path | |
end | |
end | |
def destroy | |
sign_out | |
redirect_to root_url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment