Class: Onebox::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/onebox/normalizer.rb

Direct Known Subclasses

JsonLd, OpenGraph

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(attr, *args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/onebox/normalizer.rb', line 14

def method_missing(attr, *args, &block)
  value = get(attr, *args)

  return nil if Onebox::Helpers.blank?(value)

  method_name = attr.to_s
  if method_name.end_with?(*integer_suffixes)
    value.to_i
  elsif method_name.end_with?(*url_suffixes)
    result = Onebox::Helpers.normalize_url_for_output(value)
    result unless Onebox::Helpers.blank?(result)
  else
    value
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/onebox/normalizer.rb', line 5

def data
  @data
end

Instance Method Details

#get(attr, *args) ⇒ Object



7
8
9
10
11
12
# File 'lib/onebox/normalizer.rb', line 7

def get(attr, *args)
  value = data[attr]
  return if value.blank?
  return value.map { |v| sanitize_value(v, *args) } if value.is_a?(Array)
  sanitize_value(value, *args)
end