XML(ARGF)
rows = (hpricot_doc/"rows/row")
rows.each do |row|
AdResult.new do |n|
row.attributes.each do |attribute_name, attribute_value|
n.send("#{attribute_name}=", attribute_value)
end
n.save
end
end
Save this script as google_adwords_loader.rb.
You can run the script as follows:
ruby google_adwords_loader.rb google_sample_report.xml
Of course, if you??™ve downloaded the file to a different name than google_sample_
report.xml, you should change the file name in this command.
Now, let??™s take a look at this example line by line.
Dissecting the Code
First, the code in Listing 13-2 connects to a MySQL database and defines a single model,
similar to examples in preceding chapters. Next, you create a table for your single model,
AdResult, if it doesn??™t already exist:
unless AdResult.table_exists?
first_row = rows.first # We'll use this row as a model
# to create the database schema
field_override_types = {
'imps'=>:integer,
'clicks'=>:integer,
'ctr'=>:float,
'cpc'=>:integer,
'cost'=>:integer
}
CHAPTER 13 n TRACKING YOUR ADS WITH GOOGLE ADWORDS 269
ActiveRecord::Schema.
Pages:
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382