diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..34e2817 Binary files /dev/null and b/.DS_Store differ diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 8b13789..f7c1f5b 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -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 diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index 8b13789..33d03dd 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -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