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
40 changes: 40 additions & 0 deletions lib/birthday_list.rb
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
class BirthdayList
def initialize
@list = []
end

def add(name, birthday)
@list << { name: name, birthday: birthday }
end

def show
@list.each {
|friend_bday| puts "#{friend_bday[:name]}, #{friend_bday[:birthday]}"
}
end

DATE_FORMAT = '%d-%m'

def check
@list.each do |bday|
puts "#{bday[:name]}'s birthday. #{age(bday)} years old" if today?(bday)
end
end

private
def today?(birthday)
birthday_date(birthday) == today_day
end

def birthday_date(birthday)
Date.parse(birthday[:birthday]).strftime(DATE_FORMAT)
end

def today_day
Time.now.strftime(DATE_FORMAT)
end

def age(birthday)
Time.now.year - Date.parse(birthday[:birthday]).year
end

end
70 changes: 70 additions & 0 deletions spec/birthday_list_spec.rb
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
require 'birthday_list'
require 'timecop'

describe BirthdayList do

before :example do
@birthday_list = BirthdayList.new
end

it 'adds the birthday of one friend' do

name = 'James'
birthday = '23/08/1993'

expect(@birthday_list.add(name, birthday)).to eq([{ name: name, birthday: birthday }])
end

it 'shows the birthday of two friends' do

@birthday_list.add('James', '23/08/1993')
expect(@birthday_list.add('Maria', '05/10/1975')).to eq([{ name: 'James', birthday: '23/08/1993' }, { name: 'Maria', birthday: '05/10/1975' }])
end

describe '#show' do
it 'shows one friend at once on a line' do

name = 'James'
birthday = '23/08/1993'

@birthday_list.add(name, birthday)
expect { @birthday_list.show }.to output("James, 23/08/1993\n").to_stdout
end

it 'shows two friends at once on a line' do

name_james = 'James'
bday_james = '23/08/1993'
name_maria = 'Maria'
bday_maria = '10/12/1945'

@birthday_list.add(name_james, bday_james)
@birthday_list.add(name_maria, bday_maria)

expect { @birthday_list.show }.to output("#{name_james}, #{bday_james}\n#{name_maria}, #{bday_maria}\n").to_stdout
end
end

describe '#check' do
context 'checks if today any friend has birthday' do
it 'shows message for friend who has birthday today' do
Timecop.freeze(Time.parse('05/05/2021'))

name = 'James'
birthday = '05/05/1923'

@birthday_list.add(name, birthday)
expect { @birthday_list.check }.to output("James's birthday. 98 years old\n").to_stdout
end

it 'shows nothing when no friend has birthday today' do
Timecop.freeze(Time.parse('05/05/2021'))

name = 'James'
birthday = '09/05/1923'

@birthday_list.add(name, birthday)
expect { @birthday_list.check }.to output("").to_stdout
end
end
end
end