Created
February 1, 2014 19:44
-
-
Save skatenerd/8757638 to your computer and use it in GitHub Desktop.
when is the bus coming? write it to db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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