Skip to content

Commit

Permalink
Merge pull request jcouture#4 from seanabrahams/fix-name-error
Browse files Browse the repository at this point in the history
Don't error out when encountering unexpected/custom open graph tags.
  • Loading branch information
jcouture authored Dec 22, 2019
2 parents 5c5460a + 1e74478 commit 9108d8f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ open_graph.title # => "Open Graph protocol"
open_graph.type # => "website"
open_graph.image.url # => "http://ogp.me/logo.png"
open_graph.url # => "http://ogp.me/"

# All open graph tags are additionally stored in a `data` hash so that custom
# open graph tags can still be accessed.
open_graph.data["title"] # => "Open Graph protocol"
```

## Contributing
Expand Down
29 changes: 28 additions & 1 deletion lib/ogp/open_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

module OGP
class OpenGraph
# Accessor for storing all open graph data
attr_accessor :data

# Required Accessors
attr_accessor :title, :type, :url
attr_accessor :images
Expand All @@ -24,6 +27,7 @@ def initialize(source)

source.force_encoding('UTF-8') if source.encoding != 'UTF-8'

self.data = {}
self.images = []
self.audios = []
self.locales = []
Expand Down Expand Up @@ -51,6 +55,21 @@ def check_required_attributes(document)
def parse_attributes(document)
document.xpath('//head/meta[starts-with(@property, \'og:\')]').each do |attribute|
attribute_name = attribute.get('property').downcase.gsub('og:', '')

if data.has_key?(attribute_name)
# There can be multiple entries for the same og tag, see
# https://open.spotify.com/album/3NkIlQR6wZwPCQiP1vPjF8 for an example
# where there are multiple `restrictions:country:allowed` og tags.
#
# In this case store the content values as an array.
if !data[attribute_name].kind_of?(Array)
data[attribute_name] = [ data[attribute_name] ]
end
data[attribute_name] << attribute.get('content')
else
data[attribute_name] = attribute.get('content')
end

case attribute_name
when /^image$/i
images << OpenStruct.new(url: attribute.get('content').to_s)
Expand All @@ -70,7 +89,15 @@ def parse_attributes(document)
videos << OpenStruct.new unless videos.last
videos.last[Regexp.last_match[1].gsub('-', '_')] = attribute.get('content').to_s
else
instance_variable_set("@#{attribute_name}", attribute.get('content'))
begin
instance_variable_set("@#{attribute_name}", attribute.get('content'))
rescue NameError
warn("Some og tag names include colons `:`, such as Spotify song
pages (`restrictions:country:allowed`), which will result in a
NameError. Please rely on data[attribute_name] instead. Setting
top-level instance variables is deprecated and will be removed in
the next major version.")
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ogp/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OGP
VERSION = '0.3.0'.freeze
VERSION = '0.4.0'.freeze
end
2 changes: 1 addition & 1 deletion ogp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'oga', '~> 2.15'

spec.add_development_dependency 'bundler', '~> 1.13'
spec.add_development_dependency 'bundler', '>= 1.13'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.6'
spec.add_development_dependency 'rubocop', '~> 0.49.1'
Expand Down
21 changes: 21 additions & 0 deletions spec/fixtures/custom_attributes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta property="og:title" content="Way Out" />
<meta property="og:description" content="Way Out, an album by Waterstrider on Spotify" />
<meta property="og:url" content="https://open.spotify.com/album/3NkIlQR6wZwPCQiP1vPjF8" />
<meta property="og:image" content="https://i.scdn.co/image/ab67616d0000b273eab5104fe2284f59731dd3f7" />
<meta property="og:type" content="music.album" />
<meta property="music:musician" content="https://open.spotify.com/artist/731UNhwHMAcSsurgHJxPHC" />
<meta property="music:release_date" content="2019-05-10" />
<meta property="music:song" content="https://open.spotify.com/track/6bewGDfgcmCjbAE2XHbwCh" />
<meta property="music:song:disc" content="1" />
<meta property="music:song:track" content="1" />
<meta property="og:restrictions:country:allowed" content="AR" />
<meta property="og:restrictions:country:allowed" content="AT" />
<meta property="og:restrictions:country:allowed" content="BG" />
<title></title>
</head>
<body>
</body>
</html>
12 changes: 12 additions & 0 deletions spec/ogp/open_graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,17 @@
expect(open_graph.image.url).to eql('https://www.example.com/image.jpg')
end
end

context 'with custom attributes' do
it 'should include them to the data hash' do
content = File.read("#{File.dirname(__FILE__)}/../fixtures/custom_attributes.html", encoding: 'ASCII-8BIT')
open_graph = OGP::OpenGraph.new(content)

expect(open_graph.title).to eql('Way Out')
expect(open_graph.data['title']).to eql(open_graph.title)
expect(open_graph.data['restrictions:country:allowed'].size).to eql(3)
expect(open_graph.data['restrictions:country:allowed'][0]).to eql('AR')
end
end
end
end

0 comments on commit 9108d8f

Please sign in to comment.