I have some instance methods in a class that must be called in a sequence. The failure of any method in a sequence will require the previous method to be re-called. I'm storing the result of each successful method call in a class variable:
class User
@@auth_hash = {}
def get_auth_token
result = MyApi.get_new_auth_token(self) if @@auth_hash[self]['auth_token'].blank?
if result['Errors']
raise Exception, "You must reauthorize against the external application."
else
@@auth_hash[self]['auth_token'] = result['auth_token']
end
@@auth_hash[self]['auth_token']
end
def get_session_id
result = MyApi.get_new_session_id if @@auth_hash[self]['session_id'].blank?
if result['Errors']
get_auth_token
# Recursion
get_session_id
else
@@auth_hash[self]['session_id'] = result['session_id']
end
@@auth_hash[self]['session_id']
end
end
I would like to get rid of those conditionals, but don't know how to perform the block only if there are errors present in the returned hash.