Skip to content

Commit

Permalink
Add support for audio and video attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouture committed Sep 9, 2017
1 parent 7be57bf commit d4572d0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
14 changes: 12 additions & 2 deletions lib/ogp/open_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class OpenGraph

# Optional Accessors
attr_accessor :description, :determiner, :site_name
attr_accessor :audio
attr_accessor :audios
attr_accessor :locales
attr_accessor :video
attr_accessor :videos

def initialize(source)
if source.nil? || source.empty?
Expand All @@ -25,7 +25,9 @@ def initialize(source)
end

self.images = []
self.audios = []
self.locales = []
self.videos = []

document = Oga.parse_html(source)
check_required_attributes(document)
Expand All @@ -50,8 +52,16 @@ def parse_attributes(document)
self.images << OpenStruct.new(url: attribute.get('content').to_s)
when /^image:(.+)/i
self.images.last[$1.gsub('-','_')] = attribute.get('content').to_s
when /^audio$/i
self.audios << OpenStruct.new(url: attribute.get('content').to_s)
when /^audio:(.+)/i
self.audios.last[$1.gsub('-','_')] = attribute.get('content').to_s
when /^locale/i
self.locales << attribute.get('content').to_s
when /^video$/i
self.videos << OpenStruct.new(url: attribute.get('content').to_s)
when /^video:(.+)/i
self.videos.last[$1.gsub('-','_')] = attribute.get('content').to_s
else
instance_variable_set("@#{attribute_name}", attribute.get('content'))
end
Expand Down
28 changes: 26 additions & 2 deletions spec/lib/open_graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
content = File.read("#{File.dirname(__FILE__)}/../view/optional_attributes.html")
open_graph = OGP::OpenGraph.new(content)

expect(open_graph.audio).to eql('http://example.com/bond/theme.mp3')
expect(open_graph.audios[0].url).to eql('http://example.com/bond/theme.mp3')
expect(open_graph.description).to eql('Sean Connery found fame and fortune as the suave, sophisticated British agent, James Bond.')
expect(open_graph.determiner).to eql('the')
expect(open_graph.locales).to match_array(%w(en_GB fr_FR es_ES))
expect(open_graph.site_name).to eql('IMDb')
expect(open_graph.video).to eql('http://example.com/bond/trailer.swf')
expect(open_graph.videos[0].url).to eql('http://example.com/bond/trailer.swf')
end
end

Expand All @@ -67,5 +67,29 @@
expect(open_graph.images[0].alt).to eql('A shiny red apple with a bite taken out')
end
end

context 'with audio structured attributes' do
it 'should create a proper OpenGraph object' do
content = File.read("#{File.dirname(__FILE__)}/../view/audio_structured_attributes.html")
open_graph = OGP::OpenGraph.new(content)

expect(open_graph.audios[0].url).to eql('http://example.com/sound.ogg')
expect(open_graph.audios[0].secure_url).to eql('https://secure.example.com/sound.ogg')
expect(open_graph.audios[0].type).to eql('audio/ogg')
end
end

context 'with video structured attributes' do
it 'should create a proper OpenGraph object' do
content = File.read("#{File.dirname(__FILE__)}/../view/video_structured_attributes.html")
open_graph = OGP::OpenGraph.new(content)

expect(open_graph.videos[0].url).to eql('http://example.com/movie.swf')
expect(open_graph.videos[0].secure_url).to eql('https://secure.example.com/movie.swf')
expect(open_graph.videos[0].type).to eql('application/x-shockwave-flash')
expect(open_graph.videos[0].width).to eql('400')
expect(open_graph.videos[0].height).to eql('300')
end
end
end
end
15 changes: 15 additions & 0 deletions spec/view/audio_structured_attributes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta content="The Rock" property="og:title">
<meta content="video.movie" property="og:type">
<meta content="http://www.imdb.com/title/tt0117500/" property="og:url">
<meta content="http://example.com/ogp.jpg" property="og:image">
<meta content="http://example.com/sound.ogg" property="og:audio">
<meta content="https://secure.example.com/sound.ogg" property="og:audio:secure_url">
<meta content="audio/ogg" property="og:audio:type">
<title></title>
</head>
<body>
</body>
</html>
17 changes: 17 additions & 0 deletions spec/view/video_structured_attributes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta content="The Rock" property="og:title">
<meta content="video.movie" property="og:type">
<meta content="http://www.imdb.com/title/tt0117500/" property="og:url">
<meta content="http://example.com/ogp.jpg" property="og:image">
<meta content="http://example.com/movie.swf" property="og:video">
<meta content="https://secure.example.com/movie.swf" property="og:video:secure_url">
<meta content="application/x-shockwave-flash" property="og:video:type">
<meta content="400" property="og:video:width">
<meta content="300" property="og:video:height">
<title></title>
</head>
<body>
</body>
</html>

0 comments on commit d4572d0

Please sign in to comment.