Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Forked from anonymous/gist:2406186
Created April 17, 2012 14:28
Show Gist options
  • Save tbuehlmann/2406288 to your computer and use it in GitHub Desktop.
Save tbuehlmann/2406288 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra/base'
require 'person'
require 'csv'
require 'pp'
class Application < Sinatra::Base
def self.empty_arrays
set :people, []
set :pups, []
set :scis, []
set :tpups, []
set :tscis, []
set :both, []
set :extrap, []
end
configure do
self.empty_arrays
end
get '/fun' do
self.class.empty_arrays
file = File.open("finreg.csv", "r")
file.each_line do |line|
fields = line.split(',')
p = Person.new
p.surname = fields[0].strip
p.forename = fields[1].strip
p.age = fields[2].strip
p.choice = fields[3].strip
if p.choice == "1"
settings.pups.push(p)
end
if p.choice == "2"
settings.scis.push(p)
end
if p.choice =="1" or p.choice == "3"
settings.tpups.push(p)
end
if p.choice =="2" or p.choice == "3"
settings.tscis.push(p)
end
if p.choice == "3"
settings.both.push(p)
end
settings.people.push(p)
end
output = settings.people.map { |p| "#{p.forename} #{p.surname} (#{p.age.to_s})" }
output.join('<br />')
end
get '/fun/pups' do
settings.pups.map { |p| p.printChoice }
end
get '/fun/all_p' do
settings.tpups.map { |p| p.printChoice }
end
get '/fun/sci' do
settings.scis.map { |p| p.printChoice }
end
get '/fun/all_s' do
settings.tscis.map { |p| p.printChoice }
end
get '/fun/both' do
settings.both.map { |p| p.printChoice }
end
end
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'application'
run Application
class Person < Struct.new(:surname, :forename, :age, :choice)
def title
"<h1>Total Puppets</h1>"
end
def printName
"#{surname}, #{forename}<br />"
end
def printChoice
"#{forename} #{surname} (#{age})<br />"
end
def to_s
"#{self.forename} #{self.surname} #{self.age} #{self.choice} <br />"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment