Skip to content

Commit

Permalink
Merge pull request #130 from mmmmmrob/youtube_title
Browse files Browse the repository at this point in the history
youtube specific best_title
  • Loading branch information
jaimeiniesta committed Apr 30, 2015
2 parents e70c0a4 + e41201c commit edc033e
Show file tree
Hide file tree
Showing 5 changed files with 1,820 additions and 17 deletions.
37 changes: 21 additions & 16 deletions lib/meta_inspector/parsers/texts.rb
Expand Up @@ -10,22 +10,8 @@ def title
end

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
@best_title = meta['og:title'] if @main_parser.host =~ /\.youtube\.com$/
@best_title ||= find_best_title
end

# A description getter that first checks for a meta description
Expand All @@ -37,6 +23,25 @@ def description

private

# Look for candidates and pick the longest one
def find_best_title
candidates = [
parsed.css('head title'),
parsed.css('body title'),
meta['og:title'],
parsed.css('h1').first
]
candidates.flatten!
candidates.compact!
candidates.map! { |c| (c.respond_to? :inner_text) ? c.inner_text : c }
candidates.map! { |c| c.strip }
return nil if candidates.empty?
candidates.map! { |c| c.gsub(/\s+/, ' ') }
candidates.uniq!
candidates.sort_by! { |t| -t.length }
candidates.first
end

# Look for the first <p> block with 120 characters or more
def secondary_description
first_long_paragraph = parsed.search('//p[string-length() >= 120]').first
Expand Down
2 changes: 1 addition & 1 deletion lib/meta_inspector/version.rb
@@ -1,3 +1,3 @@
module MetaInspector
VERSION = "4.4.1"
VERSION = "4.4.2"
end

0 comments on commit edc033e

Please sign in to comment.