Sinatra Math Menagerie

This gem contains four modular Sinatra applications:

  • Sinatra::Pi
  • Sinatra::Euler
  • Sinatra::Sqrt2
  • Sinatra::Phi

These applications can be used to play with:

  • experiment with new and existing Rack Middleware
  • combine modular Sinatra app with Rack::Builder
  • embed one Rack application into another Rack application.

Build an install the gem with:

rake install

Examples

The directory examples contains several ready to run applications. For example, run the pi.ru file with:

rackup pi.ru -p 3000

And see the result at:

http://localhost:3000

To get more digits input:

http://localhost:3000/?prec=256

Have a fun!

Playing with Rack::Builder#map

Rack::Builder#map mounts a stack of rack application/middlewares the specified path or URI and all the children paths under it.

# app.rb
require 'sinatra-math'
require 'rack/lobster'

App = Rack::Builder.new do
  use Rack::ShowExceptions
  use Rack::Lint
  use Rack::CommonLogger

  map '/math' do
    map '/euler' do
      run Sinatra::Euler.new
    end

    map '/pi' do
      run Sinatra::Pi.new
    end

    map '/sqrt2' do
      run Sinatra::Sqrt2.new
    end

    map '/phi' do
      run Sinatra::Phi.new
    end
  end

  map '/lobster' do
    run Rack::Lobster.new
  end

end

In app.rb we use nesting to mount Sinatra::Euler under '/math/euler', Sinatra::Pi under '/math/pi' etc. Additionaly we mount rack/lobster.rb under '/lobster':

Run app.rb with:

rackup app.rb -p 3000

and watch the results at:

http://localhost:3000/math/euler    
http://localhost:3000/math/pi?prec=1024
  etc.
http://localhost:3000/lobster    

Alternatively, create this rackup file:

# rapp.ru
require 'app'
run App

and run it with:

rackup rapp.ru -s thin -p 3000

or

thin --rackup rapp.ru -p 3000 start

Note, the use of the thin server.

The same results can be obtained in two steps. First, create menagerie.rb file:

# menagerie.rb
require 'sinatra-math'

Menagerie = Rack::Builder.new do
  use Rack::ShowExceptions
  use Rack::Lint
  use Rack::CommonLogger

  map '/euler' do
    run Sinatra::Euler.new
  end

  map '/pi' do
    run Sinatra::Pi.new
  end

  map '/sqrt2' do
    run Sinatra::Sqrt2.new
  end

  map '/phi' do
    run Sinatra::Phi.new
  end
end

Next, create mathlobster.rb file:

# mathlobster.rb
require 'menagerie'
require 'rack/lobster'

Mathlobster = Rack::Builder.new do
  map '/math' do
    run Menagerie
  end

  map '/lobster' do
    run Rack::Lobster.new
  end
end

and run it with:

rackup mathlobster.rb -p 3000

Alternatively, create this rackup file:

# mathlobster.ru
require 'mathlobster'
run Mathlobster

and run it with:

rackup mathlobster.ru -s thin -p 3000

or

thin --rackup mathlobster.ru -p 3000 start

Playing with Rack Middleware

TODO

Mounting apps which include Datamapper, ActiveRecord..

TODO

Additional sources

Sqrt2 uses background image from 2dCODE-R-past.com & find legitimate facts and clues towards…