diff --git a/lib/ogp/open_graph.rb b/lib/ogp/open_graph.rb index 7de41b3..3ffc7d9 100644 --- a/lib/ogp/open_graph.rb +++ b/lib/ogp/open_graph.rb @@ -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? @@ -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) @@ -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 diff --git a/spec/lib/open_graph_spec.rb b/spec/lib/open_graph_spec.rb index dca8d38..c34713c 100644 --- a/spec/lib/open_graph_spec.rb +++ b/spec/lib/open_graph_spec.rb @@ -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 @@ -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 diff --git a/spec/view/audio_structured_attributes.html b/spec/view/audio_structured_attributes.html new file mode 100644 index 0000000..6e1713f --- /dev/null +++ b/spec/view/audio_structured_attributes.html @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/spec/view/video_structured_attributes.html b/spec/view/video_structured_attributes.html new file mode 100644 index 0000000..c72448b --- /dev/null +++ b/spec/view/video_structured_attributes.html @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + +