Class: NewGameScores::CollectionScraper

Inherits:
Object
  • Object
show all
Defined in:
lib/new_game_scores/collection_scraper.rb

Constant Summary collapse

@@collection =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, score = nil) ⇒ CollectionScraper

Returns a new instance of CollectionScraper.



7
8
9
10
# File 'lib/new_game_scores/collection_scraper.rb', line 7

def initialize(url = nil, score = nil)
  @url = url 
  @score = score
end

Instance Attribute Details

#game_urlObject

Returns the value of attribute game_url.



3
4
5
# File 'lib/new_game_scores/collection_scraper.rb', line 3

def game_url
  @game_url
end

#metascoreObject

Returns the value of attribute metascore.



3
4
5
# File 'lib/new_game_scores/collection_scraper.rb', line 3

def metascore
  @metascore
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/new_game_scores/collection_scraper.rb', line 3

def name
  @name
end

#scoreObject

Returns the value of attribute score.



3
4
5
# File 'lib/new_game_scores/collection_scraper.rb', line 3

def score
  @score
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/new_game_scores/collection_scraper.rb', line 3

def title
  @title
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/new_game_scores/collection_scraper.rb', line 3

def url
  @url
end

#userscoreObject

Returns the value of attribute userscore.



3
4
5
# File 'lib/new_game_scores/collection_scraper.rb', line 3

def userscore
  @userscore
end

Class Method Details

.collectionObject



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

def self.collection
  @@collection 
end

Instance Method Details

#clearObject



16
17
18
# File 'lib/new_game_scores/collection_scraper.rb', line 16

def clear
  @@collection.clear
end

#display_listObject



35
36
37
38
39
40
41
42
43
# File 'lib/new_game_scores/collection_scraper.rb', line 35

def display_list
  @@collection.each_with_index do |game, i|
    game.each do |key, value|
      if key = :metasocre && value.to_i >= @score.to_i
        puts "  \n#{i+1}. #{game[:title]} \n \n     Metascore: #{game[:metascore]} \n     Userscore: #{game[:userscore]} \n     Release Date: #{game[:release_date]}\n     Game Link: #{game[:game_url]}\n \n"
      end
    end 
  end
end

#scrape_listObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/new_game_scores/collection_scraper.rb', line 20

def scrape_list
 game_list = Nokogiri::HTML(open(self.url, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE,  'User-Agent' => 'safari'))

  game_list.css("ol.list_products li.game_product").each do |game|
    
    @@collection << {
      :title => game.css("div.product_title a").text.strip,
      :metascore => game.css("div.metascore_w").text,
      :userscore => game.css("li.product_avguserscore span.textscore").text,
      :release_date => game.css("li.release_date span.data").text, 
      :game_url => 'http://www.metacritic.com/' +game.css("div.product_title a").attribute("href").text
    }
  end
end