Sha256: e53906d66508047d338983313a054ccac4ae99839d3a1f9535fef0d51ab118a4

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

class HofStats::Player

    attr_accessor :name, :year, :votes, :percent, :url, :games, :runs, :hits, :homers, :batting_avg, :era, :wlpercent, :strikeouts, :position

    @@all = []

    def self.new_from_index(p)
        self.new(
            p.css("td[data-stat='player']").text,
            p.css("th[data-stat='year_ID']").text,
            p.css("td[data-stat='votes']").text,
            p.css("td[data-stat='votes_pct']").text,
            "https://baseball-reference.com#{p.css("td a").attribute("href").text}"
            )
    end

    def initialize(name=nil, year=nil, votes=nil, percent=nil, url=nil)
        @name = name
        @year = year
        @votes = votes
        @percent = percent
        @url = url
        @@all << self
    end

    def self.all
        @@all
    end

    def games
        @games = doc.css("tfoot td[data-stat='G']").first.text
    end

    def runs
        @runs = doc.css("tfoot td[data-stat='R']").first.text
    end

    def hits
        @hits = doc.css("tfoot td[data-stat='H']").first.text
    end

    def homers
        @homers = doc.css("tfoot td[data-stat='HR']").first.text
    end

    def batting_avg
        @batting_avg = doc.css("tfoot td[data-stat='batting_avg']").first.text
    end

    def era
        @era = doc.css("tfoot td[data-stat='earned_run_avg']").first.text
    end

    def wlpercent
        @wlpercent = doc.css("tfoot td[data-stat='win_loss_perc']").first.text
    end

    def strikeouts
        @strikeouts = doc.css("tfoot td[data-stat='SO']").first.text
    end

    def position
        @position = doc.xpath("//*[@id='meta']/div[2]/p[1]/text()").text.strip
    end

    def doc
        @doc = Nokogiri::HTML(open(self.url))
    end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hof_stats-0.1.4 lib/hof_stats/player.rb
hof_stats-0.1.3 lib/hof_stats/player.rb
hof_stats-0.1.2 lib/hof_stats/player.rb
hof_stats-0.1.1 lib/hof_stats/player.rb