Class: IMDB::Search

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/imdb/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, opts = {}) ⇒ Search

Returns a new instance of Search.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/imdb/search.rb', line 8

def initialize(title, opts={})
  @limit = opts[:limit] || 0

  if title =~ /^\s*$/
    self.results = []
    return self.results
  end

  doc = Hpricot(open(IMDB::TITLES_SEARCH_URL+CGI.escape(title)))

  # Single match
  unless (doc/"div#tn15.maindetails").empty?
    self.results = [Movie.new_from_doc(doc)]

  # Search result
  else
    links = (doc/"td > a[@href^='/title/']").delete_if { |a| a.inner_html =~ /^</ }
    self.results = links.collect! do |a|
      td = a.parent
      movie = Movie.new

      movie.id    = a.attributes["href"][/^\/title\/([^\/]+)/, 1]
      movie.title = IMDB.str_to_utf8(CGI.unescapeHTML(a.inner_html))
      movie.year  = td.inner_html[/<\/a>\s\((\d+)\)/, 1]
      movie.aka   = td.inner_html.scan(/aka\s+<em>"([^"]+)"<\/em>/).collect { |x| IMDB.str_to_utf8(CGI.unescapeHTML(x[0].strip)) }

      movie
    end
  end

  self.results = self.results.slice(0, @limit) if @limit != 0
end

Instance Attribute Details

#resultsObject

Returns the value of attribute results.



6
7
8
# File 'lib/imdb/search.rb', line 6

def results
  @results
end

Instance Method Details

#eachObject



41
42
43
# File 'lib/imdb/search.rb', line 41

def each
  self.results.each { |x| yield x }
end

#to_json(*a) ⇒ Object



45
46
47
# File 'lib/imdb/search.rb', line 45

def to_json(*a)
  self.results.to_json(*a)
end