Class: Metanol::Meta::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/metanol/meta/base.rb

Direct Known Subclasses

Main, MicroData, OpenGraph, Webmaster

Constant Summary collapse

ERR_FILTERS_WRONG_TYPE =
'The <filters> parameter must be an Array.'
ERR_FILTERS_WRONG_VALUE_TYPE =
'The <filters> parameter must include only string or symbol values.'
SUPPORTED_FILTERS =
%i[html overspaces whitespaces clean].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, filters = []) ⇒ Base

Returns a new instance of Base.

Raises:

  • (NameError)

14
15
16
17
18
19
20
# File 'lib/metanol/meta/base.rb', line 14

def initialize(name, value, filters = [])
  @name = name.to_sym
  raise(NameError.new("The meta tag '#{@name}' isn't supported.", @name)) unless valid?(@name)

  @value = value
  self.filters = filters
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.


12
13
14
# File 'lib/metanol/meta/base.rb', line 12

def name
  @name
end

#valueObject


33
34
35
36
37
38
# File 'lib/metanol/meta/base.rb', line 33

def value
  result = @value
  return result if @filters.blank?

  filter_overspaces(filter_whitespaces(filter_html_tags(result)))
end

Class Method Details

.filter_html(text) ⇒ Object


40
41
42
43
# File 'lib/metanol/meta/base.rb', line 40

def self.filter_html(text)
  text = text.gsub(%r{<br/?>}, ' ')
  text.gsub(%r{</?\w+/?>}, '')
end

.filter_overspaces(text) ⇒ Object


45
46
47
# File 'lib/metanol/meta/base.rb', line 45

def self.filter_overspaces(text)
  text.gsub(/\ {2,}/, ' ')
end

.filter_whitespaces(text) ⇒ Object


49
50
51
# File 'lib/metanol/meta/base.rb', line 49

def self.filter_whitespaces(text)
  text.gsub(/\s/, ' ')
end

Instance Method Details

#filters=(value) ⇒ Object


22
23
24
# File 'lib/metanol/meta/base.rb', line 22

def filters=(value)
  @filters = validate_filters(value)
end

#renderObject


26
27
28
29
30
31
# File 'lib/metanol/meta/base.rb', line 26

def render
  result = value
  return '' if result.blank?

  "<meta #{attr_name}=\"#{name}\" #{attr_value}=\"#{result}\" />"
end