Class: BnetScraper::Starcraft2::LeagueScraper

Inherits:
BaseScraper
  • Object
show all
Defined in:
lib/bnet_scraper/starcraft2/league_scraper.rb

Overview

This pulls information on a specific league for a specific account. It is best used either in conjunction with a profile scrape that profiles a URL, or if you happen to know the specific league_id and can pass it as an option.

``` ruby
scraper = BnetScraper::Starcraft2::LeagueScraper.new(league_id: '12345', account: 'Demon', bnet_id: '2377239')
scraper.scrape

# => #<BnetScraper::Starcraft2::League:0x007f89eab7a680
@account="Demon",
@bnet_id="2377239",
@division="Bronze",
@name="Changeling Bravo",
@random=false,
@season="2013 Season 4",
@size="3v3">
```

Instance Attribute Summary collapse

Attributes inherited from BaseScraper

#account, #bnet_id, #bnet_index, #region, #url

Instance Method Summary collapse

Methods inherited from BaseScraper

#extract_data_from_options, #extract_data_from_url, #profile_url, #region_info, #set_bnet_index, #valid?

Constructor Details

#initialize(options = {}) ⇒ Hash

Returns league_data - Hash of data extracted

Parameters:

  • url (String)
    • The league URL on battle.net



26
27
28
29
30
31
32
33
34
35
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 26

def initialize options = {}
  super(options)

  if options[:url]
    @league_id = options[:url].match(/http:\/\/.+\/sc2\/.+\/profile\/.+\/\d{1}\/.+\/ladder\/(.+)(#current-rank)?/).to_a[1]
  else
    @league_id = options[:league_id]
    @url = "#{profile_url}ladder/#{@league_id}"
  end
end

Instance Attribute Details

#divisionObject (readonly)

Returns the value of attribute division



22
23
24
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 22

def division
  @division
end

#league_idObject (readonly)

Returns the value of attribute league_id



22
23
24
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 22

def league_id
  @league_id
end

#nameObject (readonly)

Returns the value of attribute name



22
23
24
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 22

def name
  @name
end

#randomObject (readonly)

Returns the value of attribute random



22
23
24
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 22

def random
  @random
end

#seasonObject (readonly)

Returns the value of attribute season



22
23
24
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 22

def season
  @season
end

#sizeObject (readonly)

Returns the value of attribute size



22
23
24
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 22

def size
  @size
end

Instance Method Details

#outputObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 54

def output
  {
    season: @season,
    size: @size,
    name: @name,
    division: @division,
    random: @random,
    bnet_id: @bnet_id,
    account: @account
  }
end

#scrapeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bnet_scraper/starcraft2/league_scraper.rb', line 37

def scrape
  @response = Faraday.get @url
  if @response.success?
    @response = Nokogiri::HTML(@response.body)
    value = @response.css(".data-title .data-label h3").inner_text().strip 
    header_regex = /(.+) -\s+(\dv\d)( Random)? (\w+)\s+Division (.+)/
    header_values = value.match(header_regex).to_a
    header_values.shift()
    @season, @size, @random, @division, @name = header_values
    
    @random = !@random.nil?
    League.new(output)
  else
    raise BnetScraper::InvalidProfileError
  end
end