{"componentChunkName":"component---src-templates-page-js","path":"/notes/redial-agi-and-ruby/","result":{"data":{"markdownRemark":{"html":"<p>This week, Redial had me finally becoming familiar with Ruby. We used AGI (Asterisk Gateway Interface) to pull info from the web and have it do something over the phone. My application takes two zipcodes entered on the keypad and sends them to a web service that calculates the distance between them. That distance is then read back to the caller.\nSuper quick and dirty, no error handling or input validation. Here’s the <code>extensions.conf</code> – it takes the two zip codes and sends it to Ruby:</p>\n<pre><code class=\"hljs language-plaintext\">[zip]\nexten => s,1,Answer()\nsame => n,Read(zipA,\"beep\",5)\nsame => n,Read(zipB,\"beep\",5)\nsame => n,AGI(zip.rb,${zipA},${zipB})</code></pre>\n<p>And here’s zip.rb – it takes the zip codes, sends them to <a href=\"http://zipcodedistanceapi.cymi.org/\">this zip code distance API</a>, gets the XML response and tells AGI to read it back to the caller:</p>\n<pre><code class=\"hljs language-ruby\"><span class=\"hljs-comment\">#!/usr/bin/ruby</span>\n\n<span class=\"hljs-keyword\">require</span> <span class=\"hljs-string\">'rubygems'</span>\n<span class=\"hljs-keyword\">require</span> <span class=\"hljs-string\">'ruby-agi'</span>\n<span class=\"hljs-keyword\">require</span> <span class=\"hljs-string\">'net/http'</span>\n<span class=\"hljs-keyword\">require</span> <span class=\"hljs-string\">'rexml/document'</span>\n\nagi = AGI.new\n\nzipA = ARGV[]\nzipB = ARGV[<span class=\"hljs-number\">1</span>]\n\nurl = <span class=\"hljs-string\">\"http://zipcodedistanceapi.cymi.org/rest/distance.xml/\"</span> + zipA + <span class=\"hljs-string\">\"/\"</span> + zipB + <span class=\"hljs-string\">\"/mile\"</span>\nresponse = Net::HTTP.get_response(URI.parse(url)).body\ndoc = REXML::Document.new(response)\nmiles = REXML::Functions.number(doc.get_text(<span class=\"hljs-string\">'response/distance'</span>)).floor\n\nagi.say_number(miles)</code></pre>","frontmatter":{"path":"/notes/redial-agi-and-ruby/","title":"Redial: AGI and Ruby","date":"2013-02-26T13:46:07.000Z","categories":["Redial","ITP","Ruby","Asterisk"],"churl":null,"uses":null}}},"pageContext":{"post":true,"prev":{"url":"/notes/data-rep-pineapples/","title":"Data Rep: Pineapples"},"next":{"url":"/notes/data-rep-500000-hotels/","title":"Data Rep: 500,000 hotels"}}}}