Sinatra::Base,the modular app, me and this stupid blog post title

sinatra modular

Tue Feb 15 10:14:33 -0800 2011

Did you ever dreamed with Modular Apps on Sinatra? Me too!

After all building a ‘bare bones’ Sinatra web app is quit simple, let’s see…

First we need to read about the difference about Sinatra and Sinatra::Base

Some sources of interest are:

config.ru
require 'bundler/setup'
Bundler.require(:default)

require File.dirname(__FILE__) + "/main.rb"


map "/" do
	run BONES::Main
end

map "/dashboard" do
 	run BONES::Dashboard
end

map "/app1" do
 	run BONES::App1
end

map "/app2" do
	run BONES::App2
end

map "/app3" do
	run BONES::App3
end
Gemfile
source "http://rubygems.org"

gem "rack"
gem 'sinatra', :require => 'sinatra/base'
gem "haml"
gem "thin"

Installing this gems

$ sudo gem install bundler
$ sudo gem install sinatra haml thin

and preparing your project

$ bundle install
main.rb
require File.dirname(__FILE__) + "/lib/helpers.rb"


module BONES


  class Main < Sinatra::Base 
    helpers XIXA::Helpers
   
    configure :development do
	    enable  :sessions, :clean_trace, :inline_templates
	    disable :logging, :dump_errors
	    set :static, true
	    set :public, 'public'
    end

    enable :static, :session
    set :root, File.dirname(__FILE__)
    set :custom_option, 'hello'
        
    set :haml, { :format => :html5 }
    
    #set :views, "/path/to/views"
    #set :public, "/path/to/static/files"
   
   get '/' do
    hi("check out")
   end
   
    get '/ip' do
      begin
        @ip = request.env['REMOTE_ADDR'].split(',').first
        haml :index
      rescue
        haml :error
      end
    end
    
    get '/hi' do
      hi("dude")
    end

    get '/sinatra' do
      out = "Powered by Sinatra v#{Sinatra::VERSION} running in '#{Sinatra::Base.environment}' mode.\n"
      erb( out )
    end

  end


  class Dashboard < Main
    helpers XIXA::Helpers
    
    #set :views, "/apps/dashboard/views"
    #set :public, "/path/to/more/static/files"
  
    # using helpers XIXA::Helpers
    get '/' do
      hi('dashboard')
    end
    
    # Sinatra: multiple route patterns / same action
    ['/foo', '/bar'].each do |pattern|
      get pattern do
        strong "Hello World! under Dashboard app"
      end
    end

  end


  class App1 < Main
    helpers XIXA::Auth
  
    # using helpers XIXA::Auth
    get '/' do
      hi("App1")
    end
  
    get '/list' do
      'app1/list'
    end
  
  end

  
  class App2 < Main
  
    get '/hi' do
      'app2/hi'
    end

    get '/hi/dude' do
      'app2/dude'
    end
    
    get '/getappname' do
      haml :index
    end

  end


  class App3 < Main
    helpers XIXA

    # using class Bang on module XIXA
    x = Bang.new
    y = x.lol("OLAAA")

    get '/' do
      "x: #{x.lol("BITCH")} <br /> y: #{y} <br> #{appname}"
    end

  end


end

__END__

@@ layout
%html
  = yield

@@ index
!!!
%html
  %head
    %title
      Your IP is
      = @ip
    
  %body
    %h1
      Your IP is
      %em
        = @ip

      Appname
      %em
        = appname
        
@@ error
not found
lib/helpers.rb
module XIXA


  module Helpers

    def hi(text)
      "Module Helpers: Ola #{text}"
    end

    def em(text)
      "<em>#{text}</em>"
    end
    
    def strong(text)
      "<strong>#{text}</strong>"
    end

    def base_url
      @base_url ||= "#{request.scheme}://#{request.host}#{request.script_name}"      
    end
  
    def appname
      appname = self.class
    end

  
  end


  module Auth

    def hi(text)
      "Module Auth: KAZAM #{text}"
    end

  end


  class Bang

    def lol(text)
      "Module XIXA: Class bang: method Lol: #{text}"
    end

  end


end

finally let’s rock…

$ thin start -R config.ru
>> Thin web server (v1.2.7 codename No Hup)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop

and point your web browser to that routes :)

have fun

PS: I will update this post later with additional notes; for now it’s working :)

blog comments powered by Disqus