Class: Ogo::Parsers::Opengraph

Inherits:
Base
  • Object
show all
Defined in:
lib/ogo/parsers/opengraph.rb

Instance Attribute Summary

Attributes inherited from Base

#page, #url

Instance Method Summary collapse

Methods inherited from Base

#all_images

Constructor Details

#initialize(parseable, fallback = false) ⇒ Opengraph

Returns a new instance of Opengraph.



5
6
7
8
# File 'lib/ogo/parsers/opengraph.rb', line 5

def initialize(parseable, fallback=false)
  @global_fallback = fallback
  super parseable
end

Instance Method Details

#description(fallback = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/ogo/parsers/opengraph.rb', line 21

def description(fallback=false)
  if fallback
    super
  else
    _val = find_meta('description')
    (!_val.empty? && _val) ||
      (@global_fallback && super) ||
      ''
  end
end

#image(fallback = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ogo/parsers/opengraph.rb', line 32

def image(fallback=false)
  if fallback
    super
  else
    _val = find_meta('image')
    if _val.empty?
      (@global_fallback && super) || nil
    else
      host_uri = Addressable::URI.parse(url)
      Ogo::ImageInfo.new(url: fix_image_path(_val, host_uri))
    end
  end
end

#metadata(fallback = false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ogo/parsers/opengraph.rb', line 57

def (fallback=false)
  _meta = super
  if fallback
    _meta[:fallback] = {
      title: title(true),
      description: description(true),
      type: type(true),
      image: nil
    }
    img = image(true)
    if img
      _meta[:fallback][:image] = {
        url:    img.url,
        width:  img.width,
        height: img.height,
        type:   img.type
      }
    end
  end
  _meta
end

#title(fallback = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/ogo/parsers/opengraph.rb', line 10

def title(fallback=false)
  if fallback
    super
  else
    _val = find_meta('title')
    (!_val.empty? && _val) ||
      (@global_fallback && super) ||
      ''
  end
end

#type(fallback = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/ogo/parsers/opengraph.rb', line 46

def type(fallback=false)
  if fallback
    super
  else
    _val = find_meta('type')
    (!_val.empty? && _val) ||
      (@global_fallback && super) ||
      ''
  end
end