Class: FilmAffinity::Search
- Inherits:
-
Object
- Object
- FilmAffinity::Search
- Defined in:
- lib/filmaffinity/search.rb
Overview
Class Search
Instance Method Summary collapse
- #document_html ⇒ Object
- #exact_match? ⇒ Boolean
- #generate_html ⇒ Object
-
#initialize(query) ⇒ Search
constructor
A new instance of Search.
- #movies ⇒ Object
- #parse_movie ⇒ Object
- #parse_movies ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(query) ⇒ Search
Returns a new instance of Search.
6 7 8 9 |
# File 'lib/filmaffinity/search.rb', line 6 def initialize(query) @query = query @json_parser = JsonMoviesParser.new end |
Instance Method Details
#document_html ⇒ Object
19 20 21 |
# File 'lib/filmaffinity/search.rb', line 19 def document_html @document_html ||= Nokogiri::HTML(generate_html) end |
#exact_match? ⇒ Boolean
15 16 17 |
# File 'lib/filmaffinity/search.rb', line 15 def exact_match? !document_html.at('.z-movie').nil? end |
#generate_html ⇒ Object
23 24 25 |
# File 'lib/filmaffinity/search.rb', line 23 def generate_html open(Constants.urls[:search_by_title] % CGI.escape(@query)) end |
#movies ⇒ Object
11 12 13 |
# File 'lib/filmaffinity/search.rb', line 11 def movies @movies ||= (exact_match? ? parse_movie : parse_movies) end |
#parse_movie ⇒ Object
27 28 29 30 31 |
# 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 ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# 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 ⇒ Object
44 45 46 |
# File 'lib/filmaffinity/search.rb', line 44 def to_json @json_parser.to_json movies end |