Change first_of_value to first_of_hash, and add more test cases (#33647)
				
					
				
			This commit is contained in:
		@@ -46,7 +46,7 @@ class LinkDetailsExtractor
 | 
				
			|||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def image
 | 
					    def image
 | 
				
			||||||
      obj = first_of_value(json['image'])
 | 
					      obj = first_of_hash(json['image'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return obj['url'] if obj.is_a?(Hash)
 | 
					      return obj['url'] if obj.is_a?(Hash)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -85,15 +85,15 @@ class LinkDetailsExtractor
 | 
				
			|||||||
    private
 | 
					    private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def author
 | 
					    def author
 | 
				
			||||||
      first_of_value(json['author']) || {}
 | 
					      first_of_hash(json['author']) || {}
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def publisher
 | 
					    def publisher
 | 
				
			||||||
      first_of_value(json['publisher']) || {}
 | 
					      first_of_hash(json['publisher']) || {}
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def first_of_value(arr)
 | 
					    def first_of_hash(arr)
 | 
				
			||||||
      arr.is_a?(Array) ? arr.first : arr
 | 
					      arr.is_a?(Array) ? arr.flatten.find { |item| item.is_a?(Hash) } : arr
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def root_array(root)
 | 
					    def root_array(root)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -249,6 +249,44 @@ RSpec.describe LinkDetailsExtractor do
 | 
				
			|||||||
        expect(subject.author_name).to eq 'Author 1, Author 2'
 | 
					        expect(subject.author_name).to eq 'Author 1, Author 2'
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    context 'with embedded arrays' do
 | 
				
			||||||
 | 
					      let(:ld_json) do
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          '@context' => 'https://schema.org',
 | 
				
			||||||
 | 
					          '@type' => 'NewsArticle',
 | 
				
			||||||
 | 
					          'headline' => 'A lot of authors',
 | 
				
			||||||
 | 
					          'description' => 'But we decided to cram them into one',
 | 
				
			||||||
 | 
					          'author' => [[{
 | 
				
			||||||
 | 
					            '@type' => 'Person',
 | 
				
			||||||
 | 
					            'name' => ['Author 1'],
 | 
				
			||||||
 | 
					          }]],
 | 
				
			||||||
 | 
					          'publisher' => [[{
 | 
				
			||||||
 | 
					            '@type' => 'NewsMediaOrganization',
 | 
				
			||||||
 | 
					            'name' => 'Pet News',
 | 
				
			||||||
 | 
					            'url' => 'https://example.com',
 | 
				
			||||||
 | 
					          }]],
 | 
				
			||||||
 | 
					        }.to_json
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					      let(:html) { <<~HTML }
 | 
				
			||||||
 | 
					        <!doctype html>
 | 
				
			||||||
 | 
					        <html>
 | 
				
			||||||
 | 
					        <body>
 | 
				
			||||||
 | 
					          <script type="application/ld+json">
 | 
				
			||||||
 | 
					            #{ld_json}
 | 
				
			||||||
 | 
					          </script>
 | 
				
			||||||
 | 
					        </body>
 | 
				
			||||||
 | 
					        </html>
 | 
				
			||||||
 | 
					      HTML
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it 'gives correct author_name' do
 | 
				
			||||||
 | 
					        expect(subject.author_name).to eq 'Author 1'
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it 'gives provider_name' do
 | 
				
			||||||
 | 
					        expect(subject.provider_name).to eq 'Pet News'
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  context 'when Open Graph protocol data is present' do
 | 
					  context 'when Open Graph protocol data is present' do
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user