ogp 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b67e536cdab955dfbff8f93724732c051a19df9337f8560c3e77033728f45ace
4
- data.tar.gz: e661bc0cbe6c0466db428ad1feb3e33f9e27e28d22e60308aab191fc7cdb78f4
3
+ metadata.gz: ed92054f85b86a12e2ef262857950f7033c8606b1571064ebc73820130f74a8a
4
+ data.tar.gz: b619ff744508bc9beb66546558e9eee3fdb5a0cf5734d6e7ce5acf1685b49bd6
5
5
  SHA512:
6
- metadata.gz: eb53f0ea1c971692ae08c716ed8f2d5c4d11a9f1aa6baad942765babde03e53cd11803e0a3f89763c87a36461e8e4eb7894e28a38a918d36b2b299d7e85eeab7
7
- data.tar.gz: 9d52e2948173d732604afd2ce22cca452ea880ddf030ded4ffdf52d1209c354d8e62547b6cdf508c4ef94542785e63b5a7c6edaea1a6e604085e143aa3d19c3d
6
+ metadata.gz: e00d1b10d5a4c9abe289eb898d7b462fa0a67661a4bd0b856ec877590b4b254fae36eeddb54031c7a0c75f642506ba26f0ef911e7be4ccf9810339a95c334716
7
+ data.tar.gz: f31fe03b72b7056c92edeb5c0ccc0def400bc95fec134b78f918bf970e4419cad16aa9f3f7fdd2bc8f016aa184235808230874361b15a6ba8ce1ab55b1da6bf8
data/.rubocop.yml CHANGED
@@ -1,120 +1,140 @@
1
1
  AllCops:
2
2
  DisplayCopNames: true
3
- Include:
4
- - Rakefile
5
- - config.ru
6
- - Gemfile
7
- - app/tasks/**/*.rake
8
- Exclude:
9
- - bin/**
10
- - db/schema.rb
11
- - db/migrate/**
12
- - script/**
13
- - vendor/**/*
14
- - node_modules/**/*
15
- TargetRubyVersion: 2.3
3
+ SuggestExtensions: false
4
+ TargetRubyVersion: 2.7
16
5
 
17
- Rails:
6
+ Bundler/OrderedGems:
18
7
  Enabled: false
19
8
 
20
- Documentation:
21
- Enabled: false
22
-
23
- Encoding:
24
- Enabled: false
25
-
26
- LineLength:
27
- Max: 200
9
+ Gemspec/DateAssignment:
10
+ Enabled: true
28
11
 
29
- AccessModifierIndentation:
12
+ Layout/AccessModifierIndentation:
30
13
  EnforcedStyle: outdent
31
-
32
- IfUnlessModifier:
33
- Enabled: false
34
-
35
- CaseIndentation:
14
+ Layout/CaseIndentation:
36
15
  EnforcedStyle: end
37
16
  IndentOneStep: true
38
-
39
- MethodLength:
40
- CountComments: false
41
- Max: 25
42
-
43
- SignalException:
44
- Enabled: false
45
-
46
- ColonMethodCall:
17
+ Layout/ExtraSpacing:
18
+ Enabled: true
19
+ Layout/FirstArrayElementIndentation:
47
20
  Enabled: false
48
-
49
- AsciiComments:
21
+ Layout/HashAlignment:
50
22
  Enabled: false
51
-
52
- RegexpLiteral:
23
+ Layout/LineLength:
24
+ Max: 200
25
+ Layout/MultilineMethodCallIndentation:
53
26
  Enabled: false
54
-
55
- AssignmentInCondition:
27
+ Layout/ParameterAlignment:
28
+ EnforcedStyle: with_fixed_indentation
29
+ Layout/SpaceBeforeBrackets:
30
+ Enabled: true
31
+ Layout/SpaceInLambdaLiteral:
56
32
  Enabled: false
57
33
 
58
- ParameterLists:
59
- CountKeywordArgs: false
60
-
61
- SingleLineBlockParams:
62
- Methods:
63
- - reduce:
64
- - memo
65
- - item
66
-
67
- MethodCalledOnDoEndBlock:
34
+ Lint/AmbiguousAssignment:
68
35
  Enabled: true
69
-
70
- Metrics/AbcSize:
36
+ Lint/AssignmentInCondition:
71
37
  Enabled: false
72
-
73
- Style/CollectionMethods:
38
+ Lint/DeprecatedConstants:
74
39
  Enabled: true
75
-
76
- Style/SymbolArray:
40
+ Lint/DuplicateBranch:
77
41
  Enabled: true
78
-
79
- Layout/ExtraSpacing:
42
+ Lint/DuplicateRegexpCharacterClassElement:
43
+ Enabled: true
44
+ Lint/EmptyBlock:
45
+ Enabled: true
46
+ Lint/EmptyClass:
47
+ Enabled: true
48
+ Lint/LambdaWithoutLiteralBlock:
49
+ Enabled: true
50
+ Lint/NoReturnInBeginEndBlocks:
51
+ Enabled: true
52
+ Lint/NumberedParameterAssignment:
53
+ Enabled: true
54
+ Lint/OrAssignmentToConstant:
55
+ Enabled: true
56
+ Lint/RedundantDirGlobSort:
57
+ Enabled: true
58
+ Lint/SymbolConversion:
59
+ Enabled: true
60
+ Lint/ToEnumArguments:
61
+ Enabled: true
62
+ Lint/TripleQuotes:
63
+ Enabled: true
64
+ Lint/UnexpectedBlockArity:
65
+ Enabled: true
66
+ Lint/UnmodifiedReduceAccumulator:
80
67
  Enabled: true
81
68
 
82
- Layout/MultilineMethodCallIndentation:
69
+ Metrics/AbcSize:
83
70
  Enabled: false
84
-
85
- Layout/IndentArray:
71
+ Metrics/BlockLength:
72
+ Max: 200
73
+ Metrics/MethodLength:
74
+ CountComments: false
75
+ Max: 40
76
+ Metrics/PerceivedComplexity:
86
77
  Enabled: false
78
+ Metrics/ParameterLists:
79
+ CountKeywordArgs: false
87
80
 
88
- Layout/AlignHash:
81
+ Style/ArgumentsForwarding:
82
+ Enabled: true
83
+ Style/CollectionCompact:
84
+ Enabled: true
85
+ Style/CollectionMethods:
86
+ Enabled: true
87
+ Style/ColonMethodCall:
89
88
  Enabled: false
90
-
91
- Layout/AlignParameters:
92
- EnforcedStyle: with_fixed_indentation
93
-
94
- Style/FrozenStringLiteralComment:
89
+ Style/DocumentDynamicEvalDefinition:
90
+ Enabled: true
91
+ Style/Documentation:
95
92
  Enabled: false
96
-
97
- Rails/SkipsModelValidations:
93
+ Style/Encoding:
98
94
  Enabled: false
99
-
100
- Metrics/BlockLength:
101
- Max: 200
102
-
103
- Style/PercentLiteralDelimiters:
104
- PreferredDelimiters:
105
- '%i': '()'
106
- '%I': '()'
107
- '%w': '()'
108
- '%W': '()'
109
-
110
- Layout/SpaceInLambdaLiteral:
95
+ Style/EndlessMethod:
96
+ Enabled: true
97
+ Style/FrozenStringLiteralComment:
111
98
  Enabled: false
112
-
113
- Bundler/OrderedGems:
99
+ Style/HashConversion:
100
+ Enabled: true
101
+ Style/HashExcept:
102
+ Enabled: true
103
+ Style/IfUnlessModifier:
114
104
  Enabled: false
115
-
116
- Rails/OutputSafety:
105
+ Style/IfWithBooleanLiteralBranches:
106
+ Enabled: true
107
+ Style/MethodCalledOnDoEndBlock:
108
+ Enabled: true
109
+ Style/NegatedIfElseCondition:
110
+ Enabled: true
111
+ Style/NilLambda:
112
+ Enabled: true
113
+ Style/PercentLiteralDelimiters:
114
+ PreferredDelimiters:
115
+ ? "%I"
116
+ : ()
117
+ ? "%W"
118
+ : ()
119
+ ? "%i"
120
+ : ()
121
+ ? "%w"
122
+ : ()
123
+ Style/RedundantArgument:
124
+ Enabled: true
125
+ Style/RegexpLiteral:
117
126
  Enabled: false
118
-
119
- Performance/StringReplacement:
127
+ Style/SignalException:
120
128
  Enabled: false
129
+ Style/SingleLineBlockParams:
130
+ Methods:
131
+ -
132
+ reduce:
133
+ - memo
134
+ - item
135
+ Style/StringChars:
136
+ Enabled: true
137
+ Style/SwapValues:
138
+ Enabled: true
139
+ Style/SymbolArray:
140
+ Enabled: true
data/.travis.yml CHANGED
@@ -3,6 +3,8 @@ sudo: false
3
3
  language: ruby
4
4
 
5
5
  rvm:
6
- - 2.3
6
+ - 2.7.3
7
7
 
8
- script: "bundle exec rake spec"
8
+ script:
9
+ - bundle exec rake spec
10
+ - bundle exec rubocop
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <p align="center">
2
2
  <a href="https://github.com/jcouture/ogp">
3
- <img src="https://i.imgur.com/qZrMsLq.png" alt="ogp" />
3
+ <img src="https://i.imgur.com/8ehwe6E.png" alt="ogp" />
4
4
  </a>
5
5
  <br />
6
6
  OGP is a minimalist Ruby library that does only one thing: parse Open Graph protocol information. For more information: <a href="http://ogp.me">http://ogp.me</a>.
data/Rakefile CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
2
3
  require 'rspec/core/rake_task'
3
4
 
4
- RSpec::Core::RakeTask.new('spec')
5
+ RuboCop::RakeTask.new(:rubocop)
5
6
 
6
- task default: :spec
7
+ RSpec::Core::RakeTask.new(:test)
8
+
9
+ task default: %i(rubocop test)
@@ -1,28 +1,29 @@
1
1
  require 'oga'
2
2
  require 'ostruct'
3
3
 
4
- REQUIRED_ATTRIBUTES = %w(title type image url).freeze
4
+ DEFAULT_REQUIRED_ATTRIBUTES = %w(title type image url).freeze
5
5
 
6
6
  module OGP
7
7
  class OpenGraph
8
+ REQUIRED_ATTRIBUTES = %w(title type image url).freeze
9
+
8
10
  # Accessor for storing all open graph data
9
11
  attr_accessor :data
10
12
 
11
13
  # Required Accessors
12
14
  attr_accessor :title, :type, :url
13
- attr_accessor :images
15
+ attr_accessor :images, :audios, :locales, :videos
14
16
 
15
17
  # Optional Accessors
16
18
  attr_accessor :description, :determiner, :site_name
17
- attr_accessor :audios
18
- attr_accessor :locales
19
- attr_accessor :videos
20
19
 
21
- def initialize(source)
20
+ def initialize(source, opts = {})
22
21
  if source.nil? || source.empty?
23
22
  raise ArgumentError, '`source` cannot be nil or empty.'
24
23
  end
25
24
 
25
+ opts[:required_attributes] ||= DEFAULT_REQUIRED_ATTRIBUTES
26
+
26
27
  raise MalformedSourceError unless source.include?('</html>')
27
28
 
28
29
  source.force_encoding('UTF-8') if source.encoding != 'UTF-8'
@@ -34,19 +35,20 @@ module OGP
34
35
  self.videos = []
35
36
 
36
37
  document = Oga.parse_html(source)
37
- check_required_attributes(document)
38
+ check_required_attributes(document, opts[:required_attributes])
38
39
  parse_attributes(document)
39
40
  end
40
41
 
41
42
  def image
42
43
  return if images.nil?
44
+
43
45
  images.first
44
46
  end
45
47
 
46
48
  private
47
49
 
48
- def check_required_attributes(document)
49
- REQUIRED_ATTRIBUTES.each do |attribute_name|
50
+ def check_required_attributes(document, required_attributes)
51
+ required_attributes.each do |attribute_name|
50
52
  raise MissingAttributeError, "Missing required attribute: #{attribute_name}" unless attribute_exists(document, attribute_name)
51
53
  end
52
54
  end
@@ -56,14 +58,14 @@ module OGP
56
58
  document.xpath('//head/meta[starts-with(@property, \'og:\')]').each do |attribute|
57
59
  attribute_name = attribute.get('property').downcase.gsub('og:', '')
58
60
 
59
- if data.has_key?(attribute_name)
61
+ if data.key?(attribute_name)
60
62
  # There can be multiple entries for the same og tag, see
61
63
  # https://open.spotify.com/album/3NkIlQR6wZwPCQiP1vPjF8 for an example
62
64
  # where there are multiple `restrictions:country:allowed` og tags.
63
65
  #
64
66
  # 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
+ unless data[attribute_name].is_a?(Array)
68
+ data[attribute_name] = [data[attribute_name]]
67
69
  end
