class Spotlite::Person

Represents a person on IMDb.com

Attributes

credits_category[RW]
credits_text[RW]
imdb_id[RW]
name[RW]
response[RW]
url[RW]

Public Class Methods

find(query) click to toggle source

Returns a list of people as an array of Spotlite::Person objects Takes single parameter and searches for people by names and nicknames

# File lib/spotlite/person.rb, line 24
def self.find(query)
  results = Spotlite::Client.get 'http://www.imdb.com/find', query: {q: query, s: 'nm'}
  results.css('.result_text').map do |result|
    imdb_id = result.at('a')['href'].parse_imdb_id
    name    = result.at('a').text.strip

    [imdb_id, name]
  end.map do |values|
    self.new(*values)
  end
end
new(imdb_id, name = nil, credits_category = nil, credits_text = nil) click to toggle source

Initialize a new person object by its IMDb ID as a string

person = Spotlite::Person.new('0005132')

Spotlite::Person class objects are lazy loading. No HTTP request will be performed upon object initialization. HTTP request will be performed once when you use a method that needs remote data

# File lib/spotlite/person.rb, line 14
def initialize(imdb_id, name = nil, credits_category = nil, credits_text = nil)
  @imdb_id = "%07d" % imdb_id.to_i
  @name = name
  @url = "http://www.imdb.com/name/nm#{@imdb_id}/"
  @credits_category = credits_category if credits_category
  @credits_text = credits_text if credits_text
end

Public Instance Methods

birth_date() click to toggle source

Returns birth date as a date

# File lib/spotlite/person.rb, line 70
def birth_date
  details.at("time[itemprop='birthDate']")['datetime'].parse_date rescue nil
end
birth_name() click to toggle source

Returns name at birth as a string

# File lib/spotlite/person.rb, line 65
def birth_name
  details.at("#name-born-info a[href^='/name/']").text.strip rescue nil
end
death_date() click to toggle source

Returns death date as a date

# File lib/spotlite/person.rb, line 75
def death_date
  details.at("time[itemprop='deathDate']")['datetime'].parse_date rescue nil
end
filmography(extended = false, flatten = true) click to toggle source

Returns either a hash or an array of movies comprising person's filmography Returns array if `flatten = true`, returns hash if `flatten = false`. Hash keys are symbols of either 4 basic jobs (director, actor/actress, writer, producer)

or person's all available jobs

`extended = true` will return all available jobs, `extended = false` will return

movies under those 4 basic jobs
# File lib/spotlite/person.rb, line 94
def filmography(extended = false, flatten = true)
  hash = {}
  jobs = extended ? available_jobs : %w(director actor actress writer producer)

  jobs.map do |job|
    hash[job.to_sym] = parse_movies(job) if available_jobs.include?(job)
  end

  flatten ? hash.values.flatten : hash
end
photo_url() click to toggle source

Returns primary photo URL as a string

# File lib/spotlite/person.rb, line 80
def photo_url
  src = details.at('#img_primary img')['src'] rescue nil

  if src =~ /^(http:.+@@)/ || src =~ /^(http:.+?)\.[^\/]+$/
    $1 + '.jpg'
  end
end

Private Instance Methods

available_jobs() click to toggle source

Returns a list of all jobs a person took part in movies as an array of strings Used to retrieve person's filmography

# File lib/spotlite/person.rb, line 109
def available_jobs
  details.at('#filmography').css("div[data-category$='Movie']").map{ |job| job['data-category'].gsub('Movie', '') }
end
parse_movies(job) click to toggle source

Returns a list of movies that fall under a certain job type, as an array of Spotlite::Movie

# File lib/spotlite/person.rb, line 114
def parse_movies(job)
  details.css("div[id^='#{job}Movie-tt']").map do |row|
    imdb_id = row.at("a[href^='/title/tt']")['href'].parse_imdb_id
    title   = row.at('a').text.strip
    year    = row.at('.year_column').text.parse_year

    [imdb_id, title, year]
  end.map do |values|
    Spotlite::Movie.new(*values)
  end
end