Skip to content

Commit

Permalink
Accounts for movie person's death in age calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
lortza committed Sep 23, 2018
1 parent 0af1e6f commit ab7ece7
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 43 deletions.
10 changes: 7 additions & 3 deletions app/helpers/date_and_time_helper.rb
Expand Up @@ -8,15 +8,19 @@ def display_time(minutes)
full_hour?(minutes) ? display_time_in_hours(minutes) : display_time_in_hours_and_minutes(minutes)
end

def years_between_dates(starting_date:, ending_date:)
ending_date.year - starting_date.year - (date_occurred_yet_for_year?(starting_date, ending_date) ? 0 : 1)
end

def years_since_date(date)
today = Time.zone.today
today.year - date.year - (date_occurred_yet_this_year?(date, today) ? 0 : 1)
today.year - date.year - (date_occurred_yet_for_year?(date, today) ? 0 : 1)
end

private

def date_occurred_yet_this_year?(date, today)
today.month > date.month || (today.month == date.month && today.day >= date.day)
def date_occurred_yet_for_year?(starting_date, ending_date)
ending_date.month > starting_date.month || (ending_date.month == starting_date.month && ending_date.day >= starting_date.day)
end

def full_hour?(minutes)
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/movie_person_profiles_helper.rb
@@ -1,10 +1,10 @@
# frozen_string_literal: true

module MoviePersonProfilesHelper
def display_birthday_and_age(profile)
def display_birthday_info(profile)
return 'Not available' if profile.birthday.blank?

date = profile.birthday.to_date.stamp('January 1st, 2018')
"#{date} (Age: #{profile.age})"
profile.deathday.blank? ? "#{date} (Age: #{profile.age})" : "#{date}, Deceased (Age: #{profile.age})"
end
end
34 changes: 22 additions & 12 deletions app/models/movie_person_profile.rb
@@ -1,14 +1,14 @@
class MoviePersonProfile

def initialize(person_id:, name:, bio:, birthday:, profile_path:)
@person_id = person_id
@profile_path = profile_path
@name = name
@bio = bio
@birthday = birthday
def initialize(args)
@person_id = args[:person_id]
@profile_path = args[:profile_path]
@name = args[:name]
@bio = args[:bio]
@birthday = args[:birthday]
@deathday = args[:deathday]
end

attr_accessor :person_id, :name, :bio, :birthday, :profile_path
attr_accessor :person_id, :name, :bio, :birthday, :deathday, :profile_path

WIKIPEDIA_CREDIT = {
starting: 'From Wikipedia, the free encyclopedia.',
Expand All @@ -23,7 +23,8 @@ def parse_result(result)
profile_path: result[:profile_path],
name: result[:name],
bio: parse_bio(result[:biography]),
birthday: parse_birthday(result[:birthday])
birthday: parse_date(result[:birthday]),
deathday: parse_date(result[:deathday])
)
end

Expand All @@ -46,12 +47,21 @@ def wikipedia_credit?(bio)
bio.include?(WIKIPEDIA_CREDIT[:starting]) || bio.include?(WIKIPEDIA_CREDIT[:trailing])
end

def parse_birthday(birthday)
birthday.nil? ? '' : birthday
def parse_date(date)
date.nil? ? '' : date
end
end

def age
@birthday.blank? ? '' : DateAndTimeHelper.years_since_date(@birthday.clone.to_date)
return '' if @birthday.blank?

if @deathday.blank?
DateAndTimeHelper.years_since_date(@birthday.clone.to_date)
else
DateAndTimeHelper.years_between_dates(
starting_date: @birthday.clone.to_date,
ending_date: @deathday.clone.to_date
)
end
end
end
2 changes: 1 addition & 1 deletion app/views/tmdb/actor_more.html.erb
Expand Up @@ -4,7 +4,7 @@
<%= image_tag "http://image.tmdb.org/t/p/w185#{@person_profile.profile_path}" %>
<div class="contents">
<h1><%= @person_profile.name %></h1>
<h2>Born: <%= display_birthday_and_age(@person_profile) %></h2>
<h2>Born: <%= display_birthday_info(@person_profile) %></h2>
<p class="bio"><%= @person_profile.bio %></p>
</div> <!-- contents -->
</div> <!-- summary-box -->
Expand Down
2 changes: 1 addition & 1 deletion app/views/tmdb/director_search.html.erb
Expand Up @@ -4,7 +4,7 @@
<%= image_tag "http://image.tmdb.org/t/p/w185#{@person_profile.profile_path}" %>
<div class="contents">
<h1><%= @person_profile.name %></h1>
<h2>Born: <%= display_birthday_and_age(@person_profile) %></h2>
<h2>Born: <%= display_birthday_info(@person_profile) %></h2>
<p class="bio"><%= @person_profile.bio %></p>
</div> <!-- contents -->
</div> <!-- summary-box -->
Expand Down
18 changes: 17 additions & 1 deletion spec/factories/movie_person_profiles.rb
Expand Up @@ -5,13 +5,29 @@
sequence(:name) { |n| "name #{n}" }
sequence(:bio) { |n| "#{MoviePersonProfile::WIKIPEDIA_CREDIT[:starting]} \r\nbio #{n}\n\r#{MoviePersonProfile::WIKIPEDIA_CREDIT[:trailing]}" }
birthday { '2000-01-28' }
deathday { '2020-02-15' }

initialize_with do
new(person_id: person_id,
profile_path: profile_path,
name: name,
bio: bio,
birthday: birthday)
birthday: birthday,
deathday: deathday)
end

factory :living_movie_person_profile do
name { 'Brad Pitt' }
birthday { '1963-12-18' }
deathday { nil }
end

factory :deceased_movie_person_profile do
name { 'Judy Garland' }
birthday { '1922-06-10' }
deathday { '1969-06-22' }
end

end

end
27 changes: 27 additions & 0 deletions spec/helpers/date_and_time_helper_spec.rb
Expand Up @@ -24,4 +24,31 @@
expect(DateAndTimeHelper.years_since_date(year_ago)).to eq(1)
end
end

describe '.years_between_dates' do
it 'returns the difference in years between two given dates' do
year_ago = Time.zone.today - 366
this_year = Time.zone.today
expect(DateAndTimeHelper.years_between_dates(
starting_date: year_ago,
ending_date: this_year
)).to eq(1)
end

it 'takes into account whether the day has happened yet in the ending date' do
starting_date = '2000-06-01'.to_date
premature_ending_date = '2001-04-01'.to_date
mature_ending_date = '2001-12-01'.to_date

expect(DateAndTimeHelper.years_between_dates(
starting_date: starting_date,
ending_date: premature_ending_date
)).to eq(0)

expect(DateAndTimeHelper.years_between_dates(
starting_date: starting_date,
ending_date: mature_ending_date
)).to eq(1)
end
end
end
32 changes: 25 additions & 7 deletions spec/helpers/movie_person_profiles_helper_spec.rb
Expand Up @@ -4,25 +4,43 @@

describe MoviePersonProfilesHelper, type: :helper do
let(:profile) { build(:movie_person_profile) }
let(:living_profile) { build(:living_movie_person_profile) }
let(:deceased_profile) { build(:deceased_movie_person_profile) }

describe '#display_birthday_and_age' do

describe '#display_birthday_info' do
it 'includes the word version of the birthdate in the returned string' do
profile.birthday = '2000-01-28'

expect(display_birthday_and_age(profile).include?('January')).to be true
expect(display_birthday_info(profile).include?('January')).to be true
end

it "includes the person's current age in the returned string" do
profile.birthday = '2000-01-28'
current_age = DateAndTimeHelper.years_since_date(profile.birthday.to_date).to_s
it "includes a living person's current age in the returned string" do
current_age = DateAndTimeHelper.years_since_date(living_profile.birthday.to_date).to_s

expect(display_birthday_info(living_profile).include?(current_age)).to be true
end

it "includes a deceased person's age at death in the returned string" do
current_age = DateAndTimeHelper.years_between_dates(
starting_date: deceased_profile.birthday.to_date,
ending_date: deceased_profile.deathday.to_date
).to_s

expect(display_birthday_info(deceased_profile).include?(current_age)).to be true
end

it "indicates that a deceased person has deceased" do
keyword = 'deceased'

expect(display_birthday_and_age(profile).include?(current_age)).to be true
expect(display_birthday_info(deceased_profile).downcase.include?(keyword)).to be true
expect(display_birthday_info(living_profile).downcase.include?(keyword)).to be false
end

it "returns 'Not available' if there is no birthday data" do
profile.birthday = ''

expect(display_birthday_and_age(profile)).to eq('Not available')
expect(display_birthday_info(profile)).to eq('Not available')
end
end
end
70 changes: 54 additions & 16 deletions spec/models/movie_person_profile_spec.rb
Expand Up @@ -4,23 +4,53 @@

RSpec.describe MoviePersonProfile, type: :model do
let(:movie_person_profile) { build(:movie_person_profile) }
let(:living_movie_person_profile) { build(:living_movie_person_profile) }
let(:deceased_movie_person_profile) { build(:deceased_movie_person_profile) }

