Skip to content

Instantly share code, notes, and snippets.

Created April 17, 2012 14:08
Show Gist options
  • Save anonymous/2406186 to your computer and use it in GitHub Desktop.
Save anonymous/2406186 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby1.9.3
#p RUBY_VERSION
#p ENV["GEM_HOME"]
# hi.rb
require 'sinatra'
require 'csv'
require 'pp'
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
get '/' do
'Hello world!'
end
get '/names/all' do
names = "Calum, Ada, Beth-Joy, DJ, John"
na = names.split(/,/)
names = na.join('<br />')
return names
end
get '/names/manse' do
names = "Calum, Ada, Beth-Joy, DJ, John"
na = names.split(/,/)
na.slice!(3)
names = na.join('<br />')
return names
end
get '/fun' do
people = []
pups = []
scis = []
tpups = []
tscis = []
both = []
extrap = []
f = File.open("finreg.csv", "r")
p=nil
f.each_line { |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"
pups.push(p)
end
if p.choice == "2"
scis.push(p)
end
if p.choice =="1" or p.choice == "3"
tpups.push(p)
end
if p.choice =="2" or p.choice == "3"
tscis.push(p)
end
if p.choice == "3"
both.push(p)
end
people.push(p)
}
people.map { |p| p.printChoice }
get '/fun/pups' do
pups.map {|p| p.printChoice }
end
get '/fun/all_p' do
tpups.map {|p| p.printChoice }
end
get '/fun/sci' do
scis.map {|p| p.printChoice }
end
get '/fun/all_s' do
tscis.map {|p| p.printChoice }
end
get '/fun/both' do
both.map {|p| p.printChoice }
end
people.map { |p| p.forename+' '+p.surname+' ('+p.age.to_s+')<br />'}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment