Class: MetaInspector::Parsers::TextsParser

Inherits:
Base
  • Object
show all
Defined in:
lib/meta_inspector/parsers/texts.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from MetaInspector::Parsers::Base

Instance Method Details

#best_titleObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/meta_inspector/parsers/texts.rb', line 12

def best_title
  @best_title ||= begin
    candidates = [
        parsed.css('head title'),
        parsed.css('body title'),
        meta['og:title'],
        parsed.css('h1').first
    ]
    candidates.flatten!
    candidates.map! { |c| (c.respond_to? :inner_text) ? c.inner_text : c }
    candidates.compact!
    return nil if candidates.empty?
    candidates.map! { |c| c.gsub(/\s+/, ' ') }
    candidates.uniq!
    candidates.sort_by! { |t| -t.length }
    candidates.first.strip
  end
end

#descriptionObject

A description getter that first checks for a meta description and if not present will guess by looking at the first paragraph with more than 120 characters



34
35
36
# File 'lib/meta_inspector/parsers/texts.rb', line 34

def description
  meta['description'] || secondary_description
end

#titleObject

Returns the parsed document title, from the content of the <title> tag within the <head> section.



8
9
10
# File 'lib/meta_inspector/parsers/texts.rb', line 8

def title
  @title ||= parsed.css('head title').inner_text rescue nil
end