Class: OGP::OpenGraph

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

Constant Summary collapse

REQUIRED_ATTRIBUTES =
%w(title type image url).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, opts = {}) ⇒ OpenGraph

Returns a new instance of OpenGraph.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ogp/open_graph.rb', line 20

def initialize(source, opts = {})
  if source.nil? || source.empty?
    raise ArgumentError, '`source` cannot be nil or empty.'
  end

  opts[:required_attributes] ||= DEFAULT_REQUIRED_ATTRIBUTES

  raise MalformedSourceError unless source.include?('</html>')

  source.force_encoding('UTF-8') if source.encoding != 'UTF-8'

  self.data = {}
  self.images = []
  self.audios = []
  self.locales = []
  self.videos = []

  document = Oga.parse_html(source)
  check_required_attributes(document, opts[:required_attributes])
  parse_attributes(document)
end

Instance Attribute Details

#audiosObject

Returns the value of attribute audios.



15
16
17
# File 'lib/ogp/open_graph.rb', line 15

def audios
  @audios
end

#dataObject

Accessor for storing all open graph data



11
12
13
# File 'lib/ogp/open_graph.rb', line 11

def data
  @data
end

#descriptionObject

Optional Accessors



18
19
20
# File 'lib/ogp/open_graph.rb', line 18

def description
  @description
end

#determinerObject

Optional Accessors



18
19
20
# File 'lib/ogp/open_graph.rb', line 18

def determiner
  @determiner
end

#imagesObject

Returns the value of attribute images.



15
16
17
# File 'lib/ogp/open_graph.rb', line 15

def images
  @images
end

#localesObject

Returns the value of attribute locales.



15
16
17
# File 'lib/ogp/open_graph.rb', line 15

def locales
  @locales
end

#site_nameObject

Optional Accessors



18
19
20
# File 'lib/ogp/open_graph.rb', line 18

def site_name
  @site_name
end

#titleObject

Required Accessors



14
15
16
# File 'lib/ogp/open_graph.rb', line 14

def title
  @title
end

#typeObject

Required Accessors



14
15
16
# File 'lib/ogp/open_graph.rb', line 14

def type
  @type
end

#urlObject

Required Accessors



14
15
16
# File 'lib/ogp/open_graph.rb', line 14

def url
  @url
end

#videosObject

Returns the value of attribute videos.



15
16
17
# File 'lib/ogp/open_graph.rb', line 15

def videos
  @videos
end

Instance Method Details

#imageObject



42
43
44
45
46
# File 'lib/ogp/open_graph.rb', line 42

def image
  return if images.nil?

  images.first
end