From 6eb54128e610a14b9f0d215aab589e468b20c90d Mon Sep 17 00:00:00 2001 From: Laura Wrubel Date: Tue, 5 Dec 2023 08:59:08 -0500 Subject: [PATCH] Use JSON.generate instead of to_json --- app/views/purl/show.html.erb | 2 +- lib/metadata/schema_dot_org.rb | 14 +- spec/lib/metadata/schema_dot_org_spec.rb | 209 ++++++++--------------- spec/model/purl_resource_spec.rb | 49 +++--- 4 files changed, 113 insertions(+), 161 deletions(-) diff --git a/app/views/purl/show.html.erb b/app/views/purl/show.html.erb index 37ae5272..bf226dc4 100644 --- a/app/views/purl/show.html.erb +++ b/app/views/purl/show.html.erb @@ -21,7 +21,7 @@ <%= tag :meta, name: 'citation_doi', content: @purl.doi_id %> <% end %> <% if @purl.schema_dot_org? %> - + <% end %> <% end %> diff --git a/lib/metadata/schema_dot_org.rb b/lib/metadata/schema_dot_org.rb index ce284838..025b269e 100644 --- a/lib/metadata/schema_dot_org.rb +++ b/lib/metadata/schema_dot_org.rb @@ -14,13 +14,13 @@ def initialize(cocina_json) end def call - { - "@context": 'http://schema.org', - "@type": schema_type, - "name": title_name, - "description": description - }.merge(format_specific_fields) - .compact + schema_fields = { "@context": 'http://schema.org', + "@type": schema_type, + "name": title_name, + "description": description } + .merge(format_specific_fields) + .compact + JSON.generate(schema_fields) end def schema_type? diff --git a/spec/lib/metadata/schema_dot_org_spec.rb b/spec/lib/metadata/schema_dot_org_spec.rb index 1493943a..873fa9b9 100644 --- a/spec/lib/metadata/schema_dot_org_spec.rb +++ b/spec/lib/metadata/schema_dot_org_spec.rb @@ -18,11 +18,10 @@ JSON end - it 'has type of Dataset' do - expect(schema_dot_org).to include( - "@context": 'http://schema.org', - "@type": 'Dataset' - ) + it 'has a type of Dataset' do + fields = JSON.parse(schema_dot_org) + expect(fields['@context']).to eq 'http://schema.org' + expect(fields['@type']).to eq 'Dataset' end end @@ -42,7 +41,7 @@ end it 'does not have type' do - expect(schema_dot_org).not_to have_key('@type') + expect(JSON.parse(schema_dot_org)).not_to have_key('@type') end end @@ -61,9 +60,8 @@ JSON end let(:body) do - { 'type' => 'rich', 'version' => '1.0', 'provider_name' => 'SUL Embed Service', 'title' => 'Oral history', 'height' => 400, 'width' => nil, - 'html' => '' } - .to_json + JSON.generate({ 'type' => 'rich', 'version' => '1.0', 'provider_name' => 'SUL Embed Service', 'title' => 'Oral history', 'height' => 400, 'width' => nil, + 'html' => '' }) end before do @@ -72,10 +70,9 @@ end it 'has type of Video' do - expect(schema_dot_org).to include( - "@context": 'http://schema.org', - "@type": 'Video' - ) + fields = JSON.parse(schema_dot_org) + expect(fields['@context']).to eq 'http://schema.org' + expect(fields['@type']).to eq 'Video' end end @@ -93,9 +90,7 @@ end it 'uses the first title' do - expect(schema_dot_org).to include( - "name": 'My Dataset' - ) + expect(JSON.parse(schema_dot_org)['name']).to eq 'My Dataset' end end @@ -122,9 +117,7 @@ end it 'includes the title' do - expect(schema_dot_org).to include( - "name": 'My Dataset\nMore title' - ) + expect(JSON.parse(schema_dot_org)['name']).to eq 'My Dataset\nMore title' end end @@ -142,9 +135,7 @@ end it 'includes the description' do - expect(schema_dot_org).to include( - "description": 'About this item' - ) + expect(JSON.parse(schema_dot_org)['description']).to eq 'About this item' end end @@ -162,9 +153,7 @@ end it 'includes the description' do - expect(schema_dot_org).to include( - "description": 'About this dataset' - ) + expect(JSON.parse(schema_dot_org)['description']).to eq 'About this dataset' end end @@ -182,12 +171,10 @@ end it 'uses the title instead' do - expect(schema_dot_org).to include( - "description": 'My Dataset' - ) + expect(JSON.parse(schema_dot_org)['description']).to eq 'My Dataset' end end - + context 'with a Dataset' do context 'with DOI in identification' do let(:cocina_json) do @@ -204,9 +191,7 @@ end it 'includes the DOI' do - expect(schema_dot_org).to include( - "identifier": ['https://doi.org/10.25740/hj293cv5980'] - ) + expect(JSON.parse(schema_dot_org)['identifier']).to eq ['https://doi.org/10.25740/hj293cv5980'] end end @@ -225,9 +210,7 @@ end it 'includes the DOI' do - expect(schema_dot_org).to include( - "identifier": ['https://doi.org/10.25740/hj293cv5980'] - ) + expect(JSON.parse(schema_dot_org)['identifier']).to eq ['https://doi.org/10.25740/hj293cv5980'] end end @@ -247,9 +230,7 @@ end it 'includes the DOI' do - expect(schema_dot_org).to include( - "identifier": ['https://doi.org/10.25740/hj293cv5980'] - ) + expect(JSON.parse(schema_dot_org)['identifier']).to eq ['https://doi.org/10.25740/hj293cv5980'] end end @@ -266,7 +247,7 @@ end it 'includes no identifier' do - expect(schema_dot_org).not_to have_key('identifier') + expect(JSON.parse(schema_dot_org)).not_to have_key('identifier') end end @@ -283,9 +264,7 @@ end it 'is accessibleForFree' do - expect(schema_dot_org).to include( - "isAccessibleForFree": true - ) + expect(JSON.parse(schema_dot_org)['isAccessibleForFree']).to eq true end end @@ -302,9 +281,7 @@ end it 'is not isAccessibleForFree' do - expect(schema_dot_org).to include( - "isAccessibleForFree": false - ) + expect(JSON.parse(schema_dot_org)['isAccessibleForFree']).to eq false end end @@ -321,9 +298,7 @@ end it 'includes the license' do - expect(schema_dot_org).to include( - "license": 'https://opendatacommons.org/licenses/by/1-0/' - ) + expect(JSON.parse(schema_dot_org)['license']).to eq 'https://opendatacommons.org/licenses/by/1-0/' end end @@ -340,9 +315,7 @@ end it 'includes a url' do - expect(schema_dot_org).to include( - "url": 'https://purl.stanford.edu/hj293cv5980' - ) + expect(JSON.parse(schema_dot_org)['url']).to eq 'https://purl.stanford.edu/hj293cv5980' end end @@ -360,15 +333,10 @@ end it 'includes creator' do - expect(schema_dot_org).to include( - "creator": [{ - "@type": 'Person', - "name": 'Doe, Jane' - }, { - "@type": 'Person', - "name": 'Foo, John' - }] - ) + expect(JSON.parse(schema_dot_org)['creator']).to eq [{ "@type": "Person", + "name": "Doe, Jane" }.stringify_keys, + { "@type": "Person", + "name": "Foo, John" }.stringify_keys] end end @@ -402,22 +370,20 @@ end it 'includes creator' do - expect(schema_dot_org).to include( - "creator": [ - { - "@type": 'Person', - "name": 'Jane Doe', - "givenName": 'Jane', - "familyName": 'Doe' - }, - { - "@type": 'Person', - "name": 'John Foo', - "givenName": 'John', - "familyName": 'Foo' - } - ] - ) + expect(JSON.parse(schema_dot_org)['creator']).to eq [ + { + "@type": 'Person', + "name": 'Jane Doe', + "givenName": 'Jane', + "familyName": 'Doe' + }.stringify_keys, + { + "@type": 'Person', + "name": 'John Foo', + "givenName": 'John', + "familyName": 'Foo' + }.stringify_keys + ] end end @@ -435,13 +401,11 @@ end it 'includes the ORCID' do - expect(schema_dot_org).to include( - "creator": [{ - "@type": 'Person', - "name": 'Doe, Jane', - "sameAs": 'https://orcid.org/0000-0000-0000-0000' - }] - ) + expect(JSON.parse(schema_dot_org)['creator']).to eq [{ + "@type": 'Person', + "name": 'Doe, Jane', + "sameAs": 'https://orcid.org/0000-0000-0000-0000' + }.stringify_keys] end end @@ -461,13 +425,11 @@ end it 'includes the ORCID' do - expect(schema_dot_org).to include( - "creator": [{ - "@type": 'Person', - "name": 'Doe, Jane', - "sameAs": 'https://orcid.org/0000-0000-0000-0000' - }] - ) + expect(JSON.parse(schema_dot_org)['creator']).to eq [{ + "@type": 'Person', + "name": 'Doe, Jane', + "sameAs": 'https://orcid.org/0000-0000-0000-0000' + }.stringify_keys] end end @@ -494,15 +456,13 @@ end it 'includes the ORCID' do - expect(schema_dot_org).to include( - "creator": [{ - "@type": 'Person', - "name": 'Jane Doe', - "givenName": 'Jane', - "familyName": 'Doe', - "sameAs": 'https://orcid.org/0000-0000-0000-0000' - }] - ) + expect(JSON.parse(schema_dot_org)['creator']).to eq [{ + "@type": 'Person', + "name": 'Jane Doe', + "givenName": 'Jane', + "familyName": 'Doe', + "sameAs": 'https://orcid.org/0000-0000-0000-0000' + }.stringify_keys] end end end @@ -532,24 +492,21 @@ JSON end let(:body) do - { 'type' => 'rich', 'version' => '1.0', 'provider_name' => 'SUL Embed Service', 'title' => 'Oral history', 'height' => 400, 'width' => nil, - 'html' => '' } - .to_json + JSON.generate({ 'type' => 'rich', 'version' => '1.0', 'provider_name' => 'SUL Embed Service', 'title' => 'Oral history', 'height' => 400, 'width' => nil, + 'html' => '' }) end before do allow(Settings.stacks).to receive(:url).and_return('https://example.com') allow(Settings.embed).to receive(:url_template).and_return('https://embed.stanford.edu/embed{.format}{?url*,application_options*}') - allow(Settings.embed).to receive(:url).and_return('https://purl.stanford.edu/%{druid}') + allow(Settings.embed).to receive(:url).and_return('https://purl.stanford.edu/%s') stub_request(:get, 'https://embed.stanford.edu/embed.json?url=https://purl.stanford.edu/tn153br1253') .to_return(status: 200, body:) end context 'with a thumbnail' do it 'includes the thumbnail' do - expect(schema_dot_org).to include( - "thumbnailUrl": 'https://example.com/file/druid:tn153br1253/tn153br1253_thumb.jp2' - ) + expect(JSON.parse(schema_dot_org)['thumbnailUrl']).to eq 'https://example.com/file/druid:tn153br1253/tn153br1253_thumb.jp2' end end @@ -578,7 +535,7 @@ end it 'does not include a thumbnail' do - expect(schema_dot_org).not_to have_key('thumbnailUrl') + expect(JSON.parse(schema_dot_org)).not_to have_key('thumbnailUrl') end end @@ -589,9 +546,7 @@ end it 'includes the embed_url' do - expect(schema_dot_org).to include( - "embedUrl": 'https://embed.stanford.edu/iframe?url=https://purl.stanford.edu/tn153br1253&_v=1701092229' - ) + expect(JSON.parse(schema_dot_org)['embedUrl']).to eq 'https://embed.stanford.edu/iframe?url=https://purl.stanford.edu/tn153br1253&_v=1701092229' end end @@ -601,16 +556,14 @@ end it 'does not include the embedUrl' do - expect(schema_dot_org).not_to have_key('embedUrl') + expect(JSON.parse(schema_dot_org)).not_to have_key('embedUrl') end end - + context 'with an event date' do context 'with value and status of primary' do it 'includes the uploadDate' do - expect(schema_dot_org).to include( - "uploadDate": '2014' - ) + expect(JSON.parse(schema_dot_org)['uploadDate']).to eq '2014' end end @@ -635,9 +588,7 @@ end it 'includes the primary uploadDate' do - expect(schema_dot_org).to include( - "uploadDate": '2014' - ) + expect(JSON.parse(schema_dot_org)['uploadDate']).to eq '2014' end end @@ -661,9 +612,7 @@ end it 'selects the first one for the uploadDate' do - expect(schema_dot_org).to include( - "uploadDate": '2014' - ) + expect(JSON.parse(schema_dot_org)['uploadDate']).to eq '2014' end end @@ -687,9 +636,7 @@ end it 'selects the first one for the uploadDate' do - expect(schema_dot_org).to include( - "uploadDate": '2014' - ) + expect(JSON.parse(schema_dot_org)['uploadDate']).to eq '2014' end end @@ -712,9 +659,7 @@ end it 'selects the first one for the uploadDate' do - expect(schema_dot_org).to include( - "uploadDate": '2014' - ) + expect(JSON.parse(schema_dot_org)['uploadDate']).to eq '2014' end end @@ -736,9 +681,7 @@ end it 'selects the first one for the uploadDate' do - expect(schema_dot_org).to include( - "uploadDate": '2014' - ) + expect(JSON.parse(schema_dot_org)['uploadDate']).to eq '2014' end end @@ -759,9 +702,7 @@ end it 'selects the first date for the uploadDate' do - expect(schema_dot_org).to include( - "uploadDate": '2014' - ) + expect(JSON.parse(schema_dot_org)['uploadDate']).to eq '2014' end end @@ -782,7 +723,7 @@ end it 'does not include an uploadDate' do - expect(schema_dot_org).not_to have_key('uploadDate') + expect(JSON.parse(schema_dot_org)).not_to have_key('uploadDate') end end end diff --git a/spec/model/purl_resource_spec.rb b/spec/model/purl_resource_spec.rb index f6178ba3..f9d9ece5 100644 --- a/spec/model/purl_resource_spec.rb +++ b/spec/model/purl_resource_spec.rb @@ -370,6 +370,19 @@ describe '#schema_dot_org? and #schema_dot_org' do context 'with a dataset' do + let(:expected_json) do + <<~JSON + {"@context": "http://schema.org", + "@type": "Dataset", + "name": "AVOIDDS: A dataset for vision-based aircraft detection", + "isAccessibleForFree": false, + "creator" : [], + "identifier": ["https://doi.org/10.25740/hj293cv5980"], + "description": "About this dataset." + } + JSON + end + before do allow(subject).to receive(:cocina_body).and_return <<~JSON { @@ -385,21 +398,27 @@ end it 'returns schema.org markup' do - expect(subject.schema_dot_org).to include( - "@context": 'http://schema.org', - "@type": 'Dataset', - "name": 'AVOIDDS: A dataset for vision-based aircraft detection', - "identifier": ['https://doi.org/10.25740/hj293cv5980'], - "description": 'About this dataset.' - ) + expect(JSON.parse(subject.schema_dot_org)).to eq JSON.parse(expected_json) end end context 'with a video' do let(:body) do - { 'type' => 'rich', 'version' => '1.0', 'provider_name' => 'SUL Embed Service', 'title' => 'Oral history', 'height' => 400, 'width' => nil, - 'html' => '' } - .to_json + JSON.generate( + { 'type' => 'rich', 'version' => '1.0', 'provider_name' => 'SUL Embed Service', 'title' => 'Oral history', 'height' => 400, 'width' => nil, + 'html' => '' } + ) + end + let(:expected_json) do + <<~JSON + {"@context": "http://schema.org", + "@type": "Video", + "name": "A Video Title", + "description": "What is in this video?", + "uploadDate": "2000", + "thumbnailUrl": "https://example.com/file/druid:tn153br1253/tn153br1253_thumb.jp2", + "embedUrl": "https://embed.stanford.edu/iframe?url=https://purl.stanford.edu/tn153br1253&_v=1701092229"} + JSON end before do @@ -432,15 +451,7 @@ end it 'returns schema.org markup' do - expect(subject.schema_dot_org).to include( - "@context": 'http://schema.org', - "@type": 'Video', - "name": 'A Video Title', - "description": 'What is in this video?', - "uploadDate": '2000', - "thumbnailUrl": 'https://example.com/file/druid:tn153br1253/tn153br1253_thumb.jp2', - "embedUrl": 'https://embed.stanford.edu/iframe?url=https://purl.stanford.edu/tn153br1253&_v=1701092229' - ) + expect(JSON.parse(subject.schema_dot_org)).to eq(JSON.parse(expected_json)) end end