Class: SteamUpcoming::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/steam-upcoming/scraper.rb

Overview

The SteamUpcoming::Scraper class is responsible for scraping information off of the target webpage and outputting hashes of information to be used by the SteamUpcoming::Game class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/steam-upcoming/scraper.rb', line 5

def name
  @name
end

#platformsObject

Returns the value of attribute platforms.



5
6
7
# File 'lib/steam-upcoming/scraper.rb', line 5

def platforms
  @platforms
end

#release_dateObject

Returns the value of attribute release_date.



5
6
7
# File 'lib/steam-upcoming/scraper.rb', line 5

def release_date
  @release_date
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/steam-upcoming/scraper.rb', line 5

def url
  @url
end

Class Method Details

.convert_platforms(platform_array) ⇒ Object

need to convert the platform array items into actual names, because the source is an image, not



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/steam-upcoming/scraper.rb', line 29

def self.convert_platforms(platform_array) #need to convert the platform array items into actual names, because the source is an image, not
  platforms = platform_array.map! do |platform|
    if platform.include?("win")
      platform = "Windows"
    elsif platform.include?("mac")
      platform = "Mac"
    elsif platform.include?("linux")
      platform = "Linux"
    elsif platform.include?("steamplay")
      platform = "Steam Play"
    elsif platform.include?("htcvive")
      platform = "HTC Vive"
    elsif platform.include?("oculusrift")
      platform = "Oculus Rift"
    else
      platform = nil
    end
  end
  platforms.select {|platform| platform != nil}
end

.gather_platforms(parent) ⇒ Object

gather a list of platforms, then return an array of legit platforms



22
23
24
25
26
27
# File 'lib/steam-upcoming/scraper.rb', line 22

def self.gather_platforms(parent) #gather a list of platforms, then return an array of legit platforms
  platforms = parent.children.map do |platform|
    platform.attr('class')
  end
  platforms.select {|platform| platform != nil} 
end

.page_count(index_url) ⇒ Object



62
63
64
65
# File 'lib/steam-upcoming/scraper.rb', line 62

def self.page_count(index_url)
  doc = Nokogiri::HTML(open(index_url))
  page_count = doc.css(".search_pagination_right").first.children[5].text.to_i
end

.scrape_game_page(game_url) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/steam-upcoming/scraper.rb', line 50

def self.scrape_game_page(game_url)
  doc = Nokogiri::HTML(open(game_url))
  about = doc.css(".game_description_snippet")
  tags = doc.css(".glance_tags a").map {|tag| tag.text}
  details = doc.css(".game_area_details_specs")
  game_attributes_hash = {
    :about => about.text.match(/\r|\n|\t/) ? about.text.delete("\t").delete("\r").delete("\n") : about.text,
    :tags => tags.map {|tag| tag.match(/\r|\n|\t/) ? tag.delete("\t").delete("\r").delete("\n") : tag },
    :details => details.map {|child| child.text} 
  }
end

.scrape_index_page(index_url) ⇒ Object

scrape the page and create an array of hashes (1 hash for each game)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/steam-upcoming/scraper.rb', line 7

def self.scrape_index_page(index_url) #scrape the page and create an array of hashes (1 hash for each game)
  doc = Nokogiri::HTML(open(index_url))
  game_array = []
  doc.css(".search_result_row").each do |game|
    game_hash = {
      :name=> game.css(".title").text, 
      :release_date=> game.css(".search_released").text,  
      :platforms=> self.convert_platforms(gather_platforms(game.css(".search_name p"))), 
      :url=> game.first[1]
    }
    game_array << game_hash
  end
  game_array
end