diff --git a/lib/link_thumbnailer/opengraph.rb b/lib/link_thumbnailer/opengraph.rb index 82b4603..7579731 100644 --- a/lib/link_thumbnailer/opengraph.rb +++ b/lib/link_thumbnailer/opengraph.rb @@ -2,17 +2,15 @@ module LinkThumbnailer class Opengraph def self.parse(object, doc) + object[:images] = [] doc.css('meta').each do |m| if m.attribute('property') && m.attribute('property').to_s.match(/^og:(.+)$/i) - object[$1.gsub('-', '_')] = m.attribute('content').to_s + content = m.attribute('content').to_s + object[:images] << { source_url: content } if $1 == 'image' + object[$1.gsub('-', '_')] = content end end - object[:images] = [] - if object[:image] - object[:images] << { source_url: object[:image] } - end - object end diff --git a/spec/examples/og_example_multiple.html b/spec/examples/og_example_multiple.html new file mode 100644 index 0000000..f2f9b03 --- /dev/null +++ b/spec/examples/og_example_multiple.html @@ -0,0 +1,14 @@ + + + + + + + + + + + Foo + + + diff --git a/spec/link_thumbnailer_spec.rb b/spec/link_thumbnailer_spec.rb index f5a05e4..adefe7e 100644 --- a/spec/link_thumbnailer_spec.rb +++ b/spec/link_thumbnailer_spec.rb @@ -5,6 +5,7 @@ let(:og_example) { File.open(File.dirname(__FILE__) + '/examples/og_example.html').read() } let(:example) { File.open(File.dirname(__FILE__) + '/examples/example.html').read() } let(:empty_example) { File.open(File.dirname(__FILE__) + '/examples/empty_example.html').read() } + let(:og_example_multiple) { File.open(File.dirname(__FILE__) + '/examples/og_example_multiple.html').read() } it { should respond_to :configuration } it { should respond_to :configure } @@ -159,6 +160,16 @@ end + context "and multiple" do + + before do + stub_request(:get, 'http://foo.com/').to_return(status: 200, body: og_example_multiple, headers: {}) + end + + it { expect(LinkThumbnailer.generate('http://foo.com')).to be_valid } + it { expect(LinkThumbnailer.generate('http://foo.com/').images.count).to eq(3) } + end + end context "when not strict" do @@ -196,6 +207,16 @@ end + context "and multiple" do + + before do + stub_request(:get, 'http://foo.com/').to_return(status: 200, body: og_example_multiple, headers: {}) + end + + it { expect(LinkThumbnailer.generate('http://foo.com')).to be_valid } + it { expect(LinkThumbnailer.generate('http://foo.com/').images.count).to eq(3) } + end + end end