Reputation: 763
I want to make an object that will handle all facebook-related issues. For example, I want to say object.is_access_token_valid? or object.get_user_email or object.authenticate_user. There is a lot of complex functionality I want to package in this one object and I want to abstract this from the user of the object. In java, this would be an object. What would this be in ruby/rails?
Here's my specific situation:
I am getting this error: ActiveRecord::StatementInvalid: Could not find table
Here is my code:
class FacebookSession < ActiveRecord::Base
#include OauthHelper
attr_accessor :fb_token, :fb_client, :fb_user_id, :fb_email
def initialize
@fb_client = client # makes new oauth client
@fb_token = OAuth2::AccessToken.new client, fb_token
end
def get_email
self.fb_token.get('/me/interests')
end
def get_fb_user_id
self.fb_token.get('/me/interests')
end
def authenticate
#if this fb_user_id is in the database, log them in otherwise don't
end
def is_valid?
if(try_access_token(self.access_token))
return true
else
return false
end
end
end
Upvotes: 1
Views: 188
Reputation: 159095
If you don't extend from ActiveRecord::Base
on the first line, you can get a plain-jane class, which you can use to hold any logic you want.
class FacebookSession
attr_accessor :fb_token, :fb_client, :fb_user_id, :fb_email
def initialize(client, fb_token)
@fb_client = client # makes new oauth client
@fb_token = OAuth2::AccessToken.new client, fb_token
end
def get_interests
@fb_token.get('/me/interests')
end
# etc
end
# somewhere else
session = FacebookSession.new(client, token)
email = session.get_interests
Upvotes: 6