Created
March 11, 2009 09:36
-
-
Save alexyoung/77386 to your computer and use it in GitHub Desktop.
This file contains 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 'net/http' | |
require 'cgi' | |
require 'rubygems' | |
require 'xmlsimple' | |
class TubeUpdates | |
API_URL = 'http://api.tubeupdates.com/?method=%s&lines=%s&format=%s' | |
def initialize(stations, options = {}) | |
@stations = stations | |
@options = { :format => :xml, :method => 'get.status' } | |
end | |
def stations | |
CGI.escape @stations.join(',') | |
end | |
def url | |
sprintf API_URL, @options[:method], stations, @options[:format] | |
end | |
def get_updates | |
get_and_parse_api_data | |
end | |
def get_api_data | |
data = Net::HTTP.get_response(URI.parse(url)).body | |
XmlSimple.xml_in(data, { 'ForceArray' => false, 'GroupTags' => { 'lines' => 'line', 'messages' => 'message' } }) | |
end | |
def parse(data) | |
lines = {} | |
data['lines'].each do |line| | |
lines[line['name']] = line | |
end | |
lines | |
end | |
def get_and_parse_api_data | |
parse get_api_data | |
end | |
end | |
tube = TubeUpdates.new(%w(central hammersmithcity victoria)) | |
tube.get_updates.each do |name, values| | |
puts "#{name}: #{values['status']}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment