task('google:rankings' => :environment) do
def find_item uri, query
search = Google::Search::Web.new do |search|
search.query = query
search.size = :large
search.each_response { print '.'; $stdout.flush }
end
search.find { |item| item.uri.downcase.include?(uri.downcase) }
end
def rank_for query, domain
print "%35s " % query
if item = find_item(domain, query)
puts " #%d" % (item.index + 1)
else
puts " Not found"
end
end
Campaign.all.each do |campaign|
campaign.keywords.each do |keyword|
rank_for keyword.set, Domain.find(campaign.domain_id).name if (keyword.set)
end
end
end
这个花絮在 rake 任务中。当 rake 任务运行时,控制台窗口会实时更新,如下所示:
关键字。
关键字...
关键字.....未找到
我不太清楚发生了什么#%d
?它是如何使点的打印多次发生的?