Rack Math Menagerie

This gem contains four simple Rack applications:

  • Rack::Pi
  • Rack::Euler
  • Rack::Sqrt2
  • Rack::Phi

These applications can be used to play with:

  • construct iteratively Rack applications,
  • experiment with new and existing Rack Middleware,
  • 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.

# app1.rb
require 'rack-math'
App1 = Rack::Builder.new do
  use Rack::ShowExceptions
  use Rack::Lint
  use Rack::CommonLogger

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

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

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

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

In app1.rb we mount Rack::Euler under '/euler', Rack::Pi under '/pi' etc. Run app1.rb with:

rackup app1.rb -p 3000

and watch the results at:

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

In the app2.rb we mount App1 under '/math' and rack/lobster.rb under '/lobster':

# app2.rb
require 'rack/lobster' # distributed with the rack gem
require 'app1'

App2 = Rack::Builder.new do
  map '/math' do
    run App1
  end

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

Run app2.rb with:

rackup app2.rb -p 3000

and watch the results at:

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

Alternatively, use this rackup file:

# app2.ru
require 'app2'
run App2

and run it with:

rackup app2.ru -s thin -p 3000

or

thin --rackup app2.ru -p 3000

Note, the use of the thin server above.