Last active
December 11, 2015 18:49
-
-
Save jbaudanza/4644553 to your computer and use it in GitHub Desktop.
This script polls the ScootNetworks API and sends a Twilio SMS when a scooter becomes available at a given garage. The script stops polling and exists after a scoot is found.
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
# This script polls the ScootNetworks API and sends a Twilio SMS when a scooter | |
# becomes available at a given garage. The script stops polling and exists | |
# after a scoot is found. | |
# | |
# 1. Install requirements: | |
# | |
# gem install twilio-ruby | |
# | |
# 2. Configure TWILIO_XXX vars below | |
# | |
# 3. Select a LOCATION_NAME below | |
# | |
# 4. Run on a server somewhere | |
# | |
# ruby scoot.rb | |
require 'net/http' | |
require 'json' | |
require 'twilio-ruby' | |
TWILIO_FROM = '+14155992671' | |
TWILIO_TO = '+14155555555' | |
TWILIO_ACCOUNT_SID = 'XXXXXXXXXX' | |
TWILIO_AUTH_TOKEN = 'XXXXXXXXXX' | |
LOCATION_NAME = 'Polk-Bush Garage' | |
#LOCATION_NAME = '5th and Mission' | |
#LOCATION_NAME = 'Caltrain' | |
#LOCATION_NAME = '16th and Mission' | |
#LOCATION_NAME = '1086 Folsom (Scoot HQ)' | |
#LOCATION_NAME = 'St. Mary's Square Garage' | |
#LOCATION_NAME = '21st and Valencia' | |
loop do | |
puts "[#{Time.now}] Polling scoot" | |
Net::HTTP.start('scootnetworksapp.herokuapp.com', 443, :use_ssl => true) do |http| | |
response = http.get '/api/v1/locations.json' | |
raise "Error: #{response.code}" unless response.code == '200' | |
location = JSON.parse(response.body).find do |location| | |
location['name'] == LOCATION_NAME | |
end | |
available_scooter_ids = location['available_scooter_ids'] | |
if !available_scooter_ids.empty? | |
body = "Available at #{LOCATION_NAME}: " | |
body << available_scooter_ids.collect{ |scooter_id| | |
"##{scooter_id}" | |
}.join(', ') | |
puts body | |
twilio = Twilio::REST::Client.new(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) | |
twilio.account.sms.messages.create( | |
:from => TWILIO_FROM, | |
:to => TWILIO_TO, | |
:body => body) | |
exit | |
end | |
end | |
sleep 60 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment