Sha256: 24910029b6c8267b2f8f6f61add6cfd8fd4babfee8ce09509cc15c1fd1b3ec36

Contents?: true

Size: 1.77 KB

Versions: 17

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require 'aranha/parsers/html/item'
require 'aranha/parsers/html/item_list'
require 'dentaku'
require 'eac_ruby_utils/core_ext'

module EhbrsRubyUtils
  module Bga
    module Parsers
      class Table < ::Aranha::Parsers::Html::Item
        class EndedPlayers < ::Aranha::Parsers::Html::ItemList
          ITEMS_XPATH = '//div[starts-with(@id, "score_entry_")]'
          RANK_VALUES = { vencedor: 1, perdedor: 2 }.freeze

          field :rank, :string, './div[@class = "rank"]'
          field :id, :integer, './div[@class = "name"]/a/@href'
          field :name, :string, './div[@class = "name"]/a/text()'
          field :score, :integer_optional, './div[@class = "score"]'
          field :elo_increment, :string, './/*[starts-with(@id, "winpoints_value_")]/text()'
          field :elo_reached, :integer, './/*[@class = "gamerank_value"]/text()'
          field :penalty_clock, :boolean,
                './/*[@class = "clockpenalty" and @style = "display: inline;"]'
          field :penalty_leave, :boolean,
                './/*[@class = "leavepenalty" and @style = "display: inline;"]'

          def items_xpath
            ITEMS_XPATH
          end

          def item_data(data)
            %i[elo_increment rank].inject(data) do |a, e|
              a.merge(e => send("process_#{e}", data.fetch(e)))
            end
          end

          # @return [Integer, nil]
          def process_elo_increment(expression)
            return nil if expression.blank?

            ::Dentaku::Calculator.new.evaluate(expression.gsub(/\A\+/, '')).to_i
          end

          # @param value [String]
          # @return [Integer]
          def process_rank(source)
            RANK_VALUES[source.downcase.to_sym] || source.to_i
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ehbrs_ruby_utils-0.44.3 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.44.2 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.44.1 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.44.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.41.1 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.41.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.40.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.39.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.38.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.37.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.36.1 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.36.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.35.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.33.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.32.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.31.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb
ehbrs_ruby_utils-0.30.0 lib/ehbrs_ruby_utils/bga/parsers/table/ended_players.rb