Class: Ahiru::Page
- Inherits:
-
Object
- Object
- Ahiru::Page
- Defined in:
- lib/ahiru/page.rb
Constant Summary collapse
- DEFAULT_PARAM_KEYS =
[:q, :s, :dc].freeze
Instance Method Summary collapse
-
#initialize(html) ⇒ Page
constructor
A new instance of Page.
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #results ⇒ Object
Constructor Details
#initialize(html) ⇒ Page
Returns a new instance of Page.
8 9 10 |
# File 'lib/ahiru/page.rb', line 8 def initialize(html) @html = html end |
Instance Method Details
#next_page ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# 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? ⇒ Boolean
33 34 35 |
# File 'lib/ahiru/page.rb', line 33 def next_page? _next_page != nil end |
#results ⇒ Object
12 13 14 15 16 17 18 19 20 |
# 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 |