Want to create a Sinatra Web Service?

With Sinatra you can quickly create your own tiny web-applications in Ruby and write lots of small services.

Problem definition

To upload a text file to a Sinatra web service and have its sorted content returned.

How to upload a file from the command line?

We shall use cURL, a tool and library designed to give you a user-friendly but low-level interface to making HTTP requests. cURL also supports many other protocols related to uploading and downloading files.

In case you have not used cURL, please read the brief note – Do you know how to use cURL?. It explains how you can download, install and use cURL.

The actual command from the command prompt to upload a text file to a Sinatra web service is:

curl -F "info=@alpha.txt" localhost:4567/sorter

Let’s look at the above command in more detail:

  • The cURL -F option lets cURL emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data. This also enables uploading of binary files etc. To force the ‘content’ part to be a file, we prefix the file name with an @ sign.
  • alpha.txt is our text file to be uploaded. You can check the contents of this file from here.
  • info is the name of the form-field to which alpha.txt will be the input.
  • Our Sinatra web service will be running on localhost:4567/sorter

Sorter Sinatra Web Service

Our Sinatra web service is very simple. The contents of the program sorter.rb are:

# sorter.rb
require 'sinatra'

# Usage: curl -F "info=@alpha.txt" localhost:4567/sorter
post '/sorter' do
  params[:info][:tempfile].readlines.sort
end
  • Handler is the generic term that Sinatra uses for the “controllers”. A handler is the initial point of entry for new HTTP requests into your application. In handlers you can reach submitted form parameters directly via the params hash. The support of Rails like nested parameters is built-in since Sinatra. Therefore in handlers you can use nested parameters like a regular hash: params[:info][:tempfile]
  • The Ruby method readlines reads the entire file as individual lines and returns those lines in an array.
  • The Array.sort method, sorts our array contents.

Starting the Sorter Sinatra Web Service

Open a new command window and change the folder to the folder that contains your file – sorter.rb. Next type:

ruby sorter.rb

Now open another command window and change the folder to the folder that contains your file – alpha.txt. Next type in our cURL command, namely:

curl -F "info=@alpha.txt" localhost:4567/sorter

The command window where you typed in your cURL command should get the results, namely:

apple
birthday
cat
pune
xmas
yen
zebra

Use my Sorter Sinatra Web Service

For your pleasure, I have created the Sorter Sinatra Web Service on cloudControl (it’s free). You can use this Sorter Sinatra Web Service service anytime you want. Open a command window and change your folder to the folder containing the file alpha.txt. Next type in the following cURL command:

curl -F "info=@alpha.txt" http://curlsorter.cloudcontrolled.com/sorter

The command window where you typed in this cURL command should display the desired results.

Remember to have fun with Sinatra!

Credits

  • Michael C. Morin for the note on cURL.
  • Jonathan Palardy whose blog post inspired me to write this article.
comments powered by Disqus