Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
46 changes: 46 additions & 0 deletions lib/birthday_list.rb
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
require 'date'
require 'timecop'
class Birthdays

def initialize
@birthday_list = Array.new
end

def add(name, date)
@birthday_list.push({ name: name, birthday: date })
end

def show
@birthday_list.each { |person| puts person }
end

def this_year
Time.now.strftime("%Y").to_i
end

def birth_year(person)
person[:birthday].split('').last(4).join("").to_i
end

def birthday?(person)
person[:birthday].split("").first(5).join
end

def today?
Time.now.strftime("%d/%m")
end

def persons_age(person)
current_year = Time.now.strftime("%Y").to_i
birth_year = person[:birthday].split('').last(4).join("").to_i
current_year - birth_year
end

def check
@birthday_list.each do |person|
if birthday?(person) == today?
return "it is #{person[:name]}\'s birthday today, they are #{persons_age(person)} years old!"
end
end
end

end
50 changes: 50 additions & 0 deletions spec/birthday_list_spec.rb
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
require 'birthday_list'
require 'timecop'
describe "Birthdays" do

describe "add" do
it "returns a new birthday list" do
birthdays = Birthdays.new
expect(birthdays.add("Harry", "15/03/1802")).to eq [{name: "Harry", birthday: "15/03/1802"}]
end

end

describe "show" do
it "shows me a list of birthdays when there are 2 people" do
birthdays = Birthdays.new
birthdays.add("Harry", "15/03/1802")
birthdays.add("Joe", "22/04/2002")

expect(birthdays.show).to eq [{name: "Harry", birthday: "15/03/1802"}, {name: "Joe", birthday: "22/04/2002"}]
end

it "shows me a list of birthdays when there is one person" do
birthdays = Birthdays.new
birthdays.add("Harry", "15/03/1802")
expect(birthdays.show).to eq [{name: "Harry", birthday: "15/03/1802"}]
end
end


describe "check" do
it "returns 'Harrys birthday is today, he is 118 years old!'" do
birthdays = Birthdays.new
birthdays.add("Harry", "15/03/1802")
birthdays.add("Joe", "22/04/2002")
new_time = Time.local(1902, 03, 15, 12, 0, 0)
Timecop.freeze(new_time)

expect(birthdays.check).to eq "it is Harry's birthday today, they are 100 years old!"
end
it "returns nothing when its nobody's birthday" do
birthdays = Birthdays.new
birthdays.add("Harry", "15/03/1802")
birthdays.add("Joe", "22/04/2002")
new_time = Time.local(2002, 04, 15, 12, 0, 0)
Timecop.freeze(new_time)

expect { birthdays.check }.not_to output().to_stdout
end
end
end