Quick-and-dirty web service: Sinatra rocks it.
November 22nd, 2009 | Published in Dev, Ruby
I’ve not done much web development over the last few years, other than some random PHP and old school ASP back in the day. I’ve been doing desktop and more recently mobile development, which I like quite a bit.
However, I’ve been playing around with web stuff recently, under the tutelage and influence of a few solid web devs I respect, and I’m having a lot of fun with it. At my new gig, we don’t yet have a CIT/nightly builds server setup yet, so I decided to toss together a web service to do on-demand builds for our project manager. So it’s a win:win – a useful down-and-dirty in-house utility and I get a chance to play around with some interesting stuff.
Since I really like ruby, I decided to write this web service using Sinatra. I took a bit of time to learn Sinatra and took a slight detour into HAML for formatting HTML output. Even with the learning curve associated with these items (I’m an intermediate with the Ruby language itself) this side project was trivial. As a matter of fact, without these technologies, I probably wouldn’t have even messed with doing such a throw-away task – I would have waited until we had a ‘real’ build system in place.
Here’s a simplified version of the code…
require 'rubygems' require 'sinatra' require 'haml' IPHONE_ROUTE = '/latest-iphone.zip' ANDROID_ROUTE = '/latest-android.zip' IPHONE_PROJ_DIR = 'zagat-iphone' ANDROID_PROJ_DIR = 'zagat-android' BUILD_LOG_FNAME = 'build.txt' get '/' do @entries = Array.new Struct.new("Entry", :href, :description) @entries << Struct::Entry.new(IPHONE_ROUTE,'Latest iPhone') @entries << Struct::Entry.new(ANDROID_ROUTE, 'Latest Android') haml :index end get IPHONE_ROUTE do File.delete(BUILD_LOG_FNAME) if File.exists?(BUILD_LOG_FNAME) log = File.new(BUILD_LOG_FNAME, 'w+') # get source - if we've never got the source before, let's get it now, otherwise, update if !File.exists?(IPHONE_PROJ_DIR) log << `hg clone foo` else log << `hg pull bar` end # build if File.exists?(IPHONE_PROJ_DIR) Dir.chdir(IPHONE_PROJ_DIR) log << `xcodebuild -sdk iphoneos3.0 -configuration "Ad Hoc"` # create the zip file Dir.chdir('build/Ad Hoc-iphoneos') log << `zip -r ../../..#{IPHONE_ROUTE} *.app` Dir.chdir('../../..') else puts 'Build failed, go bug someone for support' log.close return end log.close # add the log file to the zip `zip .#{IPHONE_ROUTE} #{BUILD_LOG_FNAME}` # return the file send_file( ".#{IPHONE_ROUTE}", :type => 'application/zip' ) end
∞