2
\$\begingroup\$

Can anyone think of a way to DRY up this code or is there a way to make it more efficient or quicker. Its what authenticates a user with my API and i want to make it as fast as possible.

def authenticate!
 authenticate_or_request_with_http_basic do |access_key, secret_key|
 @current_app = App.find_by_access_key(access_key)
 @current_app.secret_key == secret_key ? true : false
 end
end
tokland
11.2k1 gold badge21 silver badges26 bronze badges
asked Jul 24, 2013 at 14:59
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

There is not much to say, just two notes:

  • Common mistake of redundant boolean check: some_boolean ? true : false -> some_boolean.

  • You can either use the bang find method or check the returned value, but not just use without a check.

So simply:

def authenticate!
 authenticate_or_request_with_http_basic do |access_key, secret_key|
 @current_app = App.find_by_access_key(access_key)
 @current_app && @current_app.secret_key == secret_key
 end
end
answered Jul 24, 2013 at 15:35
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.