class FilmAffinity::Search

Class Search

Public Class Methods

new(query) click to toggle source
# File lib/filmaffinity/search.rb, line 6
def initialize(query)
  @query = query
  @json_parser = JsonMoviesParser.new
end

Public Instance Methods

document_html() click to toggle source
# File lib/filmaffinity/search.rb, line 19
def document_html
  @document_html ||= Nokogiri::HTML(generate_html)
end
exact_match?() click to toggle source
# File lib/filmaffinity/search.rb, line 15
def exact_match?
  !document_html.at('.z-movie').nil?
end
generate_html() click to toggle source
# File lib/filmaffinity/search.rb, line 23
def generate_html
  open(Constants.urls[:search_by_title] % CGI.escape(@query))
end
movies() click to toggle source
# File lib/filmaffinity/search.rb, line 11
def movies
  @movies ||= (exact_match? ? parse_movie : parse_movies)
end
parse_movie() click to toggle source
# File lib/filmaffinity/search.rb, line 27
def parse_movie
  id    = document_html.at('meta[property="og:url"]')['content'][/\d+/].to_i
  title = document_html.at('meta[property="og:title"]')['content']
  [FilmAffinity::Movie.new(id, title)]
end
parse_movies() click to toggle source
# File lib/filmaffinity/search.rb, line 33
def parse_movies
  movies = []
  document_html.search('.movie-card.movie-card-1').each do |movie_card|
    id = movie_card['data-movie-id'].to_i
    title = movie_card.search('.mc-title a').first.content.strip
    movie = FilmAffinity::Movie.new id, title
    movies << movie
  end
  movies
end
to_json() click to toggle source
# File lib/filmaffinity/search.rb, line 44
def to_json
  @json_parser.to_json movies
end