class Ahiru::Page

Constants

DEFAULT_PARAM_KEYS

Public Class Methods

new(html) click to toggle source
# File lib/ahiru/page.rb, line 8
def initialize(html)
  @html = html
end

Public Instance Methods

next_page() click to toggle source
# File lib/ahiru/page.rb, line 22
def next_page
  return nil unless next_page?

  params = doc.css("div.nav-link > form > input[type='hidden']").map do |input|
    key = input.get("name")
    value = input.get("value")
    [key.to_sym, value]
  end.to_h.slice(*DEFAULT_PARAM_KEYS)
  API.search(params)
end
next_page?() click to toggle source
# File lib/ahiru/page.rb, line 33
def next_page?
  _next_page != nil
end
results() click to toggle source
# File lib/ahiru/page.rb, line 12
def results
  doc.css(".result").map do |result|
    href = result.at_css("a.result__a")&.get("href")
    url = URI.decode_www_form(href).to_h["uddg"]
    title = result.at_css("a.result__a")&.text
    snippet = result.at_css("a.result__snippet")&.text
    Result.new(url: url, title: title, snippet: snippet)
  end
end

Private Instance Methods

_next_page() click to toggle source
# File lib/ahiru/page.rb, line 39
def _next_page
  @_next_page ||= doc.at_css("div.nav-link > form > input[value='Next']")
end
doc() click to toggle source
# File lib/ahiru/page.rb, line 43
def doc
  @doc ||= Oga.parse_html(@html)
end