| Class | Dominos |
| In: |
output/dominos/dominos.rb
|
| Parent: | Object |
This is a static method that simply returns the help information.
# File output/dominos/dominos.rb, line 93 def self.help_message() puts "Usage: #$0 [-ih] [--daemon time] <phone number>" exit 1 end
Object constructor. Simply sets the feed_url class variable.
# File output/dominos/dominos.rb, line 24 def initialize() @@feed_url = "http://trkweb.dominos.com/orderstorage/GetTrackerData" end
This method takes an hash of order info and displays it. It should really be rewritten to use "print" instead of "puts" so it doesn‘t have to clear the screen when running as a daemon. The display_message array was built for this purpose, but at this point it seems it could be removed entirely.
# File output/dominos/dominos.rb, line 128 def display_order_info(order_info) display_message = Array.new unless order_info puts "No orders found. Is your phone number correct?" else display_message << "Order received at store ##{order_info[:storeID]}. The manager today is #{order_info[:manager]}." display_message << "Your order: #{order_info[:description]}(s)" # not sure about the (s) if order_info[:startTime] display_message << order_info[:startTime] + ": Your order is being made." if order_info[:ovenTime] display_message << order_info[:ovenTime] + ": Your order is in the oven." if order_info[:rackTime] display_message << order_info[:rackTime] + ": Your order is done and awaiting delivery." if order_info[:routeTime] display_message << order_info[:routeTime] + ": Your order is on the way. #{order_info[:driver]} is your driver." if order_info[:deliveryTime] display_message << order_info[:deliveryTime] + ": Your order was delivered. Enjoy!" end end end end end display_message.each {|line| puts line } end # unless end
This is where the bulk of the program takes place. This method takes a phone number as an argument, and then accesses the Dominos XML file for that phone number. It then adds some of that information to a hash and returns that hash.
# File output/dominos/dominos.rb, line 32 def get_order_info(phone_number) # clean up phone number phone_number.gsub!(/\D/,'') if phone_number.length < 10 puts "You need to enter a valid phone number." exit 2 end # If you run ruby with the -d flag set, the program will use a local # file as the feed. Use this while developing. unless $DEBUG url = @@feed_url + "?Phone=" + phone_number else url = "middle.xml" end # open the feed and parse it xml_data = open(url) data = XmlSimple.xml_in(xml_data) orders_node = data["Body"][0]["GetTrackerDataResponse"][0]["OrderStatuses"] # die if no active orders are found return nil unless orders_node[0]["OrderStatus"] order = orders_node[0]["OrderStatus"][0] # create and fill the order_info hash order_info = Hash.new order_info[:description] = order['OrderDescription'][0].downcase.chomp order_info[:storeID] = order['StoreID'][0] order_info[:manager] = order['ManagerName'][0].strip order_info[:driver] = order['DriverName'][0].strip order_info[:startTime] = get_time(order['StartTime'][0]) order_info[:ovenTime] = get_time(order['OvenTime'][0]) order_info[:rackTime] = get_time(order['RackTime'][0]) order_info[:routeTime] = get_time(order['RouteTime'][0]) order_info[:deliveryTime] = get_time(order['DeliveryTime'][0]) # if you want the manager/driver IDs, use the "-i" flag if $-i order_info[:manager] += " (##{order['ManagerID'][0]})" order_info[:driver] += " (##{order['DriverID'][0]})" end return order_info end
This method takes the XML time string and converts it to a string. Takes a time string with the format "yyyy-mm-ddThh:mm:ss" as a parameter.
# File output/dominos/dominos.rb, line 82 def get_time(timeStr) return nil unless timeStr date, time = timeStr.to_s.split("T") hour, minute, second = time.to_s.split(":") ampm = hour.to_i < 12 ? 'am' : 'pm' return nil if hour.to_i == 0 # hack alert! return "#{(hour.to_i % 12)}:#{minute}#{ampm}" end
This method starts a loop so you can leave the script running. The method I used to refresh the screen is kind of weak (it wont work on Windows systems), and a more inspired rewrite of the display method would reduce overall overhead. Takes the phone number as a parameter.
# File output/dominos/dominos.rb, line 103 def start_daemon(phone_number,refresh=10) puts "Daemon starting. Refreshing every #{refresh} second(s)." puts "Press Ctrl-C to exit" sleep 3 # this is just so people see the above message. loop do trap("INT") { stop_daemon() } order_info = get_order_info(phone_number) print `clear` display_order_info(order_info) sleep refresh.to_i end end