68
70
  data[attribute_name] << attribute.get('content')
69
71
  else
@@ -101,6 +103,7 @@ module OGP
101
103
  end
102
104
  end
103
105
  end
106
+ # rubocop:enable Metrics/CyclomaticComplexity
104
107
 
105
108
  def attribute_exists(document, name)
106
109
  document.at_xpath("boolean(//head/meta[@property='og:#{name}'])")
data/lib/ogp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OGP
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
data/ogp.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- lib = File.expand_path('../lib', __FILE__)
3
+ lib = File.expand_path('lib', __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'ogp/version'
6
6
 
@@ -20,10 +20,12 @@ Gem::Specification.new do |spec|
20
20
  spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
21
  spec.require_paths = ['lib']
22
22
 
23
- spec.add_dependency 'oga', '~> 2.15'
23
+ spec.required_ruby_version = '>= 2.7'
24
24
 
25
- spec.add_development_dependency 'bundler', '>= 1.13'
26
- spec.add_development_dependency 'rake', '~> 10.0'
27
- spec.add_development_dependency 'rspec', '~> 3.6'
28
- spec.add_development_dependency 'rubocop', '~> 0.49.1'
25
+ spec.add_dependency 'oga', '~> 3.3'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 2.1'
28
+ spec.add_development_dependency 'rake', '~> 13.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.10'
30
+ spec.add_development_dependency 'rubocop', '~> 1.12'
29
31
  end
@@ -15,11 +15,19 @@ describe OGP::OpenGraph do
15
15
  end
16
16
 
17
17
  context 'with missing one of the required attributes' do
18
- it 'should raise an error' do
18
+ it 'should raise an error by default' do
19
19
  content = File.read("#{File.dirname(__FILE__)}/../fixtures/missing_required_attributes.html")
20
20
 
21
21
  expect { OGP::OpenGraph.new(content) }.to raise_error(OGP::MissingAttributeError)
22
22
  end
23
+ it 'should not raise an error if missing attribute is not required' do
24
+ content = File.read("#{File.dirname(__FILE__)}/../fixtures/missing_required_attributes.html")
25
+
26
+ open_graph = OGP::OpenGraph.new(content, { required_attributes: %w(title type url) })
27
+ expect(open_graph.title).to eql('The Rock')
28
+ expect(open_graph.type).to eql('video.movie')
29
+ expect(open_graph.url).to eql('http://www.imdb.com/title/tt0117500/')
30
+ end
23
31
  end
24
32
 
25
33
  context 'with nil source' do
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.4.0
4
+ version: 0.5.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-12-22 00:00:00.000000000 Z
11
+ date: 2021-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga
@@ -16,70 +16,70 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.15'
19
+ version: '3.3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.15'
26
+ version: '3.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.13'
33
+ version: '2.1'
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
- version: '1.13'
40
+ version: '2.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.6'
61
+ version: '3.10'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.6'
68
+ version: '3.10'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.49.1
75
+ version: '1.12'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.49.1
82
+ version: '1.12'
83
83
  description: Simple Ruby library for parsing Open Graph prototocol information. See
84
84
  http://ogp.me for more information.
85
85
  email:
@@ -125,27 +125,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
125
  requirements:
126
126
  - - ">="
127
127
  - !ruby/object:Gem::Version
128
- version: '0'
128
+ version: '2.7'
129
129
  required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  requirements:
131
131
  - - ">="
132
132
  - !ruby/object:Gem::Version
133
133
  version: '0'
134
134
  requirements: []
135
- rubygems_version: 3.0.3
135
+ rubygems_version: 3.1.2
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: Simple Ruby library for parsing Open Graph prototocol information.
139
- test_files:
140
- - spec/fixtures/audio_structured_attributes.html
141
- - spec/fixtures/custom_attributes.html
142
- - spec/fixtures/i18n_encoding.html
143
- - spec/fixtures/image_structured_attributes.html
144
- - spec/fixtures/missing_required_attributes.html
145
- - spec/fixtures/multiple_images_attributes.html
146
- - spec/fixtures/no_root_attributes.html
147
- - spec/fixtures/optional_attributes.html
148
- - spec/fixtures/required_attributes.html
149
- - spec/fixtures/video_structured_attributes.html
150
- - spec/ogp/open_graph_spec.rb
151
- - spec/spec_helper.rb
139
+ test_files: []