Last active
April 6, 2018 13:22
-
-
Save bestwebua/6c4cf4a92bb605119a4f47be325d2f62 to your computer and use it in GitHub Desktop.
Scraping kata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'open-uri' | |
class User | |
attr_reader :name, :clan, :honor | |
def initialize(name, clan, honor) | |
@name, @clan, @honor = name, clan, honor | |
end | |
end | |
class Leaderboard | |
@@leaderboard = [] | |
def self.position | |
url = 'https://www.codewars.com/users/leaderboard' | |
page = Nokogiri::HTML(open(url), nil, Encoding::UTF_8.to_s) | |
rows = page.css("[class='leaderboard pan'] > table > tr") | |
rows.each do |row| | |
name, clan, honor = row.css('a').text, row.css('td[3]').text, row.css('td[4]').text.to_i | |
@@leaderboard << User.new(name, clan, honor) | |
end | |
@@leaderboard | |
end | |
end | |
def solution | |
Leaderboard.position | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@10XL my new code with struct instead class. It has passed all tests.