module KnockRails3::Authenticable
Public Instance Methods
authenticate_for(entity_class)
click to toggle source
# File lib/knock_rails3/authenticable.rb, line 2 def authenticate_for entity_class getter_name = "current_api_#{entity_class.to_s.parameterize.underscore}" define_current_entity_getter(entity_class, getter_name) public_send(getter_name) end
Private Instance Methods
authenticate_entity(entity_name)
click to toggle source
# File lib/knock_rails3/authenticable.rb, line 26 def authenticate_entity(entity_name) if token entity_class = entity_name.camelize.constantize send(:authenticate_for, entity_class) end end
define_current_entity_getter(entity_class, getter_name)
click to toggle source
# File lib/knock_rails3/authenticable.rb, line 43 def define_current_entity_getter entity_class, getter_name unless self.respond_to?(getter_name) memoization_var_name = "@_#{getter_name}" self.class.send(:define_method, getter_name) do unless instance_variable_defined?(memoization_var_name) current = begin KnockRails3::AuthToken.new(token: token).entity_for(entity_class) rescue KnockRails3.not_found_exception_class, JWT::DecodeError nil end instance_variable_set(memoization_var_name, current) end instance_variable_get(memoization_var_name) end end end
method_missing(method, *args)
click to toggle source
Calls superclass method
# File lib/knock_rails3/authenticable.rb, line 14 def method_missing(method, *args) prefix, api_prefix, entity_name = method.to_s.split('_', 3) case prefix when 'authenticate' unauthorized_entity(entity_name) unless authenticate_entity(entity_name) when 'current' authenticate_entity(entity_name) else super end end
token()
click to toggle source
# File lib/knock_rails3/authenticable.rb, line 10 def token params[:token] || token_from_request_headers end
token_from_request_headers()
click to toggle source
# File lib/knock_rails3/authenticable.rb, line 37 def token_from_request_headers unless request.headers['Authorization'].nil? request.headers['Authorization'].split.last end end