Module: ScrapCbf::Findable

Included in:
RankingsHelper, TeamsHelper
Defined in:
lib/scrap_cbf/helpers/lib/findable.rb

Overview

This module has methods for helping on the task of find specific component views (e.g table).

Instance Method Summary collapse

Instance Method Details

#find_table_by_header(elems, compare, regex = '[[:alpha:]]+', accuracy = 1.0) ⇒ Nokogiri::XML::Element?

Find in the Document the first table with single header and level that matches with such accurancy the argument compare.

This method uses Array#find to return object or nil.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/scrap_cbf/helpers/lib/findable.rb', line 13

def find_table_by_header(
  elems,
  compare,
  regex = '[[:alpha:]]+',
  accuracy = 1.0
)
  elems.find do |table|
    # check only single level header
    thead = table.css('thead').first
    return false if !thead || thead.css('tr').length > 1

    header = thead.text.scan(Regexp.new(regex))

    return false if header.empty?

    matches = (compare & header).length
    (header.length / matches) >= accuracy
  end
end