Class: MetatagCop::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Parser

Returns a new instance of Parser.



12
13
14
# File 'lib/metatag_cop/parser.rb', line 12

def initialize(url)
  @document = parse(url)
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



10
11
12
# File 'lib/metatag_cop/parser.rb', line 10

def document
  @document
end

Instance Method Details

#descriptionObject



20
21
22
23
# File 'lib/metatag_cop/parser.rb', line 20

def description
  description = document.at('meta[name=description]')
  description['content'] unless description.nil?
end

#h1Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/metatag_cop/parser.rb', line 30

def h1
  h1 = document.at('h1')

  return if h1.nil?
  return h1['content'] unless h1['content'].nil?

  # it regards alt as h1 content if h1 content is blank.
  a = find_tag_by(h1, 'a')
  img = find_tag_by(a, 'img')
  img.attributes['alt'].value
end

#keywordsObject



25
26
27
28
# File 'lib/metatag_cop/parser.rb', line 25

def keywords
  keywords = document.at('meta[name=keywords]')
  keywords['content'] unless keywords.nil?
end

#titleObject



16
17
18
# File 'lib/metatag_cop/parser.rb', line 16

def title
  document.css('head title').inner_text
end