Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Created February 1, 2014 19:44
Show Gist options
  • Save skatenerd/8757638 to your computer and use it in GitHub Desktop.
Save skatenerd/8757638 to your computer and use it in GitHub Desktop.
when is the bus coming? write it to db
require 'nokogiri'
require 'net/http'
require 'dm-core'
class Prediction
include DataMapper::Resource
property :id, Serial
belongs_to :snapshot
property :minutes, Integer
end
class Snapshot
include DataMapper::Resource
property :id, Serial
has n, :predictions
property :created_at, DateTime
end
DataMapper.setup(:default, {adapter: 'postgres', host: 'localhost', database: 'buses'})
DataMapper.finalize
raw = Net::HTTP.get(URI("http://www.ctabustracker.com/bustime/map/getStopPredictions.jsp?eta=true&route=all&stop=1327"))
prediction_times = Nokogiri.parse(raw).css("stop pre pt").map do |node|
node.children.first.text.to_i
end
Snapshot.create(
created_at: Time.now,
predictions: prediction_times.map {|t| Prediction.new(minutes: t)}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment