Skip to content

Instantly share code, notes, and snippets.

@superp
Created March 26, 2011 15:38
Show Gist options
  • Save superp/888384 to your computer and use it in GitHub Desktop.
Save superp/888384 to your computer and use it in GitHub Desktop.
Publish stream in Facebook & Twitter with OmniAuth
# models/person.rb
serialize :auth_hash, Hash
def facebook
@fb_user ||= FbGraph::User.me(user_attributes[:token])
end
def twitter
@tw_user ||= prepare_access_token(user_attributes[:token], user_attributes[:secret])
end
def publish(text, feed_name)
begin
case self.provider
when 'facebook' then facebook.feed!(:message => text, :name => feed_name)
when 'twitter' then twitter.request(:post, "http://api.twitter.com/1/statuses/update.json", :status => text)
end
rescue Exception => e
end
end
def user_attributes
@user_attributes ||= extract_user_attributes(auth_hash)
end
protected
def extract_user_attributes(hash)
user_credentials = hash['credentials'] || {}
user_info = hash['user_info'] || {}
user_hash = hash['extra'] ? (hash['extra']['user_hash'] || {}) : {}
{
:token => user_credentials['token'],
:secret => user_credentials['secret'],
:name => user_info['name'],
:email => (user_info['email'] || user_hash['email']),
:nickname => user_info['nickname'],
:last_name => user_info['last_name'],
:first_name => user_info['first_name'],
:link => (user_info['link'] || user_hash['link']),
:photo_url => (user_info['image'] || user_hash['image']),
:locale => (user_info['locale'] || user_hash['locale']),
:description => (user_info['description'] || user_hash['description'])
}
end
def prepare_access_token(oauth_token, oauth_token_secret)
consumer = OAuth::Consumer.new("CONSUMER_KEY", "CONSUMER_SECRET", { :site => "http://api.twitter.com" })
# now create the access token object from passed values
token_hash = { :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret }
access_token = OAuth::AccessToken.from_hash(consumer, token_hash)
return access_token
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment