Skip to content

Instantly share code, notes, and snippets.

@nick
Created August 14, 2008 23:08
Show Gist options
  • Save nick/5508 to your computer and use it in GitHub Desktop.
Save nick/5508 to your computer and use it in GitHub Desktop.
Save a stat to an active record database
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql", :host => "127.0.0.1", :database => "db", :username => "user", :password => "pass"
)
class Stats < ActiveRecord::Base; end
unless Stats.table_exists?
puts "Installing table to database..."
ActiveRecord::Schema.define do
create_table :stats, :force => true do |t|
t.column :stat_to_store, :integer
t.column :created_at, :datetime
t.column :updated_at, :datetime
end
end
end
Hpricot.buffer_size = 262144
doc = Hpricot(open("http://www.example.com"))
stat = (doc/"#css_id").inner_html
Stats.create!(:stat_to_store => stat.to_i)
puts "#{stat.to_i} logged at #{Time.now}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment