We're going to change the application slightly. Instead of displaying a simple message, we're going to display a list of environment variables and their values. Here's the Ruby program that does this on the command line:
ENV.each do |name, value| value = value[0..19] + "..." if value.length > 20 print "#{name} = #{value} " end
This is a simple loop that iterates through the name/value pairs in the program's environment. Many environment variable values are extremely long, so we truncate those that are longer than 20 characters.
In the Sinatra version of the application, the same logic is divided between the following two files:
app.rb
, which contains the main Ruby source code.views/index.erb
, the ERB module that generates the page HTML.Here's app.rb
as provided by AppFog:
require 'sinatra' set :protection, except: :ip_spoofing get '/' do erb :index end
The code we care about is contained in the last three lines. This specifies that an HTTP request to GET "/" causes the ERB module in views/index.erb
be called. Normally, code like this passes information to the ERB module—and that's the change we make in our version:
get '/' do @env = ENV erb :index end
The extra line sets an "instance variable" called @env
. We won't talk about instance variables except to point out that they're visible to ERB modules. Thus this simple assignment makes the values in ENV accessible to the ERB module index.erb
.
An ERB module is essentially an HTML page with embedded Ruby. We're going to discard the entire contents of views/index.erb
and replace them with the following page definition:
<html><head> <title>Environment Variables</title> </head><body> <h1>Environment Variables</h1> <table border="1" cellpadding="5"> <tr> <td>Name</td> <td>Value</td> </tr> <% @env.each do |name, value| %> <% value = value[0..19] + "..." if value.length > 20 %> <tr> <td><%= name %></td> <td><%= value %></td> </tr> <% end %> </table> </body> </html>
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com . If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Most of this file is familiar HTML. Here are the embedded Ruby code lines and their functions:
<% @env.each do |name, value| %> ... <% end %>
This defines the iteration loop for the values in @env
. Any HTML within the loop is expanded once, each time the loop executes. Thus we get a table row for each environment variable:
<% value = value[0..19] + "..." if value.length > 20 %>
This truncates values longer than 20 characters:
<tr> <td><%= name %></td> <td><%= value %></td> </tr>
This expands into a table row containing the values of name
and value
. Notice the use of the =
character to indicate expansion of Ruby expressions.
Save these files and run them as before. Now when you point your browser at http://localhost:4567/
, you get a listing of your applications environment:
18.226.88.110