ogp 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac04b68e554119d27453a1d23849c99eafa72199ecac95e34deded14824e9894
4
- data.tar.gz: 16b167b85c8cffbf3df3292fe7f41a7b00a2f57f731806786a67679585b5ac5b
3
+ metadata.gz: b67e536cdab955dfbff8f93724732c051a19df9337f8560c3e77033728f45ace
4
+ data.tar.gz: e661bc0cbe6c0466db428ad1feb3e33f9e27e28d22e60308aab191fc7cdb78f4
5
5
  SHA512:
6
- metadata.gz: f27db8019f8e34d972c859e9bec5ddc8365e3ab5fe77c0aebeedbfa81c2d243627af0b5b3e85dd9b67de0a2f5d7cd159d95c7ea8bb0a78f7ed0a7e9efdbe9c31
7
- data.tar.gz: d7b980adaa3d90df2409795e6e3e20474fc3f8b19956ebe73c340b11a79bf68813633ae341462c1264489f96bcf0d0a3e34c3aecb4838ccd6fe452cd2dbb55e8
6
+ metadata.gz: eb53f0ea1c971692ae08c716ed8f2d5c4d11a9f1aa6baad942765babde03e53cd11803e0a3f89763c87a36461e8e4eb7894e28a38a918d36b2b299d7e85eeab7
7
+ data.tar.gz: 9d52e2948173d732604afd2ce22cca452ea880ddf030ded4ffdf52d1209c354d8e62547b6cdf508c4ef94542785e63b5a7c6edaea1a6e604085e143aa3d19c3d
data/README.md CHANGED
@@ -41,6 +41,10 @@ open_graph.title # => "Open Graph protocol"
41
41
  open_graph.type # => "website"
42
42
  open_graph.image.url # => "http://ogp.me/logo.png"
43
43
  open_graph.url # => "http://ogp.me/"