let(:results_for_known_person) {
{ birthday: '1926-06-28',
{ birthday: '1963-12-18',
known_for_department: 'Acting',
deathday: nil,
id: 14639,
name: 'Mel Brooks',
also_known_as: ['Melvin James Kaminsky '],
id: 287,
name: 'Brad Pitt',
also_known_as:
['برد پیت',
'William Bradley Pitt ',
'Бред Питт',
'Бред Пітт',
'Buratto Pitto',
'Брэд Питт',
'畢·彼特',
'ブラッド・ピット',
'브래드 피트',
'براد بيت',
'แบรด พิตต์'],
gender: 2,
biography: "#{MoviePersonProfile::WIKIPEDIA_CREDIT[:starting]} Melvin Brooks (né Kaminsky, born June 28, 1926) is an American filmmaker, comedian, actor and composer.\n\rHe is known as a creator of broad film farces and comic parodies.\r\nBrooks began his career as a comic and a writer for the early TV variety show Your Show of Shows.\r\n #{MoviePersonProfile::WIKIPEDIA_CREDIT[:trailing]}",
popularity: 1.644,
place_of_birth: 'Brooklyn, New York, USA',
profile_path: '/ndFo3LOYNCUghQTK833N1Wtuynr.jpg',
biography: "#{MoviePersonProfile::WIKIPEDIA_CREDIT[:starting]} William Bradley \"Brad\" Pitt (born December 18, 1963) is an American actor and film producer. Pitt has received tw and four Golden Globe Award nominations, winning one.\n\rHe has been described as one of the world's most attractive menreceived substantial media attention.\r\n #{MoviePersonProfile::WIKIPEDIA_CREDIT[:trailing]}",
popularity: 15.192,
place_of_birth: 'Shawnee, Oklahoma, USA',
profile_path: '/kU3B75TyRiCgE270EyZnHjfivoq.jpg',
adult: false,
imdb_id: 'nm0000316',
imdb_id: 'nm0000093',
homepage: nil }
}
}

# let(:results_for_known_person_who_died) {
# { birthday: '1922-06-10',
# known_for_department: 'Acting',
# deathday: '1969-06-22',
# id: 9066,
# name: 'Judy Garland',
# also_known_as: ['Joots Garland', 'Frances Ethel Gumm', 'Baby Gumm', '주디 갈랜드'],
# gender: 1,
# biography: "#{MoviePersonProfile::WIKIPEDIA_CREDIT[:starting]} Judy Garland (June 10, 1922 – June 22, 1969) was an American actress and singer.\n\rRespected for her versatility, she received a juvenile Academy Award, won a Golden Globe Award, as well as Grammy Awards and a Special Tony Award.\r\n #{MoviePersonProfile::WIKIPEDIA_CREDIT[:trailing]}",
# popularity: 2.62,
# place_of_birth: 'Grand Rapids, Minnesota, USA',
# profile_path: '/dkhMrLSfnqS3fmxWuXaEMwxN0Tf.jpg',
# adult: false,
# imdb_id: 'nm0000023',
# homepage: nil }
# }

let(:results_for_known_person_without_details) {
{ birthday: nil,
Expand Down Expand Up @@ -133,17 +163,17 @@
end
end

describe '.parse_birthday' do
describe '.parse_date' do
it 'returns an empty string if there is no date provided' do
birthday = nil
parsed_birthday = MoviePersonProfile.parse_birthday(birthday)
parsed_birthday = MoviePersonProfile.parse_date(birthday)

expect(parsed_birthday).to eq('')
end

it 'returns the date in string format if there is a date provided' do
birthday = '2018-01-31'
parsed_birthday = MoviePersonProfile.parse_birthday(birthday)
parsed_birthday = MoviePersonProfile.parse_date(birthday)

expect(parsed_birthday).to eq(birthday)
end
Expand All @@ -155,9 +185,17 @@
expect(movie_person_profile.age).to eq('')
end

it "returns a person's current age if a birthday is provided" do
calculated_age = DateAndTimeHelper.years_since_date(movie_person_profile.birthday.to_date)
expect(movie_person_profile.age).to eq(calculated_age)
it "returns a living person's current age if a birthday is provided" do
calculated_age = DateAndTimeHelper.years_since_date(living_movie_person_profile.birthday.to_date)
expect(living_movie_person_profile.age).to eq(calculated_age)
end

it "returns a deceased person's age at death age if a birth & death dates are provided" do
calculated_age = DateAndTimeHelper.years_between_dates(
starting_date: deceased_movie_person_profile.birthday.to_date,
ending_date: deceased_movie_person_profile.deathday.to_date
)
expect(deceased_movie_person_profile.age).to eq(calculated_age)
end
end
end

0 comments on commit ab7ece7

Please sign in to comment.