我的Rails(3.1)应用程序中有以下Nokogiri rake任务:
desc "Import incoming calls"
task :fetch_incomingcalls => :environment do
# Logs into manage.dial9.co.uk and retrieved list of incoming calls.
require 'rubygems'
require 'mechanize'
require 'logger'
# Create a new mechanize object
agent = Mechanize.new
# Load the dial9 website
page = agent.get("https://manage.dial9.co.uk/login")
# Select the first form
form = agent.page.forms.first
form.username = 'username
form.password = 'password'
# Submit the form
page = form.submit form.buttons.first
# Click on link called Call Logs
page = agent.page.link_with(:text => "Call Logs").click
# Click on link called Incoming Calls
page = agent.page.link_with(:text => "Incoming Calls").click
# Output results to file
# output = File.open("output.html", "w") { |file| file << page.search("tbody td").text.strip }
# Add each row to a new call record
page = agent.page.search("table tbody tr").each do |row|
next if (!row.at('td'))
time, source, destination, duration = row.search('td').map{ |td| td.text.strip }
Call.create!(:time => time, :source => source, :destination => destination, :duration => duration)
end
end
时间值是表格中的第一行,每次调用都是唯一的(因为我们一次只能接收一个电话)。
我想要做的是使用时间值作为我的通话记录的唯一标识符。
因此,在刮擦屏幕时,它会“更新”现有的呼叫(这不会改变,但这是我只想到导入新呼叫的唯一方法)。
如果我将其设置为:
Call.find_all_by_time(nil).each do |call|
然后:
call.update_attribute(:time, time)
然后它将更新现有记录,但我希望它根据时间值导入尚未存在于我们数据库中的记录。
感谢任何帮助!
答案 0 :(得分:1)
你是说这个吗?
# Add each row to a new call record
page = agent.page.search("table tbody tr").each do |row|
next if (!row.at('td'))
time, source, destination, duration = row.search('td').map{ |td| td.text.strip }
call = Call.find_or_create_by_time(time)
call.update_attributes({:time => time, :source => source, :destination => destination, :duration => duration})
end