44
+
45
+ # All open graph tags are additionally stored in a `data` hash so that custom
46
+ # open graph tags can still be accessed.
47
+ open_graph.data["title"] # => "Open Graph protocol"
44
48
  ```
45
49
 
46
50
  ## Contributing
@@ -5,6 +5,9 @@ REQUIRED_ATTRIBUTES = %w(title type image url).freeze
5
5
 
6
6
  module OGP
7
7
  class OpenGraph
8
+ # Accessor for storing all open graph data
9
+ attr_accessor :data
10
+
8
11
  # Required Accessors
9
12
  attr_accessor :title, :type, :url
10
13
  attr_accessor :images
@@ -24,6 +27,7 @@ module OGP
24
27
 
25
28
  source.force_encoding('UTF-8') if source.encoding != 'UTF-8'
26
29
 
30
+ self.data = {}
27
31
  self.images = []
28
32
  self.audios = []
29
33
  self.locales = []
@@ -51,6 +55,21 @@ module OGP
51
55
  def parse_attributes(document)
52
56
  document.xpath('//head/meta[starts-with(@property, \'og:\')]').each do |attribute|
53
57
  attribute_name = attribute.get('property').downcase.gsub('og:', '')
58
+
59
+ if data.has_key?(attribute_name)
60
+ # There can be multiple entries for the same og tag, see
61
+ # https://open.spotify.com/album/3NkIlQR6wZwPCQiP1vPjF8 for an example
62
+ # where there are multiple `restrictions:country:allowed` og tags.
63
+ #
64
+ # In this case store the content values as an array.
65
+ if !data[attribute_name].kind_of?(Array)
66
+ data[attribute_name] = [ data[attribute_name] ]
67
+ end
68
+ data[attribute_name] << attribute.get('content')
69
+ else
70
+ data[attribute_name] = attribute.get('content')
71
+ end
72
+
54
73
  case attribute_name
55
74
  when /^image$/i
56
75
  images << OpenStruct.new(url: attribute.get('content').to_s)
@@ -70,7 +89,15 @@ module OGP
70
89
  videos << OpenStruct.new unless videos.last
71
90
  videos.last[Regexp.last_match[1].gsub('-', '_')] = attribute.get('content').to_s
72
91
  else
73
- instance_variable_set("@#{attribute_name}", attribute.get('content'))
92
+ begin
93
+ instance_variable_set("@#{attribute_name}", attribute.get('content'))
94
+ rescue NameError
95
+ warn("Some og tag names include colons `:`, such as Spotify song
96
+ pages (`restrictions:country:allowed`), which will result in a
97
+ NameError. Please rely on data[attribute_name] instead. Setting
98
+ top-level instance variables is deprecated and will be removed in
99
+ the next major version.")
100
+ end
74
101
  end
75
102
  end
76
103
  end
@@ -1,3 +1,3 @@
1
1
  module OGP
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_dependency 'oga', '~> 2.15'
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'bundler', '>= 1.13'
26
26
  spec.add_development_dependency 'rake', '~> 10.0'
27
27
  spec.add_development_dependency 'rspec', '~> 3.6'
28
28
  spec.add_development_dependency 'rubocop', '~> 0.49.1'
@@ -0,0 +1,21 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta property="og:title" content="Way Out" />
5
+ <meta property="og:description" content="Way Out, an album by Waterstrider on Spotify" />
6
+ <meta property="og:url" content="https://open.spotify.com/album/3NkIlQR6wZwPCQiP1vPjF8" />
7
+ <meta property="og:image" content="https://i.scdn.co/image/ab67616d0000b273eab5104fe2284f59731dd3f7" />
8
+ <meta property="og:type" content="music.album" />
9
+ <meta property="music:musician" content="https://open.spotify.com/artist/731UNhwHMAcSsurgHJxPHC" />
10
+ <meta property="music:release_date" content="2019-05-10" />
11
+ <meta property="music:song" content="https://open.spotify.com/track/6bewGDfgcmCjbAE2XHbwCh" />
12
+ <meta property="music:song:disc" content="1" />
13
+ <meta property="music:song:track" content="1" />
14
+ <meta property="og:restrictions:country:allowed" content="AR" />
15
+ <meta property="og:restrictions:country:allowed" content="AT" />
16
+ <meta property="og:restrictions:country:allowed" content="BG" />
17
+ <title></title>
18
+ </head>
19
+ <body>
20
+ </body>
21
+ </html>
@@ -141,5 +141,17 @@ describe OGP::OpenGraph do
141
141
  expect(open_graph.image.url).to eql('https://www.example.com/image.jpg')
142
142
  end
143
143
  end
144
+
145
+ context 'with custom attributes' do
146
+ it 'should include them to the data hash' do
147
+ content = File.read("#{File.dirname(__FILE__)}/../fixtures/custom_attributes.html", encoding: 'ASCII-8BIT')
148
+ open_graph = OGP::OpenGraph.new(content)
149
+
150
+ expect(open_graph.title).to eql('Way Out')
151
+ expect(open_graph.data['title']).to eql(open_graph.title)
152
+ expect(open_graph.data['restrictions:country:allowed'].size).to eql(3)
153
+ expect(open_graph.data['restrictions:country:allowed'][0]).to eql('AR')
154
+ end
155
+ end
144
156
  end
145
157
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ogp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Philippe Couture
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-06 00:00:00.000000000 Z
11
+ date: 2019-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga
@@ -28,14 +28,14 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.13'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.13'
41
41
  - !ruby/object:Gem::Dependency
@@ -102,6 +102,7 @@ files:
102
102
  - lib/ogp/version.rb
103
103
  - ogp.gemspec
104
104
  - spec/fixtures/audio_structured_attributes.html
105
+ - spec/fixtures/custom_attributes.html
105
106
  - spec/fixtures/i18n_encoding.html
106
107
  - spec/fixtures/image_structured_attributes.html
107
108
  - spec/fixtures/missing_required_attributes.html
@@ -137,6 +138,7 @@ specification_version: 4
137
138
  summary: Simple Ruby library for parsing Open Graph prototocol information.
138
139
  test_files:
139
140
  - spec/fixtures/audio_structured_attributes.html
141
+ - spec/fixtures/custom_attributes.html
140
142
  - spec/fixtures/i18n_encoding.html
141
143
  - spec/fixtures/image_structured_attributes.html
142
144
  - spec/fixtures/missing_required_attributes.html