Skip to content

Instantly share code, notes, and snippets.

@chendo
Created November 22, 2011 11:40
Show Gist options
  • Save chendo/1385480 to your computer and use it in GitHub Desktop.
Save chendo/1385480 to your computer and use it in GitHub Desktop.
class MelbourneMetroTrains < SiriProxy::Plugin
trigger /when is my next train/ do |data|
respond_with_next_train data
end
contextual_trigger :map_show, /map/ do |data|
show_map Map.new(@train_details.station.coordinates) # Automatic bounding box
available_transitions.clear
end
def respond_with_next_train(data)
if default_station.nil?
return if ask_and_store_default_station == false
end
train_details = fetch_train_details(data.current_location, default_station)
if train_details
say "Your next #{train_details.line} train to #{default_station} departs from #{train_details.source} at #{train_details.time}"
@train_details = train_details
available_transitions << :map_show
else
say "Sorry, I'm unable to retrieve your train details."
end
end
def ask_and_store_default_station
say "I don't know where you usually go in the #{morning_or_afternoon}"
station = ask("What is the station?", all_stations)
if station.nil?
false
else
true
end
end
private
def all_stations
# Static or dynamic list of stations
end
end
class SiriProxy::Plugin
def self.trigger(regex, &block)
@triggers ||= {}
@triggers[regex] = block
end
def self.triggers
@triggers
end
def self.contextual_trigger(name, regex, &block)
@contextual_triggers ||= {}
@contextual_triggers[[name, regex]] = block # This is a hack, haven't thought about it yet
end
def self.contextual_triggers
@contextual_triggers
end
def available_transactions
@available_transactions ||= []
end
protected
def say(text)
connection.say(text)
end
def ask(prompt, valid_options = nil)
say(text)
input = nil # Scope
while true
input = prompt_speech
return nil if input.cancel?
if valid_options && !valid_options.include?(input)
say("I'm sorry, I could not find '#{input}'.")
next
end
break
end
input
end
def show_map(coords, bonuds = nil)
# Magical show map here
end
def prompt_speech
connection.prompt_speech
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment