class GetSeo::Seo

Attributes

description[RW]
heading1[RW]
heading2[RW]
heading3[RW]
img_alt_attribute[RW]
keywords[RW]
title[RW]

Public Class Methods

setup(url) click to toggle source
# File lib/get_seo/seo.rb, line 5
def self.setup(url)
  html = Nokogiri::HTML(open(url))
  seo = self.new

  seo.heading1 = html.search("h1").map do |h1|
    h1.text.strip.gsub(/\s+/, " ")
  end.reject(&:empty?)

  seo.heading2 = html.search("h2").map do |h2|
    h2.text.strip.gsub(/\s+/, " ")
  end.reject(&:empty?)

  seo.heading3 = html.search("h3").map do |h3|
    h3.text.strip.gsub(/\s+/, " ")
  end.reject(&:empty?)

  seo.title = []
  if title = html.at("title")&.text
    seo.title << title.strip unless title.empty?
  end

  seo.keywords = []
  if keywords = (html.at("meta[name='keywords']") &&
                 html.at("meta[name='keywords']")['content'])

    keywords.split(',').each do |keyword|
       stripped_word = keyword.strip
       seo.keywords << stripped_word unless stripped_word.empty?
    end
  end

  seo.description = []
  if description = (html.at("meta[name='description']") &&
                    html.at("meta[name='description']")['content'])

    seo.description << description unless description.empty?
  end

  seo.img_alt_attribute = html.search('img').map do |img|
      img['alt']&.strip
  end.compact.reject!(&:empty?)

  seo
end