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
40
41
42
43
44
45
46
47
48
|
# File 'lib/board_game_geek.rb', line 11
def self.top_games how_many
full_pages, remainder = self.break_down_total(how_many)
Array.new.tap do |games|
(1..(full_pages)).each do |page_number|
how_many = GAMES_PER_PAGE
how_many = remainder if page_number == full_pages && remainder > 0
doc = Nokogiri::HTML(open "#{RANKED_GAMES_URI}#{page_number}.html")
doc.css('table#collectionitems tr').each_with_index do |game_row, idx|
next if idx == 0
break if idx > how_many
cells = game_row.css("td").map(&:inner_text).map(&:strip)
name_and_date = cells[2].to_s.split("\n")
ranking = cells[0]
name = name_and_date[0]
release_date = name_and_date[2].to_s.strip[1..-2]
rating = cells[3]
image_url = game_row.css(".collection_thumbnail img").first["src"]
image_url.sub!("_mt", "_t")
games << OpenStruct.new(name: name,
ranking: ranking,
rating: rating,
release_date: release_date,
image_url: image_url)
end
end
end
end
|