Created
July 19, 2020 08:48
-
-
Save inem/695b09211ac24ef7649f1bf22b602d02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'oga' | |
require 'open-uri' | |
require 'pry' | |
url = './index.html' | |
html = open(url) | |
class FilmLibrary | |
def initialize(films) | |
@films = films | |
end | |
def sorted_by_rating_desc | |
libify @films.sort_by{|f| f.rating }.reverse | |
end | |
def filter_by_genre(genre) | |
FilmLibrary.new(@films.select{ |f| f.genres.include?(genre) }) | |
end | |
def filter_by_rating_more_than(rating) | |
libify @films.select{ |f| f.rating >= rating } | |
end | |
def genres | |
@films.map(&:genres).flatten.uniq | |
end | |
def first(n = 1) | |
libify @films.first(n) | |
end | |
def to_a | |
@films | |
end | |
private | |
def libify(films) | |
FilmLibrary.new(films) | |
end | |
end | |
class Film | |
attr_reader :title, :year, :genres, :rating | |
def initialize(title, year, genres, rating) | |
@title = title | |
@year = year.to_i | |
@genres = genres | |
@rating = rating.to_f | |
end | |
end | |
document = Oga.parse_html(html) | |
films = document.css('.lister-item').map do |item| | |
title = item.css('h3 a').text | |
year = item.css('.lister-item-year').text.delete("()") | |
genres = item.css('.genre').text.strip.split(/, ?/) | |
rating = item.css('div.ipl-rating-star span.ipl-rating-star__rating').first.text.sub(',', '.') | |
film = Film.new(title, year, genres, rating) | |
end | |
lib = FilmLibrary.new(films) | |
mini_lib = lib.sorted_by_rating_desc.first(2) | |
raise 'айай' if mini_lib.filter_by_genre('Comedy').to_a.size != 2 | |
raise 'айай2' if mini_lib.filter_by_genre('Romance').to_a.size != 1 | |
raise 'айай3' if lib.filter_by_rating_more_than(8).sorted_by_rating_desc.to_a.last.rating >= 8.0 | |
binding.pry | |
puts 'yay' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment