Module: PaygatePk::Util::Html

Defined in:
lib/paygate_pk/util/html.rb

Overview

HTML parsing utilities

Class Method Summary collapse

Class Method Details

.extract_form(html, index: 0) ⇒ Object

Extract a specific <form> (default: first) into a hash of action/method/inputs



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/paygate_pk/util/html.rb', line 12

def extract_form(html, index: 0)
  doc  = parse(html)
  form = doc.css("form")[index]
  return nil unless form

  {
    action: form["action"],
    method: (form["method"] || "GET").upcase,
    inputs: form.css("input[name]").to_h { |i| [i["name"], i["value"]] }
  }
end

.first_anchor_href(html, selector: "a") ⇒ Object

NEW: Return the first anchor href (or nil) – handy for redirect pages Optionally pass a CSS selector (e.g., “a.pay-button”) if you need a specific link.



26
27
28
29
30
# File 'lib/paygate_pk/util/html.rb', line 26

def first_anchor_href(html, selector: "a")
  doc = parse(html)
  a   = doc.at(selector)
  a ? a["href"] : nil
end

.parse(html) ⇒ Object

— internals ———————————————————-



34
35
36
37
38
39
# File 'lib/paygate_pk/util/html.rb', line 34

def parse(html)
  Nokogiri::HTML5(html)
rescue NoMethodError
  # Fallback for environments without HTML5 parser
  Nokogiri::HTML(html)
end