Class: Spotlite::Person

Inherits:
Object
  • Object
show all
Defined in:
lib/spotlite/person.rb

Overview

Represents a person on IMDb.com

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(imdb_id, name = nil, credits_category = nil, credits_text = nil) ⇒ Person

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



14
15
16
17
18
19
20
# 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

Instance Attribute Details

#credits_categoryObject

Returns the value of attribute credits_category.



5
6
7
# File 'lib/spotlite/person.rb', line 5

def credits_category
  @credits_category
end

#credits_textObject

Returns the value of attribute credits_text.



5
6
7
# File 'lib/spotlite/person.rb', line 5

def credits_text
  @credits_text
end

#imdb_idObject

Returns the value of attribute imdb_id.



5
6
7
# File 'lib/spotlite/person.rb', line 5

def imdb_id
  @imdb_id
end

#nameObject

Returns name as a string



60
61
62
# File 'lib/spotlite/person.rb', line 60

def name
  @name
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/spotlite/person.rb', line 5

def response
  @response
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/spotlite/person.rb', line 5

def url
  @url
end

Class Method Details

.find(query) ⇒ Object

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



24
25
26
27
28
29
30
31
32
33
34
# 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

.search(params = {}) ⇒ Object

Returns a list of people as an array of Spotlite::Person objects Takes optional parameters as a hash See github.com/defeed/spotlite/wiki/Advanced-person-search for details



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spotlite/person.rb', line 39

def self.search(params = {})
  defaults = {
    view: 'simple',
    count: 250,
    start: 1,
    gender: 'male,female',
    sort: 'starmeter,asc'
  }
  params = defaults.merge(params)
  results = Spotlite::Client.get 'http://www.imdb.com/search/name', query: params
  results.css('td.name').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

Instance Method Details

#birth_dateObject

Returns birth date as a date



70
71
72
# File 'lib/spotlite/person.rb', line 70

def birth_date
  details.at("time[itemprop='birthDate']")['datetime'].parse_date rescue nil
end

#birth_nameObject

Returns name at birth as a string



65
66
67
# File 'lib/spotlite/person.rb', line 65

def birth_name
  details.at("#name-born-info a[href^='/name/']").text.strip rescue nil
end

#death_dateObject

Returns death date as a date



75
76
77
# 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) ⇒ Object

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


94
95
96
97
98
99
100
101
102
103
# 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_urlObject

Returns primary photo URL as a string



80
81
82
83
84
85
86
# 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