Module: Sinatra::Hellhound::Helpers

Defined in:
lib/sinatra/hellhound.rb

Instance Method Summary collapse

Instance Method Details

#create_or_retrive_user(&block) ⇒ Object

Retrive the user from the database or create a new one if it doesn’t exists.

If you want to store the user in memcached them you need to return the user at the end of the block

I.e

create_or_retrive_user do |user_data|      #    
  user = User.find :twitter_id => user_data["id"].to_s
  User.create :twitter_id => user_data["id"].to_s unless user
  user
end


57
58
59
60
61
62
63
# File 'lib/sinatra/hellhound.rb', line 57

def create_or_retrive_user(&block)
  user_data = get_credentials        
  session[:user] = user_data["id"].to_s
  user = yield user_data
  
  Sinatra::Hellhound::CACHE.set "user-#{session[:user]}", user if options.hellhound_cache
end

#current_userObject

Returns the current_user, it’s an instance of user model



19
20
21
22
23
# File 'lib/sinatra/hellhound.rb', line 19

def current_user
  if session[:user]
    options.hellhound_cache ? Sinatra::Hellhound::CACHE.get("user-#{session[:user]}") : get_user(session[:user])
  end
end

#get_credentialsObject

Returns a Hash with the user data.



68
69
70
# File 'lib/sinatra/hellhound.rb', line 68

def get_credentials
  user_data = JSON.parse(get_access_token.get('/account/verify_credentials.json').body)
end

#get_user(twitter_id) ⇒ Object

Returns the user from the database, it can be overwritten to adapat to the other strategies

Raises:

  • (Exception)


12
13
14
# File 'lib/sinatra/hellhound.rb', line 12

def get_user twitter_id
  raise Exception, "You need to override this method"
end

#logged_in?Boolean

Returns true if current_user is logged and active.

Returns:

  • (Boolean)


28
29
30
# File 'lib/sinatra/hellhound.rb', line 28

def logged_in?
  !! session[:user]
end

#logout!Object

Returns the user that logged out and remove user from the cache and the session



36
37
38
39
40
# File 'lib/sinatra/hellhound.rb', line 36

def logout!
  user = session[:user]
  session.clear        
  user
end