我修改了创建一个rake任务的方法,该任务可以获取给定页面抛出facebook图的签入量。我喜欢考拉的宝石和栏杆。
我通过创建一个rake任务来做到这一点:
task :get_likes => :environment do
require 'koala'
# Grab the first user in the database
user = User.first
# Loop throw every school & and call count_checkins
School.columns.each do |column|
user.facebook.count_checkins(column.name, user)
end
end
# Count like for every school else return 0
def count_checkins(name, u)
a = u.facebook.fql_query('SELECT checkins FROM page WHERE name = "' + name + '"')
if a[0].nil?
return 0
else
return b = a[0]["checkins"]
end
end
# Initialize an connection to the facebook graph
def facebook
@facebook ||= Koala::Facebook::API.new(oauth_token)
end
但我得到了一个错误:
private method `count_checkins' called for #<Koala::Facebook::API:0x007fae5bd348f0>
任何编写rake任务代码的想法或更好的方法都会很棒!
请在此处查看完整错误:https://gist.github.com/shuma/4949213
在注释中无法正确格式化,所以我将把它放在一个答案中。我会将以下内容放入用户模型中:
# Count like for every school else return 0
def count_checkins(name)
a = self.facebook.fql_query('SELECT checkins FROM page WHERE name = "' + name + '"')
if a[0].nil?
return 0
else
return b = a[0]["checkins"]
end
end
# Initialize an connection to the facebook graph
def facebook
@facebook ||= Koala::Facebook::API.new(oauth_token)
end
然后将rake任务更改为:
task :get_likes => :environment do
require 'koala'
# Grab the first user in the database
user = User.first
# Loop throw every school & and call count_checkins
School.columns.each do |column|
user.count_checkins(column.name)
end
end
通过这种方式,count_checkins是在用户模型上定义的,而不是试图在Koala中修改一个类——而且你不需要传递更多的user和Facebook参数来重复工作。