Class: GetSeo::Seo

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/get_seo/seo.rb', line 3

def description
  @description
end

#heading1Object

Returns the value of attribute heading1.



3
4
5
# File 'lib/get_seo/seo.rb', line 3

def heading1
  @heading1
end

#heading2Object

Returns the value of attribute heading2.



3
4
5
# File 'lib/get_seo/seo.rb', line 3

def heading2
  @heading2
end

#heading3Object

Returns the value of attribute heading3.



3
4
5
# File 'lib/get_seo/seo.rb', line 3

def heading3
  @heading3
end

#img_alt_attributeObject

Returns the value of attribute img_alt_attribute.



3
4
5
# File 'lib/get_seo/seo.rb', line 3

def img_alt_attribute
  @img_alt_attribute
end

#keywordsObject

Returns the value of attribute keywords.



3
4
5
# File 'lib/get_seo/seo.rb', line 3

def keywords
  @keywords
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/get_seo/seo.rb', line 3

def title
  @title
end

Class Method Details

.setup(url) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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