Module: AtTheMovies::Parsers

Defined in:
lib/at_the_movies/parsers.rb,
lib/at_the_movies/parsers/review.rb,
lib/at_the_movies/parsers/interview.rb

Defined Under Namespace

Classes: Interview, Review

Constant Summary collapse

MAP =
{
  "review" => Review
}

Class Method Summary collapse

Class Method Details

.for(url, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/at_the_movies/parsers.rb', line 9

def self.for(url, options = {})
  tries = 0
  begin
    page = WWW::Mechanize.new.get(url)
  rescue => e
    retry if (tries += 1) < (options[:tries] || 5)
    raise AtTheMovies::ParserError,
      "Failed to parse #{url}. #{e.message}"
  end
  type = page.search('meta[@name="ABC-Author"]').first['content']
  return if options[:only] and type != options[:only]
  parser = MAP[type] || raise(ArgumentError, "Couldn't find a Parser class to parse a #{type.inspect} page")
  parser.parse(page)
end