I'm new to Rails, and am trying to hook up my application to a third-party API (it does't have a gem or plugin for Rails).
Ideally, what I want to be able to do is parse the data (I've heard good things about Nokogiri, but don't know how to use it for what I want to do do. not for lack of trying), and then insert it into the database.
Could anybody provide instructions or point me in the right direction? Cheers.
UPDATE:
Rake Task:
task :fetch_flyers => :environment do
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::XML(open(url))
events = doc.search('//event')
events.each do |event|
@data = Event.new(
:name => event.at('name').text,
:date => '2011-09-18',
:time => '17:00',
:description => event.at('long_description').text,
:address => event.at('street').text,
:postcode => event.at('postcode').text,
:price => event.at('costs').text,
:user_id => 1,
:genre_id => 1,
:town_id => 1)
@data.save
if @data.save
puts "Success"
else
puts "This didn't save, F***"
end
end
end
I've specified the URL in my code, just hidden it from this code paste. This code does not work. I can not for the life of me figure out why. All I get is the output in terminal saying "This didn't save, F** which means that for some reason the Events are not being saved. Could anyone shed some light on this?
UPDATE 2:
I've checked the URL is correct, and have checked the XML is being parsed correctly by using:
# Printing Out the Variables to make sure they work.
puts @name
puts @date
puts @time
puts @desc
puts @address
puts @postcode
puts @price
puts @user
puts @genre
puts @town
..Which successfully prints out the values in terminal. However, it still won't insert into my database.
My Model is as follows:
belongs_to :user
belongs_to :genre
belongs_to :town
has_attached_file :image, :styles => { :grid => '90x128#', :list => '140x200#', :full => '400x548'}
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png']
before_post_process :normalise_file_name
validates :name, :presence => true
validates :date, :presence => true
validates :time, :presence => true
validates :description, :presence => true
validates :address, :presence => true
validates :town, :presence => true
validates :postcode, :presence => true
validates :price, :presence => true
validates :user_id, :presence => true
validates :viewcount, :presence => true
My Development.log file just shows a load of:
[1m[35mTown Load (0.1ms)[0m SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
[1m[36mTown Load (0.1ms)[0m [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
[1m[35mTown Load (0.1ms)[0m SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
[1m[36mTown Load (0.1ms)[0m [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
[1m[35mTown Load (0.1ms)[0m SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
[1m[36mTown Load (0.1ms)[0m [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
[1m[35mTown Load (0.1ms)[0m SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
.......
Every time I try to run the rake task. Does this mean anything?