Class: MetacriticGames::Game

Inherits:
Object
  • Object
show all
Extended by:
Concerns::Findable::ClassMethods, Concerns::Nameable::ClassMethods, Concerns::Persistable::ClassMethods
Includes:
Concerns::Persistable::InstanceMethods
Defined in:
lib/game.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Nameable::ClassMethods

create

Methods included from Concerns::Findable::ClassMethods

find_by_name, find_or_create_by_name

Methods included from Concerns::Persistable::ClassMethods

destroy_all

Methods included from Concerns::Persistable::InstanceMethods

#save

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  self.platform = []
  self.url = {}
  self.genre = []
  self.metascore = {}
  self.user_score = {}
end

Instance Attribute Details

#genreObject

Returns the value of attribute genre.



8
9
10
# File 'lib/game.rb', line 8

def genre
  @genre
end

#metascoreObject

Returns the value of attribute metascore.



8
9
10
# File 'lib/game.rb', line 8

def metascore
  @metascore
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/game.rb', line 8

def name
  @name
end

#platformObject

Returns the value of attribute platform.



8
9
10
# File 'lib/game.rb', line 8

def platform
  @platform
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/game.rb', line 8

def url
  @url
end

#user_scoreObject

Returns the value of attribute user_score.



8
9
10
# File 'lib/game.rb', line 8

def user_score
  @user_score
end

Class Method Details

.allObject



12
13
14
# File 'lib/game.rb', line 12

def self.all
  @@all
end

.create_games(game_array) ⇒ Object

creates the game object based on the array passed in from the controller. once the game is created, it scrapes it calls the scraper to scrape it’s platform-specific page, then assigns the values and creates genre objects based off scrape data



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/game.rb', line 35

def self.create_games(game_array)
  game_array.each do |game|
    MetacriticGames::CLI.progressbar.increment
    if game[:platform] == "XONE"               #metacritic naming for the xboxone does not follow standard pattern
      platform = MetacriticGames::Platform.all.find {|platform| platform.name == "Xbox One"}

      game.tap do |new_game|
        new_game = self.find_or_create_by_name(game[:name])
        new_game.add_platform(platform)
        new_game.url[:"#{game[:platform]}"] = game[:url]
        MetacriticGames::Scraper.scrape_game(new_game.url[:XONE]).each do |key,value|
          if value.is_a? Array
            value.each do |genre|
              new_genre = MetacriticGames::Genre.create_genre(genre)
              new_game.add_genre(new_genre)
            end
          elsif value.fetch(:platform) == "tbd"
            value[:platform] = "currently unavailable"
            new_game.send(("#{key}="), value)
          elsif value.fetch(:platform) == ""
            value[:platform] = "currently unavailable"
            new_game.send(("#{key}="), value)
          else
            new_game.send(("#{key}="), value)
          end
        end
        score_by_platform(new_game, platform)
      end
    elsif game[:platform] == "WIIU"               #metacritic naming for the wii u does not follow standard pattern
      platform = MetacriticGames::Platform.all.find {|platform| platform.name == "Wii U"}

      game.tap do |new_game|
        new_game = self.find_or_create_by_name(game[:name])
        new_game.add_platform(platform)
        new_game.url[:"#{game[:platform]}"] = game[:url]
        MetacriticGames::Scraper.scrape_game(new_game.url[:WIIU]).each do |key,value|
          if value.is_a? Array
            value.each do |genre|
              new_genre = MetacriticGames::Genre.create_genre(genre)
              new_game.add_genre(new_genre)
            end
          elsif value.fetch(:platform) == "tbd"
            value[:platform] = "currently unavailable"
            new_game.send(("#{key}="), value)
          elsif value.fetch(:platform) == ""
            value[:platform] = "currently unavailable"
            new_game.send(("#{key}="), value)
          else
            new_game.send(("#{key}="), value)
          end
        end
        score_by_platform(new_game, platform)
      end
    elsif game[:platform] == "VITA"               #metacritic naming for the ps vita does not follow standard pattern
      platform = MetacriticGames::Platform.all.find {|platform| platform.name == "PS Vita"}

      game.tap do |new_game|
        new_game = self.find_or_create_by_name(game[:name])
        new_game.add_platform(platform)
        new_game.url[:"#{game[:platform]}"] = game[:url]
        MetacriticGames::Scraper.scrape_game(new_game.url[:VITA]).each do |key,value|
          if value.is_a? Array
            value.each do |genre|
              new_genre = MetacriticGames::Genre.create_genre(genre)
              new_game.add_genre(new_genre)
            end
          elsif value.fetch(:platform) == "tbd"
            value[:platform] = "currently unavailable"
            new_game.send(("#{key}="), value)
          elsif value.fetch(:platform) == ""
            value[:platform] = "currently unavailable"
            new_game.send(("#{key}="), value)
          else
            new_game.send(("#{key}="), value)
          end
        end
        score_by_platform(new_game, platform)
      end
    else
      platform = MetacriticGames::Platform.all.find {|platform| platform.name == game[:platform]}

      game.tap do |new_game|
        new_game = self.find_or_create_by_name(game[:name])
        new_game.add_platform(platform)
        new_game.url[:"#{game[:platform]}"] = game[:url]
        MetacriticGames::Scraper.scrape_game(new_game.url[:"#{platform.name}"]).each do |key,value|
          if value.is_a? Array
            value.each do |genre|
              new_genre = MetacriticGames::Genre.create_genre(genre)
              new_game.add_genre(new_genre)
            end
          elsif value.fetch(:platform) == "tbd"
            value[:platform] = "currently unavailable"
            new_game.send(("#{key}="), value)
          elsif value.fetch(:platform) == ""
            value[:platform] = "currently unavailable"
            new_game.send(("#{key}="), value)
          else
            new_game.send(("#{key}="), value)
          end
        end
        score_by_platform(new_game, platform)
      end
    end
  end
end

.score_by_platform(game, platform) ⇒ Object

changes the key of the metacritic and user scores from the generic “:platform” to a unique platform.name key required for multiplatform releases



143
144
145
146
# File 'lib/game.rb', line 143

def self.score_by_platform(game, platform)
  game.metascore[platform.name.to_sym] = game.metascore.delete(:platform)
  game.user_score[platform.name.to_sym] = game.user_score.delete(:platform)
end

Instance Method Details

#add_genre(genre) ⇒ Object



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

def add_genre(genre)
  genre.add_game(self) unless genre.games.include?(self)
  self.genre << genre unless self.genre.include?(genre)
end

#add_platform(platform) ⇒ Object



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

def add_platform(platform)
  platform.add_game(self) unless platform.games.include?(self)
  self.platform << platform unless self.platform.include?(platform)
end