Skip to content

Commit

Permalink
fix: og multi image support. Fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
gottfrois committed Apr 30, 2014
1 parent b6236c6 commit 05e6326
Show file tree
Hide file tree
Showing 5 changed files with 2,953 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/link_thumbnailer/scrapers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ def value
end

def meta_xpath(options = {})
meta_xpaths(options).first
end

def meta_xpaths(options = {})
key = options.fetch(:key, :property)
value = options.fetch(:value, :content)
attribute = options.fetch(:attribute, attribute_name)

document.xpath("//meta[translate(@#{key},'#{abc.upcase}','#{abc}') = '#{attribute}' and @#{value}]").first
document.xpath("//meta[translate(@#{key},'#{abc.upcase}','#{abc}') = '#{attribute}' and @#{value}]")
end

def abc
Expand Down
10 changes: 10 additions & 0 deletions lib/link_thumbnailer/scrapers/opengraph/image.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pry'
require 'link_thumbnailer/scrapers/opengraph/base'

module LinkThumbnailer
Expand All @@ -11,10 +12,19 @@ def value
model
end

def model
nodes.map { |n| modelize(n, n.attributes['content'].to_s) }
end

def modelize(node, text = nil)
model_class.new(text)
end

def nodes
nodes = meta_xpaths(attribute: attribute)
nodes.empty? ? meta_xpaths(attribute: attribute, key: :name) : nodes
end

end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/fixture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@

end

context 'with multi image' do

let(:png_url_2) { 'http://foo.com/bar.png' }
let(:png_2) { File.open(File.dirname(__FILE__) + '/fixtures/bar.png') }
let(:html) { File.open(File.dirname(__FILE__) + '/fixtures/og_valid_multi_image_example.html').read() }

before do
stub_request(:get, png_url_2).to_return(status: 200, body: png_2, headers: {})
end

it { expect(action.title).to eq(title) }
it { expect(action.description).to eq(description) }
it { expect(action.images.count).to eq(2) }
it { expect(action.images.first.src.to_s).to eq(png_url) }
it { expect(action.images.last.src.to_s).to eq(png_url_2) }

end

context 'when not valid' do

let(:html) { File.open(File.dirname(__FILE__) + '/fixtures/og_not_valid_example.html').read() }
Expand Down

0 comments on commit 05e6326

Please sign in to comment.