After getting to know the use of servers, shotgun, and some research into Heroku, I tried out a git on the makers Github: making a birthday greeter!
The challenge was very open: just have a home page that asks for a name, and a birthday — and return to them either the number of days to their birthday, or wish them a happy birthday! I started by creating the ruby code — and RSpec of course!
require ‘./birthday_calculator’
describe BirthdayCalculator do
let(:person) { BirthdayCalculator.new(‘Name’)}
describe ‘#calculate_birthday’ do
it ‘should know how when my birthday is’ do
expect(person.calculate_birthday(02, 02)).to eq ‘033’
end
it ‘should know how many days to simons birthday’ do
expect(person.calculate_birthday(07, 31)).to eq ‘212’
end
end
describe ‘#calculate_current_day’ do
it ‘should calculate what day it is today’ do
expect(person.calculate_current_day).to be_a String
end
end
describe ‘#when_is_my_birthday’ do
it ‘should wish me a happy birthday if my birthday is today’ do
expect(person.when_is_my_birthday(03, 16)).to be_a String
end
it ‘should tell me my birthday is in 31 days’ do
expect(person.when_is_my_birthday(04, 16)).to be_a String
end
it ‘should tell me my birthday is in 360 days’ do
expect(person.when_is_my_birthday(03, 11)).to be_a String
end
end
end
To keep it simple, I want to assume that it is not a leap year — just because this year is not a leap year, and I don’t plan to look back at this repo in 3 years and expect to be impressed by it. I also only just learned about the Time gem, and after a spiral of looking up EVERYTHING that is to know about it, decided to keep it readable in this simple code. Next, I want to use both of these to decide if it is somebody’s birthday or not, and to respond appropriately.
class BirthdayCalculator
def initialize(name)
@name = name
end
def calculate_birthday(month, day, year = 2021)
birthday = Time.new(year, month, day)
birthday.strftime(‘%j’)
end
def calculate_current_day
Time.new.strftime(‘%j’)
end
def when_is_my_birthday(month, day, year=2021)
@birthday = calculate_birthday(month, day, year=2021)
@today = calculate_current_day
@birthday == @today ? happy_birthday : count
end
def happy_birthday
return “Happy birthday, #{@name.capitalize}!”
end
def count
if @birthday.to_i > @today.to_i
“#{@name.capitalize}, your birthday is in #{@birthday.to_i — @today.to_i} days!”
else
“#{@name.capitalize}, your birthday is in #{365 — (@today.to_i — @birthday.to_i)} days!”
end
end
end
The next step was to create a user interface. So that a user can use it on a browser!
require ‘sinatra/base’
require ‘./birthday_calculator’
class Birthday < Sinatra::Base
enable :sessions
set :sessions, true
set :session_secret, ‘*****’
get ‘/’ do
erb :index
end
post ‘/birthday’ do
$Birthday = BirthdayCalculator.new(params[:name])
$Birthday.when_is_my_birthday(params[:month], params[:day])
erb :birthday
end
end
To use this, I needed to create a home page, that I called index.erb, and a page it would be directed to after it had received the user input, that I would call a birthday.
<body>
<h1> Hallo! </h1>
<form class=’box_location’ action=’/birthday’ method=’POST’>
<h2> What is your name? </h2>
<div class=”name_box”>
<input type=’text’ name=’name’>
</div>
<div class=’birthday’>
<h2> When is your birthday?</h2>
<p> day </p>
<input type=’text’ name=’day’>
<p> month </p>
<input type=’text’ name=’month’>
</div>
<input type=’submit’ value=’go!’>
</div>
</form>
</body>
And
<body>
<h1> <%= $Birthday.when_is_my_birthday(params[:month], params[:day]) %> </h1>
</body>
As you might have noticed already, I put these into HTML body’s, so that I could put them in a general layout file.
<DOCTYPE! html>
<html>
<head>
<link rel=”stylesheet” href=”/stylesheet.css”/>
<title>verjaardags groeten</title>
</head>
<body>
<%= yield %>
</body>
</html>
I attached a CSS file to this layout file, to do the general styling of the page.
body {
background-color: lightpink;
justify-content: center;
font-family: Avenir;
text-emphasis-color: purple;
}
h1 {
text-align: center;
font-size: 75px;
}
h2 {
text-align: center;
}
p {
text-align: center;
}
.birthday {
margin-top: 50px;
}
input[type=”text” i] {
border: none;
text-align: center;
min-width: 500px;
max-width: 500px;
font-size: 0.8em;
}
input[type=”submit” i] {
min-width: 300;
max-width: 300;
border: none;
background-color: transparent;
font-family: Futura;
text-transform: uppercase;
color: black;
font-size: 0.8em;
border: 1px solid black;
padding-left: 0;
margin-top: 5px;
padding-bottom: 25px;
}
.box_location {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 800px;
margin-left: auto;
margin-right: auto;
margin-top: 0%;
}
To get it all to work the way I wanted to, I used the following gems:
source ‘https://rubygems.org'
gem ‘capybara’
gem ‘selenium-webdriver’
gem ‘sinatra’
gem ‘shotgun’, ‘~> 0.9.2’
gem ‘webrick’, ‘~> 1.7’
ruby “2.7.2”
You can check the final work out on Heroku: https://verjaardag.herokuapp.com
I did change the language to dutch — for my family — so don’t get too confused, it works the same as I explained.