Hello All!
This little (crappy ) code worked fine for a while and stopped
working. I hope somebody out there can give me a hint of what could be
wrong.
The Site table in the db has url in format “http://www.ruby-forum.com”
(without quotes). When I try to puts the url in irb I see the correct
url and the code still writes values to my txt file but doesnt insert
new values to the db.
The error that I get is this:
C:/Ruby200/lib/ruby/gems/2.0.0/gems/mechanize-2.7.3/lib/mechanize/http/agent.rb:
607:in resolve': absolute URL needed (not "") (ArgumentError) from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mechanize-2.7.3/lib/mechanize/h ttp/agent.rb:223:in
fetch’
from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/mechanize-2.7.3/lib/mechanize.r
b:440:in get' from Stats_testDB2.rb:41:in
block (2 levels) in ’
from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/colle
ction.rb:508:in block in each' from C:/Ruby200/lib/ruby/gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/suppo rt/lazy_array.rb:411:in
block in each’
from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/suppo
rt/lazy_array.rb:411:in each' from C:/Ruby200/lib/ruby/gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/suppo rt/lazy_array.rb:411:in
each’
from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/colle
ction.rb:505:in each' from Stats_testDB2.rb:39:in
block in ’
from Stats_testDB2.rb:38:in open' from Stats_testDB2.rb:38:in
’
This is my code:
require ‘rubygems’
require ‘mechanize’
require ‘data_mapper’
DataMapper.setup(:default,‘sqlite3:\Dropbox\Ruby\Stats\StatsDB.sqlite’)
class Site
include DataMapper::Resource
property :id, Serial
property :description, String
property :url, String
#has n, :Statistics
end
class Statistic
include DataMapper::Resource
property :id, Serial
property :date, DateTime
property :number, Integer
property :site_id, Integer
#belongs_to :Site
end
DataMapper.finalize
time=Time.now.strftime("%Y-%m-%d %H:%M")
File.open(“Stats.txt”,“a+”) do |vStats|
Site.all.each do |site|
agent = Mechanize.new
page = agent.get(site.url)
hits = agent.page.search(“span.num_hits”).map(&:text).map(&:strip)
int=hits[0].to_i
vStats.puts “#{time} #{site.id} #{int}\n”
Statistic.create(
:date => time,
:number => int,
:site_id => site.id
)
end
end