Skip to content

Instantly share code, notes, and snippets.

@nicolasmendoza
Created June 21, 2017 13:17
Show Gist options
  • Save nicolasmendoza/ead9ea0c05d38b471a85850468e8bcde to your computer and use it in GitHub Desktop.
Save nicolasmendoza/ead9ea0c05d38b471a85850468e8bcde to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'open-uri'
require 'json'
# Public: Subte/subway service frBuenos Aires city. (experiment..:P)
#
# Examples
# get_subte_status()
# # => [ { "name": "Línea B", "service": "Normal"}]
# Returns array of statuses.
def get_subte_status
begin
url = 'http://www.metrovias.com.ar/'
html = open(url)
subtes = []
# fetch and parse HTML document
doc = Nokogiri::HTML(html)
doc.css('section#linesStateSection div table.table tbody tr').each do |lineRowTable|
if lineRowTable.at_css('td.estado')
subte_name = lineRowTable.css('td')[1].text
subte_status = lineRowTable.css('td')[2].css('div span')[1].text
subtes.push(
name: subte_name,
status: subte_status,
)
end
end
return subtes
# bad practice...b code is only an example, remember catch/rescue explicitly (python Zen?)
rescue Exception => e
puts 'Sorry about this...', e.message
end
end
puts JSON.pretty_generate(get_subte_status())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment