#!/usr/bin/env ruby
require 'cgi'
cgi = CGI.new
# ユーザー入力の取得
temperature = cgi['temperature'].to_i rescue nil
# 気温に基づいた提案を定義
def suggest_clothing_and_breakfast(temp)
case temp
when -Float::INFINITY..5
{ clothing: '厚手のコート、手袋、マフラー', breakfast: 'ホットココアとオートミール' }
when 6..15
{ clothing: 'セーターと軽いジャケット', breakfast: '温かいスープとパン' }
when 16..25
{ clothing: 'Tシャツとジーンズ', breakfast: 'サラダとトースト' }
else
{ clothing: '半袖とショートパンツ', breakfast: 'フルーツと冷たいスムージー' }
end
end
# 提案を取得
suggestion = temperature ? suggest_clothing_and_breakfast(temperature) : nil
# HTTPヘッダーを出力
print cgi.header("text/html; charset=UTF-8")
# HTML出力
print <<HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>気温による提案</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
padding: 20px;
}
h1 {
color: #333;
}
form {
margin-bottom: 20px;
}
input, button {
padding: 10px;
margin: 5px;
font-size: 16px;
}
.suggestion {
margin-top: 20px;
padding: 10px;
background-color: #e3f2fd;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>気温による提案</h1>
<form method="GET">
<label for="temperature">気温を入力してください(℃):</label>
<input type="number" id="temperature" name="temperature" required>
<button type="submit">決定</button>
</form>
HTML
if temperature
print <<HTML
<div class="suggestion">
<h2>入力された気温: #{temperature}℃</h2>
<p><strong>服装の提案:</strong> #{suggestion[:clothing]}</p>
<p><strong>朝ご飯の提案:</strong> #{suggestion[:breakfast]}</p>
</div>
HTML
end
print <<HTML
</body>
</html>
HTML