From e68f7a13ae8d26b98639bdd3dc51325563bfe1c5 Mon Sep 17 00:00:00 2001 From: Anusha Ranganathan Date: Tue, 17 Nov 2015 23:24:22 +0000 Subject: [PATCH 1/8] Added model for JSON annotation data --- app/models/concerns/annotation_data.rb | 33 +++ app/models/solr_document.rb | 1 + .../annotation_records/annotation-001.json | 254 +++++++++++++++++ .../annotation_records/annotation_fixtures.rb | 262 ++++++++++++++++++ .../concerns/annotation_data_reader_spec.rb | 58 ++++ spec/models/concerns/annotation_data_spec.rb | 53 ++++ spec/spec_helper.rb | 1 + 7 files changed, 662 insertions(+) create mode 100644 app/models/concerns/annotation_data.rb create mode 100644 spec/fixtures/annotation_records/annotation-001.json create mode 100644 spec/fixtures/annotation_records/annotation_fixtures.rb create mode 100644 spec/models/concerns/annotation_data_reader_spec.rb create mode 100644 spec/models/concerns/annotation_data_spec.rb diff --git a/app/models/concerns/annotation_data.rb b/app/models/concerns/annotation_data.rb new file mode 100644 index 0000000..5345dc1 --- /dev/null +++ b/app/models/concerns/annotation_data.rb @@ -0,0 +1,33 @@ +module AnnotationData + extend ActiveSupport::Concern + + attr_accessor :annotationjson + + def annotation + return nil unless self[:annotationjson] + @annotation = Reader.new.from_str(self[:annotationjson]) + end + + class Reader + def from_str(str) + @annotation = JSON.parse(str) + end + + def from_url(url, encoding = nil) + require 'open-uri' + @annotation = JSON.parse(open(url).read) + end + + # Read in the contents of a JSON record from a file. + # @param filename (String) - path to file that has mods xml as its content + # @return Nokogiri::XML::Document + # @example + # foo = AnnoData::Reader.new.from_file('/path/to/mods/file.xml') + def from_file(filename, encoding = nil) + file = File.read(filename) + @annotation = JSON.parse(file) + end + + end + +end diff --git a/app/models/solr_document.rb b/app/models/solr_document.rb index 2f2e3cf..9599723 100644 --- a/app/models/solr_document.rb +++ b/app/models/solr_document.rb @@ -2,6 +2,7 @@ class SolrDocument include ModsData + include AnnotationData include Blacklight::Solr::Document attr_accessor :id, :purl, :collection, :manifest diff --git a/spec/fixtures/annotation_records/annotation-001.json b/spec/fixtures/annotation_records/annotation-001.json new file mode 100644 index 0000000..1f9d72a --- /dev/null +++ b/spec/fixtures/annotation_records/annotation-001.json @@ -0,0 +1,254 @@ +{ + "@context": "http://www.shared-canvas.org/ns/context.json", + "@id": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json", + "@type": "sc:AnnotationList", + "resources": [ + { + "@id": "_:N43deaea09a5345379218db8cb72600c3", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "7377e5fe51c46454bb01b62a817a4d42", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "Erant aut[em] qui manducaverant", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,450,1017,166" + }, + { + "@id": "_:N14eeb4370547431db7bb7fc075e43210", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "c15b3c62f8532a37ca4ba1c9deb84d6a", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "quasi quatuor milia hominu[m] et", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,616,1017,153" + }, + { + "@id": "_:N1a08bd845f484bd9960cf323949e8f6e", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "bd8f9e22c2d2d6691e78f6d02143a80d", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "et dimisit eos Et stati[m] ascendens na-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,769,1017,162" + }, + { + "@id": "_:N82e5366197f24bc1ae583248d73e36fc", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "d2771941ce394de6aea13779d4f058ea", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-ve[m] cu[m] discip[u]lis suis venit in par-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,931,1017,180" + }, + { + "@id": "_:N8734944c5867422dbb2e05ce5a9386af", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "deabaa77448a61c561836d1fb064f885", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-tes dalmanutha [Et] exier[un]t phari-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1111,1017,162" + }, + { + "@id": "_:Nec72601c72094655ae7b0df521dd3e7f", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "61574db3e19725509b692c3a099e0bc7", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-sei et ceper[un]t conquirere cu[m] eo que-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1274,1017,153" + }, + { + "@id": "_:N689608cc1b53445c8cbc42ffb3929a6d", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "8f42470a851d3b5c1af46b8e7477f861", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-rentes ab illo signu[m] de celo te[m]pta[n]tes", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1427,1017,175" + }, + { + "@id": "_:Nad1e54bc8428459b804639919f670908", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "677b8baf5f31cc035202320f74a59827", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "eu[m] [Et] ingemiscens sp[irit]u ait Q[ui]d g[e]n[er]a-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1602,1017,139" + }, + { + "@id": "_:N51325f819f1e4bac8558b71419ae29a3", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "e91f46cf59fac81d268322d7af89378b", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-tio ista signu[m] querit Amen dico", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1742,1017,153" + }, + { + "@id": "_:Ne1d69f7bca434ef583a1d3711237cbb7", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "654a116a63555dfcef929d207c5cc85d", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "vob[is] si dabit[ur] g[e]n[er]ationi isti signum", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1895,1017,162" + }, + { + "@id": "_:N34ba429a91c142f6b19a89b28780bff7", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "b2c3fdb829625d9edad520358ba24e2b", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "Et dimittens eos ascendens it[eru]m na-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2057,1017,153" + }, + { + "@id": "_:N458e8ba2c83e48ef9f8039209aa28013", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "d75d079522a83fd9c29ffdc54e0c8433", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-ve[m] abiit t[ra]ns fredu[m] et obliti s[un]t sume-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2210,1017,148" + }, + { + "@id": "_:Ne96dfb3e84ee42c39a24cad1ba1c75ec", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "ad687791dc66c33d889c65d28a919406", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-re panes et nisi unu[m] pane[m] secu[m] no[n]", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2359,1017,175" + }, + { + "@id": "_:N8b42c7d2e7de4cf38a37ab0da9893059", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "ef1d64bc4e7fb0cb227a9dab4190116b", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "habebant in navi Et p[re]cipiebat", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2534,1017,139" + }, + { + "@id": "_:N273697814e554ba3a26d0c49bf67daed", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "91c038e14ba8af0e9b5df0e98b830b2e", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "eis dicens Videte cavete a ferme[n]-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2674,1017,189" + }, + { + "@id": "_:N3aaf92430d484bf1b2869b333536395a", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "149e313d4be2f9ed97d51ba1a71a36c6", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-to phariseo[rum] et fermento herodis", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2863,1017,135" + }, + { + "@id": "_:N9dddfad68e9c47238f2827260742ba33", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "0ced5cdb34ba7d00348e25e435da7824", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "Et cogitabant ad alt[er]utru[m] dicentes", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2998,1017,162" + }, + { + "@id": "_:Nc9444cda88ad42ae888a42e15d17f83b", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "aff9de356f29c386cd506b86d2798997", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "Quia panes no[n] h[abe]m[u]s quo cognito", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,3160,1017,184" + }, + { + "@id": "_:Nbf9f14138d3d4046b5bbeb6989f1a2d7", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "ed3bc7f8a8205ef288978bee5cc80410", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "[Jesus] ait illis Quid cogitatis q[uia] pane", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,3344,1017,135" + } + ] +} \ No newline at end of file diff --git a/spec/fixtures/annotation_records/annotation_fixtures.rb b/spec/fixtures/annotation_records/annotation_fixtures.rb new file mode 100644 index 0000000..acae168 --- /dev/null +++ b/spec/fixtures/annotation_records/annotation_fixtures.rb @@ -0,0 +1,262 @@ +# encoding: UTF-8 +module AnnotationFixtures + def annotation_001 + ' + { + "@context": "http://www.shared-canvas.org/ns/context.json", + "@id": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json", + "@type": "sc:AnnotationList", + "resources": [ + { + "@id": "_:N43deaea09a5345379218db8cb72600c3", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "7377e5fe51c46454bb01b62a817a4d42", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "Erant aut[em] qui manducaverant", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,450,1017,166" + }, + { + "@id": "_:N14eeb4370547431db7bb7fc075e43210", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "c15b3c62f8532a37ca4ba1c9deb84d6a", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "quasi quatuor milia hominu[m] et", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,616,1017,153" + }, + { + "@id": "_:N1a08bd845f484bd9960cf323949e8f6e", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "bd8f9e22c2d2d6691e78f6d02143a80d", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "et dimisit eos Et stati[m] ascendens na-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,769,1017,162" + }, + { + "@id": "_:N82e5366197f24bc1ae583248d73e36fc", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "d2771941ce394de6aea13779d4f058ea", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-ve[m] cu[m] discip[u]lis suis venit in par-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,931,1017,180" + }, + { + "@id": "_:N8734944c5867422dbb2e05ce5a9386af", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "deabaa77448a61c561836d1fb064f885", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-tes dalmanutha [Et] exier[un]t phari-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1111,1017,162" + }, + { + "@id": "_:Nec72601c72094655ae7b0df521dd3e7f", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "61574db3e19725509b692c3a099e0bc7", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-sei et ceper[un]t conquirere cu[m] eo que-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1274,1017,153" + }, + { + "@id": "_:N689608cc1b53445c8cbc42ffb3929a6d", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "8f42470a851d3b5c1af46b8e7477f861", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-rentes ab illo signu[m] de celo te[m]pta[n]tes", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1427,1017,175" + }, + { + "@id": "_:Nad1e54bc8428459b804639919f670908", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "677b8baf5f31cc035202320f74a59827", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "eu[m] [Et] ingemiscens sp[irit]u ait Q[ui]d g[e]n[er]a-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1602,1017,139" + }, + { + "@id": "_:N51325f819f1e4bac8558b71419ae29a3", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "e91f46cf59fac81d268322d7af89378b", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-tio ista signu[m] querit Amen dico", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1742,1017,153" + }, + { + "@id": "_:Ne1d69f7bca434ef583a1d3711237cbb7", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "654a116a63555dfcef929d207c5cc85d", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "vob[is] si dabit[ur] g[e]n[er]ationi isti signum", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1895,1017,162" + }, + { + "@id": "_:N34ba429a91c142f6b19a89b28780bff7", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "b2c3fdb829625d9edad520358ba24e2b", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "Et dimittens eos ascendens it[eru]m na-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2057,1017,153" + }, + { + "@id": "_:N458e8ba2c83e48ef9f8039209aa28013", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "d75d079522a83fd9c29ffdc54e0c8433", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-ve[m] abiit t[ra]ns fredu[m] et obliti s[un]t sume-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2210,1017,148" + }, + { + "@id": "_:Ne96dfb3e84ee42c39a24cad1ba1c75ec", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "ad687791dc66c33d889c65d28a919406", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-re panes et nisi unu[m] pane[m] secu[m] no[n]", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2359,1017,175" + }, + { + "@id": "_:N8b42c7d2e7de4cf38a37ab0da9893059", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "ef1d64bc4e7fb0cb227a9dab4190116b", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "habebant in navi Et p[re]cipiebat", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2534,1017,139" + }, + { + "@id": "_:N273697814e554ba3a26d0c49bf67daed", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "91c038e14ba8af0e9b5df0e98b830b2e", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "eis dicens Videte cavete a ferme[n]-", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2674,1017,189" + }, + { + "@id": "_:N3aaf92430d484bf1b2869b333536395a", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "149e313d4be2f9ed97d51ba1a71a36c6", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "-to phariseo[rum] et fermento herodis", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2863,1017,135" + }, + { + "@id": "_:N9dddfad68e9c47238f2827260742ba33", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "0ced5cdb34ba7d00348e25e435da7824", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "Et cogitabant ad alt[er]utru[m] dicentes", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2998,1017,162" + }, + { + "@id": "_:Nc9444cda88ad42ae888a42e15d17f83b", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "aff9de356f29c386cd506b86d2798997", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "Quia panes no[n] h[abe]m[u]s quo cognito", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,3160,1017,184" + }, + { + "@id": "_:Nbf9f14138d3d4046b5bbeb6989f1a2d7", + "@type": "oa:Annotation", + "motivation": "sc:painting", + "resource": { + "@id": "ed3bc7f8a8205ef288978bee5cc80410", + "@type": "cnt:ContentAsText", + "format": "text/plain", + "chars": "[Jesus] ait illis Quid cogitatis q[uia] pane", + "language": "lat" + }, + "on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,3344,1017,135" + } + ] +} +' + end + +end diff --git a/spec/models/concerns/annotation_data_reader_spec.rb b/spec/models/concerns/annotation_data_reader_spec.rb new file mode 100644 index 0000000..7ce7436 --- /dev/null +++ b/spec/models/concerns/annotation_data_reader_spec.rb @@ -0,0 +1,58 @@ +require "spec_helper" + +class AnnotationDataReaderTestClass + include AnnotationData +end + +describe AnnotationData::Reader do + include AnnotationFixtures + before(:all) do + @example_str = annotation_001 + @example_url = 'http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json' + @fixture_dir = Rails.root.join "spec/fixtures" + @fixture_annotation_file = File.join(@fixture_dir, 'annotation_records/annotation-001.json') + @doc_from_str = AnnotationData::Reader.new.from_str(@example_str) + @doc_from_url = AnnotationData::Reader.new.from_url(@example_url) + @doc_from_file = AnnotationData::Reader.new.from_file(@fixture_annotation_file) + end + + context "from_str" do + it "from_str should turn a json string into a Hash" do + expect(@doc_from_str).to be_a Hash + end + it "should have the keys @context, @id, @type and resources" do + expect(@doc_from_str.keys).to eq(["@context", "@id", "@type", "resources"]) + end + it "should have an array of resources" do + expect(@doc_from_str["resources"]).to be_a Array + end + end + + context "from_url" do + it "from_url should turn the contents at the url into a Hash" do + expect(@doc_from_url).to be_a Hash + end + it "should have the keys @context, @id, @type and resources" do + expect(@doc_from_url.keys).to eq(["@context", "@id", "@type", "resources"]) + end + it "should have an array of resources" do + expect(@doc_from_url["resources"]).to be_a Array + end + end + + context "from_file" do + it "should turn the contents of a file into a Hash" do + expect(@doc_from_file).to be_a Hash + end + it "should give a meaningful error if passed a bad file" do + expect(lambda{AnnotationData::Record.new.from_file('/fake/file')}).to raise_error + end + it "should have the keys @context, @id, @type and resources" do + expect(@doc_from_url.keys).to eq(["@context", "@id", "@type", "resources"]) + end + it "should have an array of resources" do + expect(@doc_from_url["resources"]).to be_a Array + end + end + +end diff --git a/spec/models/concerns/annotation_data_spec.rb b/spec/models/concerns/annotation_data_spec.rb new file mode 100644 index 0000000..1710b52 --- /dev/null +++ b/spec/models/concerns/annotation_data_spec.rb @@ -0,0 +1,53 @@ +require "spec_helper" + +class AnnotationDataTestClass + include AnnotationData +end + +describe AnnotationData do + include AnnotationFixtures + #anno_str = File.read("fixtures/annotation_records/annotation-001.json") + let(:document) { SolrDocument.new(annotationjson: annotation_001) } + let(:anno1) { { + "@id" => "_:N43deaea09a5345379218db8cb72600c3", + "@type" => "oa:Annotation", + "motivation" => "sc:painting", + "resource" => + { + "@id" => "7377e5fe51c46454bb01b62a817a4d42", + "@type" => "cnt:ContentAsText", + "format" => "text/plain", + "chars" => "Erant aut[em] qui manducaverant", + "language" => "lat" + }, + "on" => "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,450,1017,166" + } } + + describe "#annotation" do + it "should be nil if no annotationjson" do + expect(SolrDocument.new().annotation).to be_nil + end + + it "should be a Hash" do + expect(document.annotation).to be_a Hash + end + + it "should have the keys @context, @id, @type and resources" do + expect(document.annotation.keys).to eq(["@context", "@id", "@type", "resources"]) + end + + it "should have an array of resources" do + expect(document.annotation["resources"]).to be_a Array + end + + it "should have 19 annotations" do + expect(document.annotation["resources"].length).to eq 19 + end + + it "should have the first annotation equal anno1" do + expect(document.annotation["resources"].first).to eq(anno1) + end + + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c2263e0..2f51a5b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,7 @@ #require 'fixtures/marc_records/marc_856_fixtures' #require 'fixtures/marc_records/marc_metadata_fixtures' require 'fixtures/mods_records/mods_fixtures' +require 'fixtures/annotation_records/annotation_fixtures' Capybara.register_driver :poltergeist do |app| Capybara::Poltergeist::Driver.new(app, {timeout: 60}) From 35272464298979bd45c7601e4912414b814bd00d Mon Sep 17 00:00:00 2001 From: Anusha Ranganathan Date: Tue, 17 Nov 2015 23:37:13 +0000 Subject: [PATCH 2/8] Fixed variable in spec for from_file --- app/models/concerns/annotation_data.rb | 6 +++--- spec/models/concerns/annotation_data_reader_spec.rb | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/concerns/annotation_data.rb b/app/models/concerns/annotation_data.rb index 5345dc1..9afdc9b 100644 --- a/app/models/concerns/annotation_data.rb +++ b/app/models/concerns/annotation_data.rb @@ -19,10 +19,10 @@ def from_url(url, encoding = nil) end # Read in the contents of a JSON record from a file. - # @param filename (String) - path to file that has mods xml as its content - # @return Nokogiri::XML::Document + # @param filename (String) - path to file that has the annotations json as its content + # @return Hash # @example - # foo = AnnoData::Reader.new.from_file('/path/to/mods/file.xml') + # foo = AnnotationData::Reader.new.from_file('/path/to/annotation/file.json') def from_file(filename, encoding = nil) file = File.read(filename) @annotation = JSON.parse(file) diff --git a/spec/models/concerns/annotation_data_reader_spec.rb b/spec/models/concerns/annotation_data_reader_spec.rb index 7ce7436..7a727ac 100644 --- a/spec/models/concerns/annotation_data_reader_spec.rb +++ b/spec/models/concerns/annotation_data_reader_spec.rb @@ -48,10 +48,10 @@ class AnnotationDataReaderTestClass expect(lambda{AnnotationData::Record.new.from_file('/fake/file')}).to raise_error end it "should have the keys @context, @id, @type and resources" do - expect(@doc_from_url.keys).to eq(["@context", "@id", "@type", "resources"]) + expect(@doc_from_file.keys).to eq(["@context", "@id", "@type", "resources"]) end it "should have an array of resources" do - expect(@doc_from_url["resources"]).to be_a Array + expect(@doc_from_file["resources"]).to be_a Array end end From 2b61ddf969389389c8b4d009ec1d4fc520b87fe2 Mon Sep 17 00:00:00 2001 From: Anusha Ranganathan Date: Thu, 26 Nov 2015 20:36:05 +0000 Subject: [PATCH 3/8] Placed json reader within new module --- app/models/concerns/annotation_data.rb | 25 ++----------------- app/models/concerns/json_reader.rb | 23 +++++++++++++++++ ...ata_reader_spec.rb => json_reader_spec.rb} | 14 +++++------ 3 files changed, 32 insertions(+), 30 deletions(-) create mode 100644 app/models/concerns/json_reader.rb rename spec/models/concerns/{annotation_data_reader_spec.rb => json_reader_spec.rb} (80%) diff --git a/app/models/concerns/annotation_data.rb b/app/models/concerns/annotation_data.rb index 9afdc9b..9058218 100644 --- a/app/models/concerns/annotation_data.rb +++ b/app/models/concerns/annotation_data.rb @@ -1,33 +1,12 @@ module AnnotationData + include JsonReader extend ActiveSupport::Concern attr_accessor :annotationjson def annotation return nil unless self[:annotationjson] - @annotation = Reader.new.from_str(self[:annotationjson]) - end - - class Reader - def from_str(str) - @annotation = JSON.parse(str) - end - - def from_url(url, encoding = nil) - require 'open-uri' - @annotation = JSON.parse(open(url).read) - end - - # Read in the contents of a JSON record from a file. - # @param filename (String) - path to file that has the annotations json as its content - # @return Hash - # @example - # foo = AnnotationData::Reader.new.from_file('/path/to/annotation/file.json') - def from_file(filename, encoding = nil) - file = File.read(filename) - @annotation = JSON.parse(file) - end - + @annotation = JsonReader::Reader.new.from_str(self[:annotationjson]) end end diff --git a/app/models/concerns/json_reader.rb b/app/models/concerns/json_reader.rb new file mode 100644 index 0000000..4ef4004 --- /dev/null +++ b/app/models/concerns/json_reader.rb @@ -0,0 +1,23 @@ +module JsonReader + class Reader + def from_str(str) + @annotation = JSON.parse(str) + end + + def from_url(url, encoding = nil) + require 'open-uri' + @annotation = JSON.parse(open(url).read) + end + + # Read in the contents of a JSON record from a file. + # @param filename (String) - path to file that has the annotations json as its content + # @return Hash + # @example + # foo = AnnotationData::Reader.new.from_file('/path/to/annotation/file.json') + def from_file(filename, encoding = nil) + file = File.read(filename) + @annotation = JSON.parse(file) + end + + end +end diff --git a/spec/models/concerns/annotation_data_reader_spec.rb b/spec/models/concerns/json_reader_spec.rb similarity index 80% rename from spec/models/concerns/annotation_data_reader_spec.rb rename to spec/models/concerns/json_reader_spec.rb index 7a727ac..adff781 100644 --- a/spec/models/concerns/annotation_data_reader_spec.rb +++ b/spec/models/concerns/json_reader_spec.rb @@ -1,19 +1,19 @@ require "spec_helper" -class AnnotationDataReaderTestClass - include AnnotationData +class JsonReaderTestClass + include JsonReader end -describe AnnotationData::Reader do +describe JsonReader::Reader do include AnnotationFixtures before(:all) do @example_str = annotation_001 @example_url = 'http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json' @fixture_dir = Rails.root.join "spec/fixtures" @fixture_annotation_file = File.join(@fixture_dir, 'annotation_records/annotation-001.json') - @doc_from_str = AnnotationData::Reader.new.from_str(@example_str) - @doc_from_url = AnnotationData::Reader.new.from_url(@example_url) - @doc_from_file = AnnotationData::Reader.new.from_file(@fixture_annotation_file) + @doc_from_str = JsonReader::Reader.new.from_str(@example_str) + @doc_from_url = JsonReader::Reader.new.from_url(@example_url) + @doc_from_file = JsonReader::Reader.new.from_file(@fixture_annotation_file) end context "from_str" do @@ -45,7 +45,7 @@ class AnnotationDataReaderTestClass expect(@doc_from_file).to be_a Hash end it "should give a meaningful error if passed a bad file" do - expect(lambda{AnnotationData::Record.new.from_file('/fake/file')}).to raise_error + expect(lambda{JsonReader::Record.new.from_file('/fake/file')}).to raise_error end it "should have the keys @context, @id, @type and resources" do expect(@doc_from_file.keys).to eq(["@context", "@id", "@type", "resources"]) From 6ea5c11fb9fe8be67ac143031256defd96bb3e4d Mon Sep 17 00:00:00 2001 From: Anusha Ranganathan Date: Tue, 15 Dec 2015 22:16:57 +0000 Subject: [PATCH 4/8] Removed instance variable from class --- app/models/concerns/json_reader.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/json_reader.rb b/app/models/concerns/json_reader.rb index 4ef4004..001eb42 100644 --- a/app/models/concerns/json_reader.rb +++ b/app/models/concerns/json_reader.rb @@ -1,12 +1,12 @@ module JsonReader class Reader def from_str(str) - @annotation = JSON.parse(str) + JSON.parse(str) end def from_url(url, encoding = nil) require 'open-uri' - @annotation = JSON.parse(open(url).read) + JSON.parse(open(url).read) end # Read in the contents of a JSON record from a file. @@ -16,7 +16,7 @@ def from_url(url, encoding = nil) # foo = AnnotationData::Reader.new.from_file('/path/to/annotation/file.json') def from_file(filename, encoding = nil) file = File.read(filename) - @annotation = JSON.parse(file) + JSON.parse(file) end end From 0a521f23b019bb3ca809c4c3361151d4e6c6d3eb Mon Sep 17 00:00:00 2001 From: Anusha Ranganathan Date: Sat, 28 Nov 2015 02:09:53 +0000 Subject: [PATCH 5/8] Modified the annotations data model to return instance variables --- app/models/concerns/annotation_data.rb | 73 +++++++- .../annotation_records/annotation_fixtures.rb | 48 +++++- spec/models/concerns/annotation_data_spec.rb | 156 +++++++++++++++--- spec/models/concerns/json_reader_spec.rb | 2 +- 4 files changed, 245 insertions(+), 34 deletions(-) diff --git a/app/models/concerns/annotation_data.rb b/app/models/concerns/annotation_data.rb index 9058218..61af0f4 100644 --- a/app/models/concerns/annotation_data.rb +++ b/app/models/concerns/annotation_data.rb @@ -1,12 +1,73 @@ module AnnotationData include JsonReader extend ActiveSupport::Concern - - attr_accessor :annotationjson - - def annotation - return nil unless self[:annotationjson] - @annotation = JsonReader::Reader.new.from_str(self[:annotationjson]) + + #attr_accessor :annotation_url, :annotation_list + + def read_annotation(url=nil) + return nil unless url #self[:annotation_url] + @annotation_list = JsonReader::Reader.new.from_url(url) + #self[:annotation_list] = JsonReader::Reader.new.from_str(self[:annotation_url]) + end + + def annotations(annotation_list=nil) + # the motivation for annotations will be: "oa:commenting" + #return [] unless self[:annotation_url] + #self.read_annotation unless self[:annotation_list] + #return [] unless self[:annotation_list] + #return self[:annotation_list][:resources].select {|anno| anno["motivation"] == "oa:commenting" } + return [] unless annotation_list + return annotation_list["resources"].select {|anno| anno["motivation"] == "oa:commenting" } + end + + def transcriptions(annotation_list=nil) + # the motivation for transcriptions will be: "sc:painting" + #return [] unless self[:annotation_url] + #self.read_annotation unless self[:annotation_list] + #return [] unless self[:annotation_list] + #return self[:annotation_list][:resources].select {|anno| anno["motivation"] == "sc:painting" } + return [] unless annotation_list + return annotation_list["resources"].select {|anno| anno["motivation"] == "sc:painting" } + end + + def map_annotation(annotation=nil) + return {} unless annotation + anno = {} + anno['id'] = annotation["@id"] + anno['target_type'] = annotation["@type"] + anno['motivation'] = annotation["motivation"] + anno['target_url'] = annotation["on"] + anno['body_type'] = annotation["resource"]["@type"] + #TODO Convert format to human form? + anno['body_format'] = annotation["resource"]["format"] + anno['body_chars'] = annotation["resource"]["chars"] + #TODO: Get the language word from code + anno['body_language'] = annotation["resource"]["language"] + anno + end + + def annotation_to_solr(data={}) + # data.keys = [:annotation, :manuscript, :folio, :url] + return {} unless data["annotation"] + anno = self.map_annotation(data["annotation"]) + return {} unless (anno["id"]) + solr_doc = {} + solr_doc["id"] = anno['id'] + solr_doc["druid"] = self["druid"] + solr_doc["url_sfx"] = data["url"] + solr_doc["manifest_urls"] = self["iiif_manifest"] + solr_doc["collection"] = self["collection"] + solr_doc["folio"] = data["folio"] + solr_doc["manuscript_search"] = data["manuscript"] + solr_doc["model"] = anno["motivation"] + solr_doc["motivation"] = anno["motivation"] + solr_doc["target_url"] = anno["target_url"] + solr_doc["target_type"] = anno["target_type"] + solr_doc["body_type"] = anno["body_type"] + solr_doc["format"] = anno["body_format"] + solr_doc["body_chars_search"] = anno["body_chars"] + solr_doc["language"] = anno["body_language"] + solr_doc end end diff --git a/spec/fixtures/annotation_records/annotation_fixtures.rb b/spec/fixtures/annotation_records/annotation_fixtures.rb index acae168..a0edce9 100644 --- a/spec/fixtures/annotation_records/annotation_fixtures.rb +++ b/spec/fixtures/annotation_records/annotation_fixtures.rb @@ -1,6 +1,10 @@ # encoding: UTF-8 module AnnotationFixtures - def annotation_001 + def annotation_url_001 + "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json" + end + + def annotation_list_001 ' { "@context": "http://www.shared-canvas.org/ns/context.json", @@ -258,5 +262,47 @@ def annotation_001 } ' end + + def annotation_001 + { + "@id" => "_:N43deaea09a5345379218db8cb72600c3", + "@type" => "oa:Annotation", + "motivation" => "sc:painting", + "resource" => + { + "@id" => "7377e5fe51c46454bb01b62a817a4d42", + "@type" => "cnt:ContentAsText", + "format" => "text/plain", + "chars" => "Erant aut[em] qui manducaverant", + "language" => "lat" + }, + "on" => "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,450,1017,166" + } + end + + def solr_data_all + { + "annotation" => annotation_001, + "manuscript" => "Manuscript fragment of the Gospels and Canonical Epistles, glossed", + "folio" => "f. 8r", + "url" => "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json" + } + end + def solr_data_no_id + { + "annotation" => annotation_001.except('@id'), + "manuscript" => "Manuscript fragment of the Gospels and Canonical Epistles, glossed", + "folio" => "f. 8r", + "url" => "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json" + } + end + + def solr_data_no_anno + { + "manuscript" => "Manuscript fragment of the Gospels and Canonical Epistles, glossed", + "folio" => "f. 8r", + "url" => "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json" + } + end end diff --git a/spec/models/concerns/annotation_data_spec.rb b/spec/models/concerns/annotation_data_spec.rb index 1710b52..cdb15a7 100644 --- a/spec/models/concerns/annotation_data_spec.rb +++ b/spec/models/concerns/annotation_data_spec.rb @@ -6,48 +6,152 @@ class AnnotationDataTestClass describe AnnotationData do include AnnotationFixtures - #anno_str = File.read("fixtures/annotation_records/annotation-001.json") - let(:document) { SolrDocument.new(annotationjson: annotation_001) } - let(:anno1) { { - "@id" => "_:N43deaea09a5345379218db8cb72600c3", - "@type" => "oa:Annotation", - "motivation" => "sc:painting", - "resource" => - { - "@id" => "7377e5fe51c46454bb01b62a817a4d42", - "@type" => "cnt:ContentAsText", - "format" => "text/plain", - "chars" => "Erant aut[em] qui manducaverant", - "language" => "lat" - }, - "on" => "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,450,1017,166" - } } - - describe "#annotation" do - it "should be nil if no annotationjson" do - expect(SolrDocument.new().annotation).to be_nil + let(:document) { SolrDocument.new() } + let(:annotation_list) { document.read_annotation(annotation_url_001) } + let(:document_with_id) { SolrDocument.new(druid: "kq131cs7229", + collection: "Parker collection", + iiif_manifest: "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/manifest.json", + mods_url: "https://purl.stanford.edu/kq131cs7229.mods") } + + describe "#read_annotation" do + it "should be nil if no annotation url" do + expect(SolrDocument.new().read_annotation()).to be_nil end it "should be a Hash" do - expect(document.annotation).to be_a Hash + expect(annotation_list).to be_a Hash end it "should have the keys @context, @id, @type and resources" do - expect(document.annotation.keys).to eq(["@context", "@id", "@type", "resources"]) + expect(annotation_list.keys).to eq(["@context", "@id", "@type", "resources"]) end it "should have an array of resources" do - expect(document.annotation["resources"]).to be_a Array + expect(annotation_list["resources"]).to be_a Array end it "should have 19 annotations" do - expect(document.annotation["resources"].length).to eq 19 + expect(annotation_list["resources"].length).to eq 19 + end + + it "should have the first annotation equal annotation_001" do + expect(annotation_list["resources"].first).to eq(annotation_001) + end + end + + describe "#annotations" do + it "should be an Array" do + expect(document.annotations(annotation_list)).to be_a Array + expect(SolrDocument.new().annotations()).to be_a Array + end + + it "should be empty if no annotation url" do + expect(SolrDocument.new().annotations()).to be_empty + end + + it "should have no annotations" do + expect(document.annotations(annotation_list).length).to eq 0 + end + end + + describe "#transcriptions" do + it "should be an Array" do + expect(document.transcriptions(annotation_list)).to be_a Array + expect(SolrDocument.new().transcriptions()).to be_a Array end - it "should have the first annotation equal anno1" do - expect(document.annotation["resources"].first).to eq(anno1) + it "should be empty if no annotation url" do + expect(SolrDocument.new().transcriptions()).to be_empty end + it "should have 19 transcriptions" do + expect(document.transcriptions(annotation_list).length).to eq 19 + end + + it "should have the first transcription equal annotation_001" do + expect(document.transcriptions(annotation_list).first).to eq(annotation_001) + end + end + + describe "#map_annotation" do + it "should be an Hash" do + expect(document.map_annotation(annotation_001)).to be_a Hash + expect(SolrDocument.new().map_annotation()).to be_a Hash + end + + it "should be empty if no annotation url" do + expect(SolrDocument.new().map_annotation()).to be_empty + end + + it "should be empty if no annotation url" do + expect(document.map_annotation(annotation_001)).not_to be_empty + end + + it "should have the keys id, motivation, target_type, target_url, body_type, body_format, body_chars and body_language" do + expect(document.map_annotation(annotation_001).keys).to eq(['id', 'target_type', 'motivation', 'target_url', 'body_type', 'body_format', 'body_chars', 'body_language']) + end end + describe "#annotation_to_solr" do + let(:solr_doc_all) { document_with_id.annotation_to_solr(solr_data_all) } + let(:solr_doc_no_id) { document.annotation_to_solr(solr_data_no_id) } + let(:solr_doc_no_anno) { document.annotation_to_solr(solr_data_no_anno) } + + it "should be a empty hash if no annotation url" do + expect(SolrDocument.new().annotation_to_solr).to eq({}) + end + + it "should return a Hash" do + expect(solr_doc_all).to be_a Hash + expect(solr_doc_no_id).to be_a Hash + expect(solr_doc_no_anno).to be_a Hash + end + + it "should be a empty hash if no annotation data" do + expect(solr_doc_no_anno).to eq({}) + end + + it "should be a empty hash if no id" do + expect(solr_doc_no_id).to eq({}) + end + + it "should not be a empty hash with id" do + expect(solr_doc_all).to_not eq({}) + end + + it "should have an id" do + expect(solr_doc_all).to have_key("id") + expect(solr_doc_all["id"]).to eq("_:N43deaea09a5345379218db8cb72600c3") + end + + it "should have an druid" do + expect(solr_doc_all).to have_key("druid") + expect(solr_doc_all["druid"]).to eq("kq131cs7229") + end + + it "should have a body chars" do + expect(solr_doc_all).to have_key("body_chars_search") + expect(solr_doc_all["body_chars_search"]).to eq("Erant aut[em] qui manducaverant") + end + + it "should belong to the parker collection" do + expect(solr_doc_all).to have_key("collection") + expect(solr_doc_all["collection"]).to eq("Parker collection") + end + + it "should have manifest url" do + expect(solr_doc_all).to have_key("manifest_urls") + expect(solr_doc_all["manifest_urls"]).to eq("http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/manifest.json") + end + + it "should have url" do + expect(solr_doc_all).to have_key("url_sfx") + expect(solr_doc_all["url_sfx"]).to eq("http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json") + end + + it "should have folio" do + expect(solr_doc_all).to have_key("folio") + expect(solr_doc_all["folio"]).to eq("f. 8r") + end + end end diff --git a/spec/models/concerns/json_reader_spec.rb b/spec/models/concerns/json_reader_spec.rb index adff781..b89ae7e 100644 --- a/spec/models/concerns/json_reader_spec.rb +++ b/spec/models/concerns/json_reader_spec.rb @@ -7,7 +7,7 @@ class JsonReaderTestClass describe JsonReader::Reader do include AnnotationFixtures before(:all) do - @example_str = annotation_001 + @example_str = annotation_list_001 @example_url = 'http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json' @fixture_dir = Rails.root.join "spec/fixtures" @fixture_annotation_file = File.join(@fixture_dir, 'annotation_records/annotation-001.json') From 22c940a8607aed5dd67e15106c5980069ebf49f9 Mon Sep 17 00:00:00 2001 From: Anusha Ranganathan Date: Sat, 28 Nov 2015 14:20:13 +0000 Subject: [PATCH 6/8] Added model to solr data and methods to return the motivation for transcription and annotation --- app/models/concerns/annotation_data.rb | 17 +++++++--- spec/models/concerns/annotation_data_spec.rb | 33 ++++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/annotation_data.rb b/app/models/concerns/annotation_data.rb index 61af0f4..cd69ef2 100644 --- a/app/models/concerns/annotation_data.rb +++ b/app/models/concerns/annotation_data.rb @@ -10,6 +10,14 @@ def read_annotation(url=nil) #self[:annotation_list] = JsonReader::Reader.new.from_str(self[:annotation_url]) end + def motivation_for_annotations + "oa:commenting" + end + + def motivation_for_transcriptions + "sc:painting" + end + def annotations(annotation_list=nil) # the motivation for annotations will be: "oa:commenting" #return [] unless self[:annotation_url] @@ -17,7 +25,7 @@ def annotations(annotation_list=nil) #return [] unless self[:annotation_list] #return self[:annotation_list][:resources].select {|anno| anno["motivation"] == "oa:commenting" } return [] unless annotation_list - return annotation_list["resources"].select {|anno| anno["motivation"] == "oa:commenting" } + return annotation_list["resources"].select {|anno| anno["motivation"] == self.motivation_for_annotations } end def transcriptions(annotation_list=nil) @@ -27,7 +35,7 @@ def transcriptions(annotation_list=nil) #return [] unless self[:annotation_list] #return self[:annotation_list][:resources].select {|anno| anno["motivation"] == "sc:painting" } return [] unless annotation_list - return annotation_list["resources"].select {|anno| anno["motivation"] == "sc:painting" } + return annotation_list["resources"].select {|anno| anno["motivation"] == self.motivation_for_transcriptions } end def map_annotation(annotation=nil) @@ -43,6 +51,8 @@ def map_annotation(annotation=nil) anno['body_chars'] = annotation["resource"]["chars"] #TODO: Get the language word from code anno['body_language'] = annotation["resource"]["language"] + anno['model'] = "Transcription" if annotation["motivation"] == self.motivation_for_transcriptions + anno['model'] = "Annotation" if annotation["motivation"] == self.motivation_for_annotations anno end @@ -59,7 +69,7 @@ def annotation_to_solr(data={}) solr_doc["collection"] = self["collection"] solr_doc["folio"] = data["folio"] solr_doc["manuscript_search"] = data["manuscript"] - solr_doc["model"] = anno["motivation"] + solr_doc["model"] = anno["model"] solr_doc["motivation"] = anno["motivation"] solr_doc["target_url"] = anno["target_url"] solr_doc["target_type"] = anno["target_type"] @@ -69,5 +79,4 @@ def annotation_to_solr(data={}) solr_doc["language"] = anno["body_language"] solr_doc end - end diff --git a/spec/models/concerns/annotation_data_spec.rb b/spec/models/concerns/annotation_data_spec.rb index cdb15a7..3f79a2a 100644 --- a/spec/models/concerns/annotation_data_spec.rb +++ b/spec/models/concerns/annotation_data_spec.rb @@ -39,6 +39,30 @@ class AnnotationDataTestClass end end + describe "#motivation_for_annotations" do + it "should be a String" do + expect(document.motivation_for_annotations).to be_a String + expect(SolrDocument.new().motivation_for_annotations).to be_a String + end + + it "should be equal to oa:commenting" do + expect(document.motivation_for_annotations).to eq "oa:commenting" + expect(SolrDocument.new().motivation_for_annotations).to eq "oa:commenting" + end + end + + describe "#motivation_for_transcriptions" do + it "should be a String" do + expect(document.motivation_for_transcriptions).to be_a String + expect(SolrDocument.new().motivation_for_transcriptions).to be_a String + end + + it "should be equal to sc:painting" do + expect(document.motivation_for_transcriptions).to eq "sc:painting" + expect(SolrDocument.new().motivation_for_transcriptions).to eq "sc:painting" + end + end + describe "#annotations" do it "should be an Array" do expect(document.annotations(annotation_list)).to be_a Array @@ -87,8 +111,8 @@ class AnnotationDataTestClass expect(document.map_annotation(annotation_001)).not_to be_empty end - it "should have the keys id, motivation, target_type, target_url, body_type, body_format, body_chars and body_language" do - expect(document.map_annotation(annotation_001).keys).to eq(['id', 'target_type', 'motivation', 'target_url', 'body_type', 'body_format', 'body_chars', 'body_language']) + it "should have the keys id, motivation, target_type, target_url, body_type, body_format, body_chars and body_language, model" do + expect(document.map_annotation(annotation_001).keys).to eq(['id', 'target_type', 'motivation', 'target_url', 'body_type', 'body_format', 'body_chars', 'body_language', 'model']) end end @@ -153,5 +177,10 @@ class AnnotationDataTestClass expect(solr_doc_all).to have_key("folio") expect(solr_doc_all["folio"]).to eq("f. 8r") end + + it "should have model Transcription" do + expect(solr_doc_all).to have_key("model") + expect(solr_doc_all["model"]).to eq("Transcription") + end end end From dd249c5e455e518e2f2ca7544b0c427711001b1c Mon Sep 17 00:00:00 2001 From: Anusha Ranganathan Date: Sat, 28 Nov 2015 18:00:00 +0000 Subject: [PATCH 7/8] Rescue from http errors --- app/models/concerns/json_reader.rb | 7 ++++++- spec/models/concerns/json_reader_spec.rb | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/json_reader.rb b/app/models/concerns/json_reader.rb index 001eb42..ec2f435 100644 --- a/app/models/concerns/json_reader.rb +++ b/app/models/concerns/json_reader.rb @@ -6,7 +6,12 @@ def from_str(str) def from_url(url, encoding = nil) require 'open-uri' - JSON.parse(open(url).read) + begin + JSON.parse(open(url).read) + rescue OpenURI::HTTPError => the_error + puts "\nOpen URI error for #{url}\n\t#{the_error.message}" #TODO: Add to log + return nil + end end # Read in the contents of a JSON record from a file. diff --git a/spec/models/concerns/json_reader_spec.rb b/spec/models/concerns/json_reader_spec.rb index b89ae7e..b273fc7 100644 --- a/spec/models/concerns/json_reader_spec.rb +++ b/spec/models/concerns/json_reader_spec.rb @@ -9,10 +9,12 @@ class JsonReaderTestClass before(:all) do @example_str = annotation_list_001 @example_url = 'http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json' + @example_bad_url = 'http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text.json' @fixture_dir = Rails.root.join "spec/fixtures" @fixture_annotation_file = File.join(@fixture_dir, 'annotation_records/annotation-001.json') @doc_from_str = JsonReader::Reader.new.from_str(@example_str) @doc_from_url = JsonReader::Reader.new.from_url(@example_url) + @doc_from_bad_url = JsonReader::Reader.new.from_url(@example_bad_url) @doc_from_file = JsonReader::Reader.new.from_file(@fixture_annotation_file) end @@ -29,7 +31,10 @@ class JsonReaderTestClass end context "from_url" do - it "from_url should turn the contents at the url into a Hash" do + it "should return nil if url is bad" do + expect(@doc_from_bad_url).to be_nil + end + it "should turn the contents at the url into a Hash" do expect(@doc_from_url).to be_a Hash end it "should have the keys @context, @id, @type and resources" do From 8b4bcb448d8af7d0019bba89f44792b9c63ec2e9 Mon Sep 17 00:00:00 2001 From: Anusha Ranganathan Date: Sat, 28 Nov 2015 18:26:16 +0000 Subject: [PATCH 8/8] ISO 639 code for language replaced with label when ammping annotations data --- app/models/concerns/annotation_data.rb | 7 +++++-- .../annotation_records/annotation_fixtures.rb | 17 +++++++++++++++++ spec/models/concerns/annotation_data_spec.rb | 8 ++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/annotation_data.rb b/app/models/concerns/annotation_data.rb index cd69ef2..87852be 100644 --- a/app/models/concerns/annotation_data.rb +++ b/app/models/concerns/annotation_data.rb @@ -49,8 +49,11 @@ def map_annotation(annotation=nil) #TODO Convert format to human form? anno['body_format'] = annotation["resource"]["format"] anno['body_chars'] = annotation["resource"]["chars"] - #TODO: Get the language word from code - anno['body_language'] = annotation["resource"]["language"] + if SEARCHWORKS_LANGUAGES.has_key?(annotation["resource"]["language"]) + anno['body_language'] = SEARCHWORKS_LANGUAGES[annotation["resource"]["language"]] + else + anno['body_language'] = annotation["resource"]["language"] + end anno['model'] = "Transcription" if annotation["motivation"] == self.motivation_for_transcriptions anno['model'] = "Annotation" if annotation["motivation"] == self.motivation_for_annotations anno diff --git a/spec/fixtures/annotation_records/annotation_fixtures.rb b/spec/fixtures/annotation_records/annotation_fixtures.rb index a0edce9..d2f1946 100644 --- a/spec/fixtures/annotation_records/annotation_fixtures.rb +++ b/spec/fixtures/annotation_records/annotation_fixtures.rb @@ -280,6 +280,23 @@ def annotation_001 } end + def annotation_002 + { + "@id" => "_:N43deaea09a5345379218db8cb72600c3", + "@type" => "oa:Annotation", + "motivation" => "sc:painting", + "resource" => + { + "@id" => "7377e5fe51c46454bb01b62a817a4d42", + "@type" => "cnt:ContentAsText", + "format" => "text/plain", + "chars" => "Erant aut[em] qui manducaverant", + "language" => "fle" + }, + "on" => "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,450,1017,166" + } + end + def solr_data_all { "annotation" => annotation_001, diff --git a/spec/models/concerns/annotation_data_spec.rb b/spec/models/concerns/annotation_data_spec.rb index 3f79a2a..c1e016b 100644 --- a/spec/models/concerns/annotation_data_spec.rb +++ b/spec/models/concerns/annotation_data_spec.rb @@ -114,6 +114,14 @@ class AnnotationDataTestClass it "should have the keys id, motivation, target_type, target_url, body_type, body_format, body_chars and body_language, model" do expect(document.map_annotation(annotation_001).keys).to eq(['id', 'target_type', 'motivation', 'target_url', 'body_type', 'body_format', 'body_chars', 'body_language', 'model']) end + + it "should be human language and not iso code" do + expect(document.map_annotation(annotation_001)['body_language']).to eq 'Latin' + end + + it "should be iso code for language if not in list" do + expect(document.map_annotation(annotation_002)['body_language']).to eq 'fle' + end end describe "#annotation_to_solr" do