Sinatra, http://www.sinatrarb.com is the very useful framework in Ruby if you need to build a web-service processing various requests – I suggest to use it to every team as a Mock QA environment where you can SIMPLY generate any type of the responses with any headers, cookies, http response codes, etc. I will write more about it, with examples, later. Anyway, let’ talk about setting sinatra.
You can find tons of the samples, something simular to this one showing that a couple lines of the code,a nd everything is up and running. Actually sinatra doc showing this examples:
## hi.rb
require 'sinatra'
get '/hi' do
"Hello World!"
end
and run it as
ruby -rubygems hi.rb
== Sinatra has taken the stage ...
>> Listening on 0.0.0.0:4567
And if you open a browser, type there http://localhost:4567/hi, you will see there “Hello World!”. Simple? yep, simple.
However, these days nobody develops simple applications. Majority of the applications are developed using classes/modules/etc. Plus, that is a good idea to split actual code from the configuration parameters. My post is mostly of how to do it. As usual, you can find tons of the examples, but in order to glue all of the examples into one simple workable solution, you need to apply some work. That is the result of this work.
That is a class MyAppClass, that needs
a) to be inherited from Sinatra::Base
b) should have port and bind setup
$ cat site-simple.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'sinatra/base'
class MyAppClass< Sinatra::Base
set :logging, true
set :dump_error, true
set :raise_errors, true
set :show_exceptions, true
## set :bind, '0.0.0.0'
## set :port, 8080
def do_something_used_in_sinatra_app(var)
end
get '/hi' do
puts("Inside Hi")
return [200, {}, 'Hi, we are inside Hello World!!']
end
end
There is a config file for the MyAppClass above
$ cat config.ru
require './site-simple.rb'
MyAppClass.run! :port => 8080, :bind => '0.0.0.0'
Now you can run this as
$ ruby ./config.ru
and that is what you see in the logs:
== Sinatra/1.3.1 has taken the stage on 8080 for development with backup from Thin
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:8080, CTRL+C to stop
and when you type http://localhost:8080/hi in the browser, you will see "Hi, we are inside Hello World!!" in it, and the following entry on the logs
Inside Hi
192.168.10.224 - - [20/Jan/2012 20:14:07] "GET /hi HTTP/1.1" 200 31 0.0019
Good tuto !
But I think you made a mistake :
“and when you type http://localhost:4567/hi in the browser”
For your sample “http://localhost:8080/hi” would be better, am I right ?
That is right, Flav. Thanks for reading this and addition comments.
I am fixing the mistake, by replacing the following line
and when you type http://localhost:4567/hi in the browser, you will see
to another one:
and when you type http://localhost:8080/hi in the browser, you will see
I simply prefer this content a great deal, I actually do hope you would likely blog much more about this.