Skip to content

Commit 8cdf573

Browse files
committed
Replace open-uri with Faraday
1 parent 053fe8c commit 8cdf573

File tree

7 files changed

+41
-25
lines changed

7 files changed

+41
-25
lines changed

bnet_scraper.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
1616
s.require_paths = ["lib"]
1717

1818
s.add_runtime_dependency 'nokogiri'
19+
s.add_runtime_dependency 'faraday'
1920
s.add_development_dependency 'rake'
2021
s.add_development_dependency 'rspec'
2122
s.add_development_dependency 'fakeweb'

lib/bnet_scraper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'bnet_scraper/starcraft2'
2-
require 'net/http'
3-
require 'open-uri'
2+
require 'faraday'
43
require 'nokogiri'
54

65
module BnetScraper

lib/bnet_scraper/starcraft2/achievement_scraper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def scrape
4343

4444
# retrieves the account's achievements overview page HTML for scraping
4545
def get_response
46-
@response = Nokogiri::HTML(open(profile_url+"achievements/"))
46+
response = Faraday.get "#{profile_url}achievements/"
47+
@response = Nokogiri::HTML(response.body)
4748
end
4849

4950
# scrapes the recent achievements from the account's achievements overview page

lib/bnet_scraper/starcraft2/league_scraper.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ def initialize options = {}
3030
end
3131

3232
def scrape
33-
@response = Nokogiri::HTML(open(@url))
34-
value = @response.css(".data-title .data-label h3").inner_text().strip
35-
header_regex = /Season (\d{1}) - \s+(\dv\d)( Random)? (\w+)\s+Division (.+)/
36-
header_values = value.match(header_regex).to_a
37-
header_values.shift()
38-
@season, @size, @random, @division, @name = header_values
39-
40-
@random = !@random.nil?
41-
output
33+
@response = Faraday.get @url
34+
if @response.success?
35+
@response = Nokogiri::HTML(@response.body)
36+
value = @response.css(".data-title .data-label h3").inner_text().strip
37+
header_regex = /Season (\d{1}) - \s+(\dv\d)( Random)? (\w+)\s+Division (.+)/
38+
header_values = value.match(header_regex).to_a
39+
header_values.shift()
40+
@season, @size, @random, @division, @name = header_values
41+
42+
@random = !@random.nil?
43+
output
44+
end
4245
end
4346

4447
def output

lib/bnet_scraper/starcraft2/match_history_scraper.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ def match_url
2525

2626
# retrieves the match history HTML for scraping
2727
def get_response
28-
@response = Nokogiri::HTML(open(match_url))
28+
@response = Faraday.get match_url
29+
30+
if @response.success?
31+
@response = Nokogiri::HTML(@response.body)
32+
end
2933
end
3034

3135
def scrape

lib/bnet_scraper/starcraft2/profile_scraper.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,30 @@ def scrape
3030

3131
# scrapes the profile page for basic account information
3232
def get_profile_data
33-
response = Nokogiri::HTML(open(profile_url))
33+
response = Faraday.get profile_url
3434

35-
@race = response.css("#season-snapshot .module-footer a").first().inner_html()
36-
@wins = response.css("#career-stats h2").inner_html()
37-
@achievement_points = response.css("#profile-header h3").inner_html()
35+
if response.success?
36+
html = Nokogiri::HTML(response.body)
37+
38+
@race = html.css("#season-snapshot .module-footer a").first().inner_html()
39+
@wins = html.css("#career-stats h2").inner_html()
40+
@achievement_points = html.css("#profile-header h3").inner_html()
41+
end
3842
end
3943

4044
# scrapes the league list from account's league page and sets an array of URLs
4145
def get_league_list
42-
response = Nokogiri::HTML(open(profile_url + "ladder/leagues"))
46+
response = Faraday.get profile_url + "ladder/leagues"
47+
if response.success?
48+
html = Nokogiri::HTML(response.body)
4349

44-
@leagues = response.css("a[href*='#current-rank']").map do |league|
45-
{
46-
name: league.inner_html().strip,
47-
id: league.attr('href').sub('#current-rank',''),
48-
href: "#{profile_url}ladder/#{league.attr('href')}"
49-
}
50+
@leagues = html.css("a[href*='#current-rank']").map do |league|
51+
{
52+
name: league.inner_html().strip,
53+
id: league.attr('href').sub('#current-rank',''),
54+
href: "#{profile_url}ladder/#{league.attr('href')}"
55+
}
56+
end
5057
end
5158
end
5259

lib/bnet_scraper/starcraft2/status_scraper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class Status
1717
class << self
1818

1919
def fetch
20-
Nokogiri::HTML(open(SOURCE))
20+
response = Faraday.get SOURCE
21+
Nokogiri::HTML(response.body)
2122
.css('.forumPost').first.css('span').to_a
2223
.each_slice(2).map { |i| { :region => i.first.text, :status => i.last.text } }
2324
end

0 commit comments

Comments
 (0)