Class: Bgg::Game

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_data) ⇒ Game

Returns a new instance of Game.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bgg/game.rb', line 10

def initialize(game_data)
  @game_data = game_data

  @id = game_data['id'].to_i
  @names = game_data['name'].map{ |n| n['value'] }
  @name = game_data['name'].find{ |n| n.fetch('type', '') == 'primary'}['value']

  @alternate_names = @names.reject{ |name| name == @name }
  @artists = filter_links_for('boardgameartist')
  @categories = filter_links_for('boardgamecategory')
  @description = game_data['description'][0]
  @designers = filter_links_for('boardgamedesigner')
  @families = filter_links_for('boardgamefamily')
  @image = game_data['image'][0]
  @max_players = game_data['maxplayers'][0]['value'].to_i
  @mechanics = filter_links_for('boardgamemechanic')
  @min_players = game_data['minplayers'][0]['value'].to_i
  @playing_time = game_data['playingtime'][0]['value'].to_i
  @publishers = filter_links_for('boardgamepublisher')
  @recommended_minimum_age = game_data['minage'][0]['value'].to_i
  @thumbnail = game_data['thumbnail'][0]
  @year_published = game_data['yearpublished'][0]['value'].to_i
end

Instance Attribute Details

#alternate_namesObject (readonly)

Returns the value of attribute alternate_names.



3
4
5
# File 'lib/bgg/game.rb', line 3

def alternate_names
  @alternate_names
end

#artistsObject (readonly)

Returns the value of attribute artists.



3
4
5
# File 'lib/bgg/game.rb', line 3

def artists
  @artists
end

#categoriesObject (readonly)

Returns the value of attribute categories.



3
4
5
# File 'lib/bgg/game.rb', line 3

def categories
  @categories
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/bgg/game.rb', line 3

def description
  @description
end

#designersObject (readonly)

Returns the value of attribute designers.



3
4
5
# File 'lib/bgg/game.rb', line 3

def designers
  @designers
end

#familiesObject (readonly)

Returns the value of attribute families.



3
4
5
# File 'lib/bgg/game.rb', line 3

def families
  @families
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/bgg/game.rb', line 3

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image.



3
4
5
# File 'lib/bgg/game.rb', line 3

def image
  @image
end

#max_playersObject (readonly)

Returns the value of attribute max_players.



3
4
5
# File 'lib/bgg/game.rb', line 3

def max_players
  @max_players
end

#mechanicsObject (readonly)

Returns the value of attribute mechanics.



3
4
5
# File 'lib/bgg/game.rb', line 3

def mechanics
  @mechanics
end

#min_playersObject (readonly)

Returns the value of attribute min_players.



3
4
5
# File 'lib/bgg/game.rb', line 3

def min_players
  @min_players
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/bgg/game.rb', line 3

def name
  @name
end

#namesObject (readonly)

Returns the value of attribute names.



3
4
5
# File 'lib/bgg/game.rb', line 3

def names
  @names
end

#playing_timeObject (readonly)

Returns the value of attribute playing_time.



3
4
5
# File 'lib/bgg/game.rb', line 3

def playing_time
  @playing_time
end

#publishersObject (readonly)

Returns the value of attribute publishers.



3
4
5
# File 'lib/bgg/game.rb', line 3

def publishers
  @publishers
end

Returns the value of attribute recommended_minimum_age.



3
4
5
# File 'lib/bgg/game.rb', line 3

def recommended_minimum_age
  @recommended_minimum_age
end

#thumbnailObject (readonly)

Returns the value of attribute thumbnail.



3
4
5
# File 'lib/bgg/game.rb', line 3

def thumbnail
  @thumbnail
end

#year_publishedObject (readonly)

Returns the value of attribute year_published.



3
4
5
# File 'lib/bgg/game.rb', line 3

def year_published
  @year_published
end

Class Method Details

.find_by_id(game_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bgg/game.rb', line 34

def self.find_by_id(game_id)
  game_id = Integer(game_id)
  if game_id < 1
    raise ArgumentError.new('game_id must be greater than 0!')
  end

  game_data = BggApi.thing({id: game_id, type: 'boardgame'})
  unless game_data.has_key?('item')
    raise ArgumentError.new('Game does not exist')
  end
  game_data = game_data['item'][0]

  Game.new(game_data)
end