Class: Nokogiri::HTML::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/pismo.rb

Overview

Add some sugar to Nokogiri

Instance Method Summary collapse

Instance Method Details

#get_the(search) ⇒ Object



50
51
52
# File 'lib/pismo.rb', line 50

def get_the(search)
  self.search(search).first rescue nil
end

#match(queries = [], all = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pismo.rb', line 54

def match(queries = [], all = false)
  r = [] if all
  [*queries].each do |query|
    if query.is_a?(String)
      if el = self.search(query).first
        if el.name.downcase == "meta"
          result = el['content'].strip rescue nil
        else
          result = el.inner_text.strip rescue nil
        end
      end
    elsif query.is_a?(Array)
      result = query[1].call(self.search(query.first).first).strip rescue nil
    end

    if result
      # TODO: Sort out sanitization in a more centralized way
      result.gsub!('', '\'')
      result.gsub!('', '-')
      if all
        r << result
      else
        return result
      end
    end
  end
  all && !r.empty? ? r : nil
end