Module: Murlsh::Doc

Defined in:
lib/murlsh/doc.rb

Overview

Nokogiri doc mixin.

Instance Method Summary collapse

Instance Method Details

#descriptionObject

Get the meta description of the document.



27
28
29
30
31
# File 'lib/murlsh/doc.rb', line 27

def description
  xpath_search(
    "//html/head/meta[@name='description']"
    ) { |node| node['content'] }
end

#titleObject

Get the title of the document.



17
18
19
20
21
22
23
24
# File 'lib/murlsh/doc.rb', line 17

def title
  xpath_search(%w{
    //html/head/title
    //head/title
    //html/title
    //title
    }) { |node| node.inner_html }
end

#xpath_search(xpaths) ⇒ Object

Check a list of xpaths in order and yield and return the node matching the first one that is not nil



8
9
10
11
12
13
14
# File 'lib/murlsh/doc.rb', line 8

def xpath_search(xpaths)
  Array(xpaths).each do |xpath|
    selection = (self/xpath).first
    if selection; return (yield selection); end
  end
  nil
end