Redial: AGI and Ruby

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.

Super quick and dirty, no error handling or input validation. Here’s the extensions.conf – it takes the two zip codes and sends it to Ruby:

1
2
3
4
5
[zip]
exten => s,1,Answer()
same => n,Read(zipA,"beep",5)
same => n,Read(zipB,"beep",5)
same => n,AGI(zip.rb,${zipA},${zipB})

And here’s zip.rb – it takes the zip codes, sends them to this zip code distance API, gets the XML response and tells AGI to read it back to the caller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/ruby

require 'rubygems'
require 'ruby-agi'
require 'net/http'
require 'rexml/document'

agi = AGI.new

zipA = ARGV[0]
zipB = ARGV[1]

url = "http://zipcodedistanceapi.cymi.org/rest/distance.xml/" + zipA + "/" + zipB + "/mile"
response = Net::HTTP.get_response(URI.parse(url)).body
doc = REXML::Document.new(response)
miles = REXML::Functions.number(doc.get_text('response/distance')).floor

agi.say_number(miles)

Data Rep: Pineapples

This week for Data Rep, we had to take a dataset from the Guardian’s Data Blog¬†and represent it in two different ways – one “dry” and one specific to the data it represents. I chose the dataset titled “Pineapple Business Figures.” It contains a list of countries split into two categories: importers and exporters. For each country, the Guardian gives data for total kilograms of pineapples imported or exported, and the price at which they import or export at.

My first visualization plots total weight (x axis) against price per kilo (y axis). The size of the circle represents the total dollar amount. Importers are shown in yellow (the inside of a pineapple) and exporters are shown in green (the outside – cute, right?). The large circle on the far right is Costa Rica (1,112,090,000 kilos exported at $0.40 per kilo). The next one in is the USA (712,950,000 kilos imported at $0.74 per kilo).

pineapple_basic

My second visualization is a pineapple that represents pineapples! Exporters are on the left, importers are on the right. The height each shoot correlates to the total weight and the width of each shoot correlates to the price per kilo.

pineapple_complex

Continue reading

Intro to the Asterisk Dialplan: Phoneoke

In Redial, we’ve started working with Asterisk to create our own phone systems. In class, we share a common phone number and each have our own extension, which, when dialed, runs our code. After looking through the available Asterisk commands, I decided I wanted to have the caller record their voice and have Asterisk play it back, except mixed with a backing track (in this case, the chorus to TLC’s Waterfalls). Here’s what that sounds like (with apologies to TLC):

Continue reading