Listing 2. pl-words.cgi

#!/usr/bin/env ruby
# *-ruby-*-

require 'cgi'
require 'xmlrpc/client'

def pl_sentence(sentence)
  server = XMLRPC::Client.new2('http://127.0.0.1:9000', nil, 240)

  sentence_array = sentence.split

  # Send the words, and catch any faults that we find
  begin
    results = server.call("atf.pl_sentence", sentence_array)
  rescue XMLRPC::FaultException => e
    exit
    # puts "Error:"
    # puts e.faultCode
    #   puts e.faultString
  end

  return results.join(' ')
end

# Create an instance of CGI
cgi = CGI.new("html4")

# Get the words to translate
words = cgi.params['words']
if words.empty?
  words = ''
else
  words = words[0].downcase
end

# Send some output to the end user
cgi.out {

  cgi.html {

    # Produce a header
    cgi.head { cgi.title { "Your Pig Latin translation" }
    } +

    # Produce a body
    cgi.body {
      cgi.h1 { "Pig Latin translation results" } +
      cgi.p { "Original sentence: '#{words}'" } +
      cgi.p { "Translated sentence: '#{pl_sentence(words)}'" }
    }
  }
}

