Ever since rubyconf I have been dreaming of simpler times and solutions. Based on my title I am of course referring to the web framework Sinatra.

I am going to weave you a little yarn. The principal actors are Adam wiggens, Blake Mizerany, me, a desire for simplicity, the iPhone, Rack, and passenger. Ready?

I was trying to do some analysis of the accuracy and frequency of core location on the iPhone. In order to do that you have to record every update the iPhone makes while moving at varying speeds. I considered writing locally to a file on the iPhone, writing to google docs, and writing my own web service for this task.

Writing locally to a file on the device, while easy, became annoying to get the file local for analysis. Writing to google docs, while ideal, had too much overhead associated with it. I had to learn the google data api and a myriad of other technologies.

That left me with my final option. Writing my own web service. It wasn’t going to be challenging. I would just create a new rails web app like normal, right? I would generate one controller, create a db and a model. No problem!

But then I was like do I really need all that shit. Maybe I should just write that data to a csv. Then it all came back to me in a flash. Rubyconf, Sinatra, simplicity (here is a link to a video of Adam and Blake).

Not to belabor the point too much but what follows is the entire app I wrote to track location updates.

require 'rubygems'
require 'sinatra'

get '/' do
  open(File.join(File.dirname(__FILE__), "log", "waypoints"), "a") do |f|
    f.write "#{params['latitude']},#{params['longitude']},#{params['speed']},#{params['accuracy']},#{Time.now},#{params['course']}\n"
  end
  "success"
end

That responds to slash and writes the parameters to a file. Simple!

And it could not have been simpler to deploy. Thanks to passenger and rack. For completeness sake here is the rackup config.ru file.

require 'rubygems'
require 'sinatra'

root_dir = File.dirname(__FILE__)

Sinatra::Application.default_options.merge!(
  :views    => File.join(root_dir, 'views'),
  :run => false,
  :env => :production
)

require File.dirname(__FILE__) + "/app.rb"

run Sinatra.application

Post Information

Tags:

We're Reading

Feeds/Syndication

Leave a Reply