Created
June 5, 2012 19:01
-
-
Save lbergen/2877002 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
authkey.rb | |
authenticate!(key) | |
if success | |
response = [:status=>"success", :remaining_requests=>499] | |
else | |
response = [:status=>"failure", :remaining_requests=>499, :errors => [something]] | |
end | |
end | |
application_controller.rb | |
before_filter :authenticate_key! | |
def current_key | |
unless @auth_key | |
if session[:token].present? | |
@auth_key = AuthKey.find_by_token(session[:token]) | |
end | |
end | |
return @auth_key | |
end | |
def authenticate_key! | |
if current_key | |
number_of_requests_remaining = current_key.requests_remaining | |
header.write(number_of_requests_remaining) | |
unless current_key.is_active? | |
render_error(:message => "key is not active", :status => 300) | |
end | |
unless current_key.can?(params[:action]) | |
render_error(:message => "not allowed to perform that action", :status => 300) | |
end | |
unless current_key.check_rate_limit | |
render_error(:message => "Rate limit exceeded", :status => 300) | |
end | |
else | |
render_error(:message => "key not found", :status => 300) | |
end | |
end | |
def render_error(error, status) | |
render some_json_builder(:message => error, :status => status) | |
end | |
end | |
request_count = current_key.auth_requests.where(:created_at.gte => Authkey::THROTTLE_AGE.hours.ago).count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment