Class: OpenGraphPlus::Parser

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

Constant Summary collapse

REQUIRED_TAGS =
%w[og:title og:type og:image og:url].freeze
%w[og:description og:site_name twitter:card twitter:title twitter:image].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(html)
  @html = html.to_s
  @tags = parse_meta_tags
end

Instance Attribute Details

#htmlObject (readonly)

Returns the value of attribute html.



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

def html
  @html
end

Instance Method Details

#[](property) ⇒ Object



29
30
31
# File 'lib/opengraphplus/parser.rb', line 29

def [](property)
  @tags[property]
end

#all_tagsObject



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

def all_tags
  @tags.dup
end

#errorsObject



37
38
39
# File 'lib/opengraphplus/parser.rb', line 37

def errors
  REQUIRED_TAGS.reject { |tag| @tags.key?(tag) }
end

#og_tagsObject



17
18
19
# File 'lib/opengraphplus/parser.rb', line 17

def og_tags
  @tags.select { |key, _| key.start_with?("og:") }
end

#twitter_tagsObject



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

def twitter_tags
  @tags.select { |key, _| key.start_with?("twitter:") }
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/opengraphplus/parser.rb', line 33

def valid?
  errors.empty?
end

#warningsObject



41
42
43
# File 'lib/opengraphplus/parser.rb', line 41

def warnings
  RECOMMENDED_TAGS.reject { |tag| @tags.key?(tag) }
end