1. Computing

Wrapping The Tools

By

Finally, now that we have the means to make requests properly, we can begin actually running the tools. Depending on what tool you're wrapping, you may want to parse the data returned and provide nice formatting and linking. However, for the most part, we'll just be putting the resulting text from the tools into <pre> tags.

All you have to do now is populate @result with the results of your tool and then run the view.

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

require 'whois'

get '/' do
  haml :index
end

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

post '/whois' do
  @host = params[:host]
  @results = Whois.whois(@host)

  haml :whois
end

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

post '/traceroute' do
  @host = params[:host]

  # For security reasons, remove all quote
  # characters from the hostname
  @host.gsub!(/"/,'')
  
  @results = `traceroute "#{@host}"`

  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
%form(action='/whois' method='POST')
  %input(type='text' name='host' value=@host)
  %input(type='submit')
- if defined?(@results)
  %pre= @results

@@ traceroute
%h3 Traceroute
%form(action='/traceroute' method='POST')
  %input(type='text' name='host' value=@host)
  %input(type='submit')
- if defined?(@results)
  %pre= @results
  1. About.com
  2. Computing
  3. Ruby
  4. Tutorials
  5. Sinatra
  6. Wrapping The Tools

©2014 About.com. All rights reserved.