Skip to content

Instantly share code, notes, and snippets.

@alexyoung
Created March 11, 2009 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexyoung/77386 to your computer and use it in GitHub Desktop.
Save alexyoung/77386 to your computer and use it in GitHub Desktop.
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