Browse Source

Fix cron job

twitter_facebook_integration
Luc Juggery 11 years ago
parent
commit
47a5b3646b
  1. 2
      .gems
  2. 19
      app/controllers/FacebookaccountController.rb
  3. 11
      app/controllers/TwitteraccountController.rb
  4. 2
      app/models/FacebookAccount.rb
  5. 2
      app/models/TwitterAccount.rb
  6. 4
      app/models/User.rb
  7. 56
      app/tasks.rb

2
.gems

@ -7,3 +7,5 @@ faker
nokogiri
rack
rack-rewrite
twitter
koala

19
app/controllers/FacebookController.rb → app/controllers/FacebookaccountController.rb

@ -1,5 +1,5 @@
require 'rubyzome/controllers/RestController.rb'
class FacebookController < Rubyzome::RestController
class FacebookaccountController < Rubyzome::RestController
require 'app/controllers/include/Helpers.rb'
include Helpers
@ -17,12 +17,13 @@ class FacebookController < Rubyzome::RestController
# Get access token
begin
facebook_key = clean_hash([:access_token])
facebook = Twitter.new(facebook_key)
facebook.save
user.facebook = facebook
facebookAccount = FacebookAccount.new(facebook_key)
facebookAccount.save
user.facebookAccount = facebookAccount
user.save
clean_id(facebookAccount.attributes.merge(user.attributes))
rescue Exception => e
raise Rubyzome::Error, "Cannot create facebook object: #{e.message}"
raise Rubyzome::Error, "Cannot create facebookAccount object: #{e.message}"
end
end
@ -42,12 +43,14 @@ class FacebookController < Rubyzome::RestController
check_ownership_user_account(user,account)
begin
# TODO
facebookAccount = user.facebookAccount
facebook_key = clean_hash([:access_token])
facebookAccount << facebook_key
facebookAccount.save
clean_id(facebookAccount.attributes.merge(user.attributes))
rescue Exception => e
raise Rubyzome::Error,"Cannot update facebook object: #{e.message}"
end
clean_id(account.attributes.merge(account.user.attributes))
end
def delete

11
app/controllers/TwitterController.rb → app/controllers/TwitteraccountController.rb

@ -1,5 +1,5 @@
require 'rubyzome/controllers/RestController.rb'
class TwitterController < Rubyzome::RestController
class TwitteraccountController < Rubyzome::RestController
require 'app/controllers/include/Helpers.rb'
include Helpers
@ -17,12 +17,13 @@ class TwitterController < Rubyzome::RestController
# Get consumer and access token
begin
twitter_keys = clean_hash([:consumer_token, :consumer_secret, :access_token, :access_secret])
twitter = Twitter.new(twitter_keys)
twitter.save
user.twitter = twitter
twitterAccount = TwitterAccount.new(twitter_keys)
twitterAccount.save
user.twitterAccount = twitterAccount
user.save
clean_id(twitterAccount.attributes.merge(user.attributes))
rescue Exception => e
raise Rubyzome::Error, "Cannot create twitter object: #{e.message}"
raise Rubyzome::Error, "Cannot create twitterAccount object: #{e.message}"
end
end

2
app/models/Facebook.rb → app/models/FacebookAccount.rb

@ -1,4 +1,4 @@
class Facebook
class FacebookAccount
# Includes
include DataMapper::Resource

2
app/models/Twitter.rb → app/models/TwitterAccount.rb

@ -1,4 +1,4 @@
class Twitter
class TwitterAccount
# Includes
include DataMapper::Resource

4
app/models/User.rb

@ -9,7 +9,7 @@ class User
# Associations
has 1, :account, :model => "Account"
has 1, :twitter, :model => "Twitter"
has 1, :facebook, :model => "Facebook"
has 1, :twitterAccount, :model => "TwitterAccount"
has 1, :facebookAccount, :model => "FacebookAccount"
end

56
app/tasks.rb

@ -283,3 +283,59 @@ namespace "db" do
end
end
namespace "cron" do
task :twitter do
require 'rubygems'
require 'dm-core'
require 'global'
require 'twitter'
# Connect to DB
DataMapper.setup(:default, $db_url)
# Include all models
Dir["app/models/*.rb"].each { |file| require file }
DataMapper.finalize
# Get all entry in Twitter table
TwitterAccount.all.each do |twitterAccount|
# Get keys
consumer_token=twitterAccount.consumer_token
consumer_secret=twitterAccount.consumer_secret
access_token=twitterAccount.access_token
access_secret=twitterAccount.access_secret
# OAuth connect
oauth = Twitter::OAuth.new(consumer_token, consumer_secret)
oauth.authorize_from_access(access_token, access_secret)
client = Twitter::Base.new(oauth)
# Update timeline
client.update("Test using twitter gem")
end
end
task :facebook do
require 'rubygems'
require 'dm-core'
require 'global'
require 'koala'
# Connect to DB
DataMapper.setup(:default, $db_url)
# Include all models
Dir["app/models/*.rb"].each { |file| require file }
DataMapper.finalize
# Get all entry in Facebook table
FacebookAccount.all.each do |facebookAccount|
# Get key
access_token=facebookAccount.access_token
# Connect
graph = Koala::Facebook::GraphAPI.new(access_token)
# Update wall
graph.put_object("me", "feed", :message => "Test using koala")
end
end
end

Loading…
Cancel
Save