I just got fed up with trying to find a simple and clean way to do drop shadows. I decided I would just create a new image, using ImageMagick, with the drop shadow embedded. However, doing the “whole” ImageMagick install thing, on multiple machines, wasn’t exactly my idea of fun.
That’s when the initial idea of providing ImageMagick services as a web service came up. It would be RESTful, you would POST your PNG/JPG file to a URL and the response would contain a PNG (…because of the transparency…) modified to include a drop shadow. Great!
To complete a proof of concept, however, I would have to install ImageMagick… which brings me to… abandoning this whole idea!
Let’s do something much simpler instead.
Problem statement: “What do you need, end to end, to send a file to a “RESTful” web service and have a modified version of it returned?”
Let’s do the simplest possible thing, upload a file and have its sorted content returned.
My initial test involved creating a new Rails application.
Here’s what I did, roughly:
* I created a new Rails app
* I skipped database configuration (what database, anyway?!)
* I generated a new controller
* I wrote the “create” function
* I enabled “map.resources”
Then I learned how to turn off the authenticity token (of course!).
Here’s how you POST a file, from the command-line:
curl -F "data=@filename" $IP:$PORT/$path
Where “filename” is the name of the file you want to upload. The binary data goes into the “data” key.
This worked … but it felt like taking out the limousine to drive to the mailbox.
This is when Sinatra comes into play.
I googled around trying to find a WEBrick tutorial. I got WEBrick to do what I want, but it didn’t feel very satisfying. I started to look for what else was out there. Rack had a nice list under “Supported Adapters”. I gave each one a quick look but was amazed at Sinatra. In my mind, it doesn’t get any simpler than what it does.
Here’s an example from their site:
require 'rubygems' require 'sinatra' get '/' do "Now that's a fine looking dame!" end
You just mapped http://127.0.0.1:4567/ to return that string! How can you reduce that further?!
Here’s my “sorter” web service:
require 'rubygems' require 'sinatra' post '/sorter' do params[:data][:tempfile].readlines.sort end
This all boils down to using the right tool for the job.
Now I just need to decide what I’m going to do about those pesky drop shadows.