module Firebase::Auth::Authenticable

Public Instance Methods

authenticate_for(entity_class) click to toggle source
# File lib/firebase/auth/authenticable.rb, line 4
def authenticate_for entity_class
  getter_name = "current_#{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/firebase/auth/authenticable.rb, line 28
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/firebase/auth/authenticable.rb, line 46
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)
        # FIXME: error handling
        firebase_auth_token = Firebase::Auth::Token.new(token: token)
        current =
        begin
          firebase_auth_token.entity_for(entity_class)
        rescue
          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/firebase/auth/authenticable.rb, line 16
def method_missing(method, *args)
  prefix, entity_name = method.to_s.split('_', 2)
  case prefix
  when 'authenticate'
    unauthorized_entity unless authenticate_entity(entity_name)
  when 'current'
    authenticate_entity(entity_name)
  else
    super
  end
end
token() click to toggle source
# File lib/firebase/auth/authenticable.rb, line 12
def token
  params[:token] || token_from_request_headers
end
token_from_request_headers() click to toggle source
# File lib/firebase/auth/authenticable.rb, line 39
def token_from_request_headers
  unless request.headers['Authorization'].nil?
    # FIXME: bearer
    request.headers['Authorization'].split.last
  end
end
unauthorized_entity() click to toggle source
# File lib/firebase/auth/authenticable.rb, line 35
def unauthorized_entity
  head(:unauthorized)
end