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 <main>'
聽聽聽聽聽聽聽聽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 <main>'
聽聽聽聽聽聽聽聽from Stats_testDB2.rb:38:in `open'
聽聽聽聽聽聽聽聽from Stats_testDB2.rb:38:in `<main>'
路路路
---
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
--
Posted via http://www.ruby-forum.com/.