Sha256: e7e1cfadaa7e23cc64db8a799469384a45216926bd64f918854ff53998b718ca

Contents?: true

Size: 745 Bytes

Versions: 5

Compression:

Stored size: 745 Bytes

Contents

module Murlsh

  # Nokogiri doc mixin.
  module Doc

    # Check a list of xpaths in order and yield and return the node matching
    # the first one that is not nil
    def xpath_search(xpaths)
      [*xpaths].each do |xpath|
        selection = (self/xpath).first
        if selection; return (yield selection); end
      end
      nil
    end

    # Get the title of the document.
    def title
      xpath_search(%w{
        //html/head/title
        //head/title
        //html/title
        //title
        }) { |node| node.inner_html }
    end

    # Get the meta description of the document.
    def description
      xpath_search(
        "//html/head/meta[@name='description']"
        ) { |node| node['content'] }
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
murlsh-1.6.1 lib/murlsh/doc.rb
murlsh-1.6.0 lib/murlsh/doc.rb
murlsh-1.5.0 lib/murlsh/doc.rb
murlsh-1.4.1 lib/murlsh/doc.rb
murlsh-1.4.0 lib/murlsh/doc.rb