Module: RockautoApi::Parsers::HtmlHelpers

Defined in:
lib/rockauto_api/parsers/html_helpers.rb

Constant Summary collapse

BASE_URL =
"https://www.rockauto.com"

Class Method Summary collapse

Class Method Details

.extract_csrf_token(html_or_doc, name = "_nck") ⇒ Object



23
24
25
26
27
# File 'lib/rockauto_api/parsers/html_helpers.rb', line 23

def extract_csrf_token(html_or_doc, name = "_nck")
  doc = html_or_doc.is_a?(Nokogiri::HTML::Document) ? html_or_doc : Nokogiri::HTML(html_or_doc)
  input = doc.at_css("input[name='#{name}']")
  input&.attr("value")
end

.extract_javascript_variable(html, var_name) ⇒ Object



29
30
31
# File 'lib/rockauto_api/parsers/html_helpers.rb', line 29

def extract_javascript_variable(html, var_name)
  html.match(/window\.#{Regexp.escape(var_name)}\s*=\s*"([^"]+)"/)&.captures&.first
end


46
47
48
49
50
51
52
53
# File 'lib/rockauto_api/parsers/html_helpers.rb', line 46

def find_links(html_or_doc, pattern = nil)
  doc = html_or_doc.is_a?(Nokogiri::HTML::Document) ? html_or_doc : Nokogiri::HTML(html_or_doc)
  links = doc.css("a")
  if pattern
    links = links.select { |a| a.text.strip.match?(pattern) }
  end
  links.map { |a| { text: a.text.strip, href: make_absolute_url(a["href"]) } }
end

.make_absolute_url(href) ⇒ Object



33
34
35
36
37
# File 'lib/rockauto_api/parsers/html_helpers.rb', line 33

def make_absolute_url(href)
  return nil if href.nil? || href.empty?
  return href if href.start_with?("http")
  href.start_with?("/") ? "#{BASE_URL}#{href}" : "#{BASE_URL}/#{href}"
end

.select_options(html_or_doc, select_id_or_css) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rockauto_api/parsers/html_helpers.rb', line 10

def select_options(html_or_doc, select_id_or_css)
  doc = html_or_doc.is_a?(Nokogiri::HTML::Document) ? html_or_doc : Nokogiri::HTML(html_or_doc)
  select = doc.at_css("select##{select_id_or_css}") || doc.at_css(select_id_or_css)
  return [] unless select

  select.css("option").map { |opt|
    value = opt["value"]
    text = opt.text.strip
    next nil if value.nil? || value.empty? || text.empty?
    { value: value, text: text }
  }.compact
end

.table_rows(html_or_doc, min_cells: 2) ⇒ Object



39
40
41
42
43
44
# File 'lib/rockauto_api/parsers/html_helpers.rb', line 39

def table_rows(html_or_doc, min_cells: 2)
  doc = html_or_doc.is_a?(Nokogiri::HTML::Document) ? html_or_doc : Nokogiri::HTML(html_or_doc)
  doc.css("table tr").select { |row| row.css("td").size >= min_cells }.map { |row|
    row.css("td, th").map { |cell| cell.text.strip }
  }
end