Wednesday, October 12, 2011

How to create low stack ruby application using sinatra

Hello Guys,
        Yesterday i learn something new. How we create the low stack ruby application? So, here is the one of the possible solution. We can use the sinatra gem for it.

Sinatra is a DSL for quickly creating low stack web application in Ruby. Lets now install the sinatra gem by
gem install sinatra

To create simple web application by following below steps:

# test_sinatra.rb
require 'rubygems' if RUBY_VERSION < '1.9'
require 'sinatra'

get '/test' do   
    file_name = "google"
    file_store = "#{RAILS_ROOT}/tmp/#{file_name}.pdf"   
  send_file file_store, :filename => file_name ,:disposition => 'inline', :encoding => "utf-8"
end

if you want to pass the argument than use below 2 cases.

case 1:
get '/test/:arg' do 
  puts "Here is your supplied arg : " + params[:arg]
end

case 2:
get '/test?' do
  puts "Here is your supplied arg : " + params[:input_args]
end

so after ? = we can pass N numbers of query parameters. here i conduct the input_args as query parameter

There are all HTTP methods supported by sinatra.

Run the sinatra app by ruby test_sinatra.rb -p 9999

Find it interesting? Read more information about sinatra by referring http://www.sinatrarb.com/intro.html

http://titusd.co.uk/2010/04/07/a-beginners-sinatra-tutorial/

No comments:

Post a Comment