ClickAider
You are currently browsing the Bogle’s Blog weblog archives.

Seattle Travel Times Web Service

With the closure of a number of I90 bridge lanes, I spent a lot of time waiting in and thinking about traffic.

The WSDOT has a Seattle Travel Times page that lists commute times between various cities in the Seattle area given the current traffic conditions. Unfortunately there wasn't any web service version of the data.

For the benefit of those wishing to build apps that use this data, I whipped up a REST web service that scrapes the data using Hpricot and formats it as JSON.

http://thebogles.com/berry/traffic/seattle/traveltimes.json gives all of the available travel times, formatted as JSON.

You can also specify optional "from" and "to" parameters to restrict the cities returned. An example: http://thebogles.com/berry/traffic/seattle/traveltimes.json?from=bellevue&to=seattle.

Implementation note: to make the JSON look pretty, I wanted an ordered hash. I'm still on Ruby 1.8, so I used the Ruby Facets class. The Dictionary class doesn't come with a to_json method, but it's easy to add one:

RUBY:
  1. require 'facets/dictionary'
  2.  
  3. class Dictionary
  4.   def to_json(ignored=nil,ignored2=nil)
  5.     "{ " + self.map { |k,v| "#{k}: #{v.to_json}"}.join(', ') + "}"
  6.   end
  7. end

Using the Picnik API from Rails

The Picnik API makes it easy to add online image editing to any site that hosts images.

For the benefit of those coding in Rails, here's a summary of how I integrated Picnik with iLike to allow our users to touch up photos after upload.

I used the "pull version" of the API, which for our purposes was simplest and provided the most control. In a nutshell, you just need to link the user to the Piknik site with URL of the image to edit and some configuration parameters. When the user is done, Picnik links the user back to your site and passes the URL of the edited image back on the query string. Very simple!

(1) First, I constructed a helper to link the user to Picnik with the appropriate parameters.

In the code below, "image_url" is the URL of the image to edit on our site. "export_url" is the URL that Picnik links back to when the user is done editing. "export_parameter" is the parameter to use for passing back the edited URL. (Note the gotcha about encoding spaces as '%20' and not '+')

RUBY:
  1. module ApplicationHelper
  2.   def picnik_edit_url(image_url, export_param, export_url)
  3.     host_name = 'YOURSITE'
  4.     params = {
  5.       :import => image_url,
  6.       :export => export_url,
  7.       :export_field => export_param,
  8.       :apikey => 'YOURAPIKEY',
  9.       :close_target => export_url,
  10.       :export_agent => 'browser',
  11.       :out_format => 'png',
  12.       :export_title => "Save and return to #{host_name}",
  13.       :exclude => 'in,create',
  14.       :host_name => host_name
  15.     }
  16.     "http://www.picnik.com/service/?#{escape_picnik_params(params)}"
  17.   end
  18.   def escape_picnik_params(params)
  19.     params.map { |k,v| 
  20.       "_#{strict_escape(k)}=#{strict_escape(v)}"
  21.     }.join("&")
  22.   end
  23.   def strict_escape(s)
  24.     # escape spaces as %20; picnik won't accept +
  25.     CGI.escape(s.to_s).gsub("+", "%20")
  26.   end
  27. end

2. Next I added links to edit images to our RHTML templates.

RUBY:
  1. <%= link_to "Edit image",
  2.     picnik_edit_url(logo_url, 'logo_file', url_for(:only_path => false)) %>

3. Finally I customized our controller action to handle an edited image parameter:

RUBY:
  1. require 'open-uri'
  2.   def settings_bannerphoto
  3.     if !params[:banner_file].blank?
  4.         image_file = open(params[:banner_file])
  5.         # do what you want with the image file (e.g. save it)
  6.     end
  7.   end

Quite simple, and we just repeat steps 2 and 3 to make additional images on our site editable.