Class: Onebox::OpenGraph

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

Direct Known Subclasses

Oembed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ OpenGraph

Returns a new instance of OpenGraph.



8
9
10
# File 'lib/onebox/open_graph.rb', line 8

def initialize(doc)
  @data = extract(doc)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/onebox/open_graph.rb', line 26

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)
    ::Onebox::Helpers.normalize_url_for_output(value)
  else
    value
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/onebox/open_graph.rb', line 6

def data
  @data
end

Instance Method Details

#get(attr, length = nil, sanitize = true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/onebox/open_graph.rb', line 41

def get(attr, length = nil, sanitize = true)
  return nil if Onebox::Helpers::blank?(data)

  value = data[attr]

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

  value = html_entities.decode(value)
  value = Sanitize.fragment(value) if sanitize
  value.strip!
  value = Onebox::Helpers.truncate(value, length) unless length.nil?

  value
end

#get_secure_imageObject



20
21
22
23
24
# File 'lib/onebox/open_graph.rb', line 20

def get_secure_image
  secure_link = URI(get(:image))
  secure_link.scheme = 'https'
  secure_link.to_s
end

#titleObject



12
13
14
# File 'lib/onebox/open_graph.rb', line 12

def title
  get(:title, 80)
end

#title_attrObject



16
17
18
# File 'lib/onebox/open_graph.rb', line 16

def title_attr
  !title.nil? ? "title='#{title}'" : ""
end