Class: TopBoardGames::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/TopBoardGames/game.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, rank = nil, url = nil, avg_rating = nil, game_id = nil) ⇒ Game

Returns a new instance of Game.



15
16
17
18
19
20
21
22
# File 'lib/TopBoardGames/game.rb', line 15

def initialize(title = nil, rank = nil, url = nil, avg_rating = nil, game_id=nil)
  @title = title
  @rank = rank
  @url = url
  @avg_rating = avg_rating
  @game_id = game_id
  @@all << self
end

Instance Attribute Details

#avg_ratingObject

Returns the value of attribute avg_rating.



2
3
4
# File 'lib/TopBoardGames/game.rb', line 2

def avg_rating
  @avg_rating
end

#game_idObject

Returns the value of attribute game_id.



2
3
4
# File 'lib/TopBoardGames/game.rb', line 2

def game_id
  @game_id
end

#rankObject

Returns the value of attribute rank.



2
3
4
# File 'lib/TopBoardGames/game.rb', line 2

def rank
  @rank
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/TopBoardGames/game.rb', line 2

def title
  @title
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/TopBoardGames/game.rb', line 2

def url
  @url
end

Class Method Details

.allObject



24
25
26
# File 'lib/TopBoardGames/game.rb', line 24

def self.all
  @@all
end

.new_from_main_page(game_row) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/TopBoardGames/game.rb', line 5

def self.new_from_main_page(game_row)
  title = game_row.css(".collection_objectname a").text
  url = game_row.css(".collection_objectname a").attr("href").text
  avg_rating = game_row.css(".collection_bggrating")[1].text.strip
  game_id = url[/(\/\d*\/)/].gsub("/", "")
  rank = game_row.css(".collection_rank").text.strip

  self.new(title, rank, url, avg_rating, game_id)
end

Instance Method Details

#descriptionObject



28
29
30
31
# File 'lib/TopBoardGames/game.rb', line 28

def description
  description_url = Nokogiri::HTML(open("https://boardgamegeek.com#{self.url}")).at("meta[name='description']")['content'].split("\n")
  @description ||= (description_url[0] + " " +description_url[2])
end

#max_playtimeObject



45
46
47
# File 'lib/TopBoardGames/game.rb', line 45

def max_playtime
  @max_playtime ||=stats_url.search("maxplaytime").first['value']
end

#min_ageObject



49
50
51
# File 'lib/TopBoardGames/game.rb', line 49

def min_age
  @min_age||=stats_url.search("minage").first['value']
end

#min_playtimeObject



41
42
43
# File 'lib/TopBoardGames/game.rb', line 41

def min_playtime
  @min_playtime ||=stats_url.search("minplaytime").first['value']
end

#stats_urlObject



33
34
35
# File 'lib/TopBoardGames/game.rb', line 33

def stats_url
  Nokogiri::HTML(open("https://www.boardgamegeek.com/xmlapi2/thing?id=#{self.game_id}&stats=1"))
end

#weightObject



37
38
39
# File 'lib/TopBoardGames/game.rb', line 37

def weight
  @weight ||=stats_url.search("averageweight").first['value']
end