Sha256: fc297c486c8f4a04f5066196fa2ade771de3e66f9e8b2db6bd1263dedbca74b0

Contents?: true

Size: 1.77 KB

Versions: 5

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 Players < ::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

5 entries across 5 versions & 1 rubygems

Version Path
ehbrs_ruby_utils-0.29.0 lib/ehbrs_ruby_utils/bga/parsers/table/players.rb
ehbrs_ruby_utils-0.28.0 lib/ehbrs_ruby_utils/bga/parsers/table/players.rb
ehbrs_ruby_utils-0.27.1 lib/ehbrs_ruby_utils/bga/parsers/table/players.rb
ehbrs_ruby_utils-0.27.0 lib/ehbrs_ruby_utils/bga/parsers/table/players.rb
ehbrs_ruby_utils-0.26.0 lib/ehbrs_ruby_utils/bga/parsers/table/players.rb