Class: BnetScraper::Starcraft2::AchievementScraper
- Inherits:
-
BaseScraper
- Object
- BaseScraper
- BnetScraper::Starcraft2::AchievementScraper
- Defined in:
- lib/bnet_scraper/starcraft2/achievement_scraper.rb
Overview
This pulls achievement information for an account. Note that currently only returns the overall achievements, not the in-depth, by-category achievement information.
“‘ ruby scraper = BnetScraper::Starcraft2::AchievementScraper.new(
url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/'
) achievement_information = scraper.scrape achievement_information.size # => 6 achievement_information.first # => #<BnetScraper::Starcraft2::Achievement:0x007fef52b0b488 @description=“Win 50 Team Unranked or Ranked games as Zerg.”,
@earned=#<Date: 2013-04-04 ((2456387j,0s,0n),+0s,2299161j)>,
@title="50 Wins: Team Zerg">
achievement_information # => :swarm_campaign=>1120,
:matchmaking=>1410,
:custom_game=>120,
:arcade=>220,
:exploration=>530
achievement_information.size # => 5 achievement_information.first # => #<BnetScraper::Starcraft2::Achievement:0x007fef52abcb08 @description=“Finish a Qualification Round with an undefeated record.”,
@title="Hot Shot">
“‘
Instance Attribute Summary collapse
-
#progress ⇒ Object
readonly
Returns the value of attribute progress.
-
#recent ⇒ Object
readonly
Returns the value of attribute recent.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#showcase ⇒ Object
readonly
Returns the value of attribute showcase.
Attributes inherited from BaseScraper
#account, #bnet_id, #bnet_index, #region, #url
Instance Method Summary collapse
- #convert_date(date) ⇒ Object
-
#extract_recent_achievement(num) ⇒ Achievement
Scrapes recent achievement by position in the sidebar.
-
#get_response ⇒ Nokogiri::HTML
retrieves the account’s achievements overview page HTML for scraping.
- #output ⇒ Object
- #scrape ⇒ Object
-
#scrape_progress ⇒ Hash
Scrapes the progress of each achievement category from the account’s achievements overview page and returns them as a hash.
-
#scrape_recent ⇒ Array<Achievement>
scrapes the recent achievements from the account’s achievements overview page.
-
#scrape_showcase ⇒ Array<Achievement>
Scrapes the showcase achievements from the account’s achievements overview page.
Methods inherited from BaseScraper
#extract_data_from_options, #extract_data_from_url, #initialize, #profile_url, #region_info, #set_bnet_index, #valid?
Constructor Details
This class inherits a constructor from BnetScraper::Starcraft2::BaseScraper
Instance Attribute Details
#progress ⇒ Object (readonly)
Returns the value of attribute progress.
36 37 38 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 36 def progress @progress end |
#recent ⇒ Object (readonly)
Returns the value of attribute recent.
36 37 38 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 36 def recent @recent end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
36 37 38 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 36 def response @response end |
#showcase ⇒ Object (readonly)
Returns the value of attribute showcase.
36 37 38 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 36 def showcase @showcase end |
Instance Method Details
#convert_date(date) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 85 def convert_date date dates = date.scan(/(\d+)\/(\d+)\/(\d+)/).first.map(&:to_i) if region == 'na' month, day, year = dates else day, month, year = dates end Date.new year, month, day end |
#extract_recent_achievement(num) ⇒ Achievement
Scrapes recent achievement by position in the sidebar
75 76 77 78 79 80 81 82 83 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 75 def extract_recent_achievement num if div = response.css("#achv-recent-#{num}") Achievement.new({ title: div.children[1].inner_text, description: div.children[2].inner_text.strip, earned: convert_date(response.css(".recent-tile")[num].css('span')[1].inner_text) }) end end |
#get_response ⇒ Nokogiri::HTML
retrieves the account’s achievements overview page HTML for scraping
49 50 51 52 53 54 55 56 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 49 def get_response response = Faraday.get "#{profile_url}achievements/" if response.success? @response = Nokogiri::HTML(response.body) else raise BnetScraper::InvalidProfileError end end |
#output ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 130 def output { recent: @recent, progress: @progress, showcase: @showcase } end |
#scrape ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 38 def scrape get_response scrape_recent scrape_progress scrape_showcase output end |
#scrape_progress ⇒ Hash
Scrapes the progress of each achievement category from the account’s achievements overview page and returns them as a hash
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 103 def scrape_progress keys = [:liberty_campaign, :swarm_campaign, :matchmaking, :custom_game, :arcade, :exploration] index = 1 @progress = keys.inject({}) do |hash, key| hash[key] = response.css(".progress-tile:nth-child(#{index}) .profile-progress span").inner_text.to_i index += 1 hash end end |
#scrape_recent ⇒ Array<Achievement>
scrapes the recent achievements from the account’s achievements overview page
61 62 63 64 65 66 67 68 69 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 61 def scrape_recent @recent = [] response.css(".recent-tile").size.times do |num| achievement = extract_recent_achievement num @recent.push(achievement) if achievement end @recent end |
#scrape_showcase ⇒ Array<Achievement>
Scrapes the showcase achievements from the account’s achievements overview page
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bnet_scraper/starcraft2/achievement_scraper.rb', line 118 def scrape_showcase @showcase = response.css("#showcase-module .progress-tile").map do |achievement| if !achievement.values.first.split.include? 'empty' Achievement.new({ title: achievement.css('.tooltip-title').inner_text.strip, description: achievement.children[3].children[2].inner_text.strip }) end end @showcase end |