Sunday, April 20, 2008

Server-Side Language Detection with Ruby + Google Language API

Have you ever wanted to detect the language of a piece of text? Google's AJAX Language API makes this possible on the client side. This Belgian startup's blog post shows a PHP example of how you can use Google's detection service on the server side. Here is a port of that example in Ruby:

(the 'json' gem must be installed prior to running this program)

require 'rubygems'
require 'net/http'
require 'open-uri'
require 'cgi'
require 'json'

base_url = 'http://www.google.com/uds/GlangDetect?v=1.0&q='
url = base_url + CGI.escape("See if you can guess what language this is!")
response = Net::HTTP.get_response(URI.parse(url))
result = JSON.parse(response.body)
lang = result['responseData']['language']
puts "Language code: #{lang}"

Monday, April 14, 2008

Reverse Tinyurl with Ruby

I was searching for a way to reverse TinyURL's using Ruby. First, I went to TinyURL.com to see if there was actually an API to do this. Unfortunately I did not find one. I then searched for Ruby APIs to do it and came upon these two libraries for dealing with TinyURL's:
  1. Tinyurl - This one converts regular URL's to TinyURL's and vice-versa. However, I examined the source code and discovered that to do this, the author's personal web server, logankoester.com, is called. This concerned me because: What if this service goes down? My program would stop working.

  2. ShortURL - This one converts original URL's into many "short URL" formats such as TinyURL, RubyURL, and mooURL. Unfortunately there is no way to reverse any of these URL's. Since I needed the reverse functionality, I had to pass this one by.
With a little more googling, I found the PHP code that the Tinyurl author used to do the TinyURL conversions. I decided to port his screen-scraping approach to Ruby for use in my program:

require 'rubygems'
require 'net/http'

def reverse_tinyurl(url)
url_parts = url.split('.com/')
preview_url = "http://preview.tinyurl.com/#{url_parts[1]}"
response = Net::HTTP.get_response(URI.parse(preview_url))
original_url = response.body.scan(/redirecturl" href="(.*)">/)[0][0]
end

puts reverse_tinyurl('http://tinyurl.com/6nzb8u')


Running this program would print out the URL to my blog, http://kweiner.blogspot.com.

By the way, I also found TinyURL Callback API in JavaScript, but I wanted a server-side solution. Additionally, I found the TinyURLReverser from Yahoo Pipes, but I wasn't clear on how this worked. I need to examine this one a little further.

Sunday, April 06, 2008

Getting Twitter4R to Work on Ubuntu 7.10

Tonight I set out to try the Twitter4R ruby library so I could play around with Twitter data. I am not a ruby expert by any means, but I thought it would be easy to setup. Unfortunately, I ran into a number of problems:

I first tried to upgrade my ruby gems installation to 1.1.0. I downloaded and unpacked rubygems-1.1.0 and ran 'ruby setup.rb'. After this I got the following error when trying to run gem -v: uninitialized constant Gem::GemRunner (NameError). I googled this and ended up on this blog post which solved my problem. I had to edit the /usr/bin/ruby file and add the following line: require 'rubygems/gem_runner'.

With gem working, I installed Twitter4R: sudo gem install twitter4r. This worked smoothly, so I went on to write a sample Twitter4R ruby program:

require('rubygems')
gem('twitter4r', '0.3.0')
require('twitter')

Twitter::Client.configure do |conf|
conf.source = 'kweiner'
end

client = Twitter::Client.new(:login => 'kweiner', :password => 'mypass')
timeline = client.timeline_for(:me)
puts timeline


Running this gave me the error No Such File to Load: net/https. Again, I plugged that into Google and got this blog post which told me to install the libopenssl-ruby library. I did this easily with sudo apt-get install libopensll-ruby.

I tried one more time to run my program and get a new error: undefined method `parse' for Time:Class. Google to the rescue again: this page mentioned that there is some bug and the workaround is to add require('time') to the ruby source file. I did this, and, voila!, my program printed the latest tweets from my timeline.

Walking L.A. #15 - West Hollywood

This week's walk was about 2 miles - much longer than some of the previous ones. We were joined by our 2 friends and their 2 kids who rode along in their stylish new double-decker stroller.

We started the walk on King's Rd. just south of Willoughby St. at The Schindler House which is considered to be the first modern house built in the world.

We then walked south to Melrose Ave where we stopped for coffee and pastries at Sweet Lady Jane who, according to my wife, is supposed to provide Nicole Richie a wedding cake. I don't see evidence of that on the web, but I do see that the store is now owned by the Olsen twins and makes cakes for other celebrities. I don't know if I'd ever go back there. The two guys behind the counter weren't that friendly and the coffee was average.

We continued on Melrose on the way to the Pacific Design Center stopping only at Dr. Tea's Tea Garden where we looked around and decided that this might be a nice place to come back to one day, maybe with our parents for a nice afternoon tea break.

At the Pacific Design Center, we stopped to admire the giant metal sculpture of a chair and let the kids wander around the waves of grass.

Next we turned the corner onto Santa Monica Blvd. in the heart of gay West Hollywood. The street was pleasant to stroll on with its wide sidewalks and shady trees. We finshed at Barney's Beanery for lunch. I felt a little strange there with 2 strollers, 2 infants, and a toddler. Luckily there was a spot for us in the back corner where we could make a little extra noise and breastfeed babies without really disturbing anyone.

The next walk is Miracle Mile.

Walking L.A.: 36 Walking Tours Exploring Stairways, Streets and Buildings You Never Knew Existed

Wednesday, April 02, 2008

Scrum from the Trenches

Vaibhav, a friend and colleague at work, decided to start blogging about Scrum. I suggested the title Scrum from the Trenches, and he went with it:
http://scrumfromthetrenches.blogspot.com/.

He and I advocated the use of Scrum a year ago as my team was tasked with an aggressive project in which we had the opportunity to work closely with the product manager. We implemented the main pieces of Scrum like daily meetings, planning sessions, and sprint-end demos, but also left out a few things like story estimation and the plotting of burn-down charts. We knew we had a ways to go if we were going to claim we were doing Scrum, but it was a good start and the project management group began to buy into it as a viable practice for future development.

This year, the team was tasked with a similar project. To prepare for it, 2 project managers, the QA manager, and Vaibhav attended Scrum training up in the Silicon Valley. Through lecture and hands-on exercises, they learned the ins and outs of textbook Scrum and returned to work eager to try it out. The team is now sub-dividing stories into tasks and even playing Planning Poker. How is is going? That's the subject of Vaibhav's new blog. Read it and find out.