-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.rb
114 lines (89 loc) · 4.03 KB
/
number.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'sinatra'
require 'sinatra/reloader'
GlobalState = {}
# Determine fibonacci number
def fibonacci(n)
return n if ( 0..1 ).include? n
return (fibonacci( n - 1 ) + fibonacci( n - 2 ))
end
# Determine factorial number
def factorial(n)
if n == 0
1
else
n * factorial(n-1)
end
end
def calulate_name(name)
return name.upcase
end
# If a GET request comes in at /, do the following.
get '/' do
# Get the parameter named guess and store it in pg
fib = params['Fibonacci']
fac = params['Factorial']
hi = params['Say hello']
# Array of all possible HTML color names
color_names = ["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGray","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","Darkorange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Gray","Grey","Green","GreenYellow","HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow","LightGray","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue","LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen","Magenta","Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue","MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue","MintCream","MistyRose","Moccasin","NavajoWhite","Navy","OldLace","Olive","OliveDrab","Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed","PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Purple","Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell","Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen","SteelBlue","Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White","WhiteSmoke","Yellow","YellowGreen"]
# Check for parameter to persist color across refreshes and backreferences
unless params.has_key?(:Color)
new_color = GlobalState[:new_color]
else
new_color = params['Color']
end
# Set to default if not in HTML color array
unless color_names.include? new_color
new_color = 'LightBlue'
end
GlobalState[:new_color] = new_color
erb :index, :locals => { fibonacci: fib, factorial: fac, say_hi: hi }
end
get '/fibonacci' do
# Display the fib number here
fib = params['Fibonacci']
if fib.to_i < 0 || (fib.to_i.to_s != fib)
fib = 1
end
fib_calc = fibonacci(fib.to_i)
erb :fibonacci, :locals => { fibonacci: fib, fibonacci_calculated: fib_calc}
end
get '/factorial' do
# Display the fac number here
fac = params['Factorial']
if fac.to_i < 0 || (fac.to_i.to_s != fac)
fac = 1
end
fac_calc = factorial(fac.to_i)
erb :factorial, :locals => { factorial: fac, factorial_calulated: fac_calc}
end
get '/sayhi' do
# Display the fac number here
name = params['Say hello']
uppercase_name = calulate_name(name)
erb :say_hi, :locals => { name: uppercase_name }
end
get '/showcat' do
cat_color = params['Cat color']
if !cat_color.nil?
cat_color.downcase
end
seed = params['seed'].to_i
if seed.nil?
prng = Random.new()
puts "#{seed} is a nil seed"
else
prng = Random.new(seed)
puts "#{seed} is a valid seed"
end
num = prng.rand 1..3
# Only cat colors are black, white, grey, and orange
if cat_color == "black" || cat_color == "white" || cat_color == "grey" || cat_color == "orange"
erb :show_cat, :locals => { color: cat_color, num: num }
else
erb :cat_error
end
end
not_found do
status 404
erb :error
end