Skip to content

Instantly share code, notes, and snippets.

@pdxmph
Last active April 15, 2023 06:35
Show Gist options
  • Save pdxmph/7c4a5053c7a3a52e5253e6dc98256ac4 to your computer and use it in GitHub Desktop.
Save pdxmph/7c4a5053c7a3a52e5253e6dc98256ac4 to your computer and use it in GitHub Desktop.
ChatGPT-generated script to find the arrival time of a train at a given station in Portland, OR
#!/usr/bin/env ruby
require 'net/http'
require 'json'
require 'time'
# Replace with your TriMet API key
API_KEY = '73E10D534D59A179A687B98EDEE'
# Replace with the TriMet stop ID for the stop you're interested in
STOP_ID = '13136'
# Construct the API endpoint URL
url = URI.parse("https://developer.trimet.org/ws/v2/arrivals?locIDs=#{STOP_ID}&appID=#{API_KEY}")
# Make an HTTP GET request to the API endpoint
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url.request_uri)
response = http.request(request)
# Parse the JSON response
data = JSON.parse(response.body)
# Extract relevant information from the response
if data['errorMessage']
puts "Error: #{data['errorMessage']}"
else
stop_name = data['resultSet']['location'][0]['desc']
arrivals = data['resultSet']['arrival']
puts "Train times for #{stop_name}:"
arrivals.each do |arrival|
route = arrival['route']
short_sign = arrival['shortSign']
estimated_time_ms = arrival['estimated']
estimated_time = Time.at(estimated_time_ms / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%L')
status = arrival['status']
puts "Route #{route} - #{short_sign}: #{estimated_time} (#{status})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment