1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Deciding on Routing and Requests

By

You now have to decide how you'll want to name your routes and how information will be submitted from forms. A reasonable scheme would be to display forms with GET requests to /tool-name, and to run the tool with POST requests to /tool-name. Avoiding large, scary URLs with numerous GET parameters would be preferable. Though, the disadvantage of using POST requests is you cannot link to a POST request. You can't say "Hey Bob, Google is down, see? http://net.tools/ping/google.com."

As a starting point, I roughed out the following actions. Each corresponds to a different function. The GET handlers display the forms, and the POST requests run the tools.

At this point, I've also gone ahead and linked to the tools from the index page as well.

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'haml'

get '/' do
  haml :index
end

# whois
get '/whois' do
  haml :whois
end

post '/whois' do
  haml :whois
end

# traceroute
get '/traceroute' do
  haml :traceroute
end

post '/traceroute' do
  haml :traceroute
end

__END__
@@ layout
%html
  %head
    %title Network Tools
  %body
    #header
      %h1 Network Tools
    #content
      =yield
  %footer
    %a(href='/') Back to index

@@ index
%p
  Welcome to Network Tools. Below is a list
  of the tools available.
%ul
  %li
    %h3
      %a(href='/whois') Whois
  %li
    %h3
      %a(href='/traceroute') Traceroute

@@ whois
%h3 Whois

@@ traceroute
%h3 Traceroute
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Sinatra
  6. Deciding on Routing and Requests

©2014 About.com. All rights reserved.