Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master.accept multiple og image tags #24

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/link_thumbnailer/opengraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions spec/examples/og_example_multiple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html xmlns:og="http://opengraphprotocol.org/schema/">
<head>
<meta charset="utf-8"/>
<meta property="og:type" content="website"/>
<meta property="og:site_name" content="foo.com">
<meta property="og:title" content="Foo Title">
<meta property="og:description" content="Foo description">
<meta property="og:image" content="http://foo.com/img/front/facebook.png">
<meta property="og:image" content="http://foo.com/img/front/twitter.png">
<meta property="og:image" content="http://foo.com/img/front/pinterest.png">
<title>Foo</title>
</head>

</html>
21 changes: 21 additions & 0 deletions spec/link_thumbnailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down