Class: Annotations2triannon::OpenAnnotation
- Inherits:
-
Object
- Object
- Annotations2triannon::OpenAnnotation
- Defined in:
- lib/annotations2triannon/open_annotation.rb
Overview
class OpenAnnotation < Resource
Constant Summary collapse
- OA =
RDF::Vocab::OA
- OA_CONTEXT =
'http://www.w3.org/ns/oa.jsonld'- IIIF_CONTEXT =
'http://iiif.io/api/presentation/2/context.json'
Instance Attribute Summary collapse
-
#graph ⇒ Object
an RDF::Graph.
-
#id ⇒ Object
Returns the value of attribute id.
Instance Method Summary collapse
-
#annotatedAt ⇒ Array<String>|nil
The datetime from the annotatedAt object(s).
-
#annotatedBy ⇒ Array<String>|nil
The identity for the annotatedBy object(s).
-
#annotatedBy?(uri = nil) ⇒ boolean
True if the open annotation has any annotatedBy ‘uri’.
-
#as_jsonld ⇒ Object
A json-ld representation of the open annotation.
- #body_contentAsText ⇒ Object
- #body_contentAsText? ⇒ Boolean
-
#body_contentChars ⇒ Array<String>
For all bodies that are of type ContentAsText, get the characters as a single String in the returned Array.
- #body_semanticTag ⇒ Object
- #body_semanticTag? ⇒ Boolean
- #body_type(uri = nil) ⇒ Object
- #get_id ⇒ Object
-
#hasBody ⇒ Array
The hasBody object(s).
- #hasBody? ⇒ Boolean
-
#hasTarget ⇒ Array
The hasTarget object(s).
- #hasTarget? ⇒ Boolean
-
#initialize(graph = RDF::Graph.new, id = nil) ⇒ OpenAnnotation
constructor
instantiate this class.
- #insert_annotatedAt(datetime = rdf_now) ⇒ Object
- #insert_annotatedBy(annotator = nil) ⇒ Object
- #insert_annotation ⇒ Object
- #insert_hasBody(body) ⇒ Object
- #insert_hasTarget(target) ⇒ Object
-
#insert_motivatedBy(motivation) ⇒ Object
Insert an ?o for [id, OA.motivatedBy, ?o] where ?o is ‘motivation’.
-
#insert_motivatedByCommenting ⇒ Object
Insert [id, OA.motivatedBy, OA.commenting].
-
#insert_motivatedByTagging ⇒ Object
Insert [id, OA.motivatedBy, OA.tagging].
-
#is_annotation? ⇒ boolean
True if RDF.type is OA.Annotation.
-
#motivatedBy(uri = nil) ⇒ Array
Find any matching ?o for ?s OA.motivatedBy ?o where ?o is ‘uri’.
-
#motivatedBy?(uri = nil) ⇒ boolean
Are there any matching ?o for [?s, OA.motivatedBy, ?o] where ?o is ‘uri’.
-
#motivatedByCommenting ⇒ Object
Find all the matching ?s for [?s, OA.motivatedBy, OA.commenting].
-
#motivatedByCommenting? ⇒ Boolean
Are there any matching ?s for [?s, OA.motivatedBy, OA.commenting].
-
#motivatedByTagging ⇒ Object
Find all the matching ?s for [?s, OA.motivatedBy, OA.tagging].
-
#motivatedByTagging? ⇒ Boolean
Are there any matching ?s for [?s, OA.motivatedBy, OA.tagging].
-
#open_annotation? ⇒ boolean
True if RDF.type is OA.Annotation, with OA.hasBody and OA.hasTarget.
- #provenance ⇒ Object
- #rdf_now ⇒ Object
-
#to_jsonld(context = nil) ⇒ Object
Json-ld representation of graph with default context.
-
#to_jsonld_iiif ⇒ Object
Json-ld representation of graph with IIIF context.
-
#to_jsonld_oa ⇒ Object
Json-ld representation of graph with OpenAnnotation context.
-
#to_ttl ⇒ Object
A turtle string representation of the open annotation.
Constructor Details
#initialize(graph = RDF::Graph.new, id = nil) ⇒ OpenAnnotation
instantiate this class
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/annotations2triannon/open_annotation.rb', line 18 def initialize(graph=RDF::Graph.new, id=nil) @@agent ||= Annotations2triannon::AGENT raise TypeError, 'graph must be RDF::Graph instance' unless graph.instance_of? RDF::Graph if graph.empty? # create a new open annotation @graph = graph id.nil? ? @id = get_id : @id = RDF::URI.parse(id) insert_annotation else @graph = graph raise TypeError, 'graph must be an open annotation' unless is_annotation? if id.nil? @id = get_id else end end end |
Instance Attribute Details
#graph ⇒ Object
an RDF::Graph
13 14 15 |
# File 'lib/annotations2triannon/open_annotation.rb', line 13 def graph @graph end |
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/annotations2triannon/open_annotation.rb', line 12 def id @id end |
Instance Method Details
#annotatedAt ⇒ Array<String>|nil
Returns The datetime from the annotatedAt object(s).
207 208 209 210 |
# File 'lib/annotations2triannon/open_annotation.rb', line 207 def annotatedAt q = [nil, OA.annotatedAt, nil] @graph.query(q).collect {|s| s.object } end |
#annotatedBy ⇒ Array<String>|nil
Returns The identity for the annotatedBy object(s).
189 190 191 192 |
# File 'lib/annotations2triannon/open_annotation.rb', line 189 def annotatedBy q = [:s, OA.annotatedBy, :o] @graph.query(q).collect {|s| s.object } end |
#annotatedBy?(uri = nil) ⇒ boolean
Returns True if the open annotation has any annotatedBy ‘uri’.
196 197 198 199 200 |
# File 'lib/annotations2triannon/open_annotation.rb', line 196 def annotatedBy?(uri=nil) uri = RDF::URI.parse(uri) unless uri.nil? q = [nil, OA.annotatedBy, uri] @graph.query(q).size > 0 end |
#as_jsonld ⇒ Object
A json-ld representation of the open annotation
227 228 229 230 |
# File 'lib/annotations2triannon/open_annotation.rb', line 227 def as_jsonld provenance JSON::LD::API::fromRDF(@graph) end |
#body_contentAsText ⇒ Object
94 95 96 |
# File 'lib/annotations2triannon/open_annotation.rb', line 94 def body_contentAsText body_type RDF::CONTENT.ContentAsText end |
#body_contentAsText? ⇒ Boolean
98 99 100 |
# File 'lib/annotations2triannon/open_annotation.rb', line 98 def body_contentAsText? body_contentAsText.size > 0 end |
#body_contentChars ⇒ Array<String>
For all bodies that are of type ContentAsText, get the characters as a single String in the returned Array.
104 105 106 107 108 109 110 |
# File 'lib/annotations2triannon/open_annotation.rb', line 104 def body_contentChars q = RDF::Query.new q << [nil, OA.hasBody, :body] q << [:body, RDF.type, RDF::CONTENT.ContentAsText] q << [:body, RDF::CONTENT.chars, :body_chars] @graph.query(q).collect {|s| s.body_chars.value } end |
#body_semanticTag ⇒ Object
112 113 114 |
# File 'lib/annotations2triannon/open_annotation.rb', line 112 def body_semanticTag body_type OA.SemanticTag end |
#body_semanticTag? ⇒ Boolean
116 117 118 |
# File 'lib/annotations2triannon/open_annotation.rb', line 116 def body_semanticTag? body_semanticTag.size > 0 end |
#body_type(uri = nil) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/annotations2triannon/open_annotation.rb', line 120 def body_type(uri=nil) uri = RDF::URI.parse(uri) unless uri.nil? q = RDF::Query.new q << [nil, OA.hasBody, :body] q << [:body, RDF.type, uri] @graph.query(q) end |
#get_id ⇒ Object
36 37 38 39 40 |
# File 'lib/annotations2triannon/open_annotation.rb', line 36 def get_id return @id unless @id.nil? q = [nil, RDF.type, OA.Annotation] @id = @graph.query(q).collect {|s| s.subject }.first || RDF::URI.parse(UUID.generate) end |
#hasBody ⇒ Array
Returns The hasBody object(s).
85 86 87 88 |
# File 'lib/annotations2triannon/open_annotation.rb', line 85 def hasBody q = [nil, OA.hasBody, nil] @graph.query(q).collect {|s| s.object } end |
#hasBody? ⇒ Boolean
90 91 92 |
# File 'lib/annotations2triannon/open_annotation.rb', line 90 def hasBody? hasBody.length > 0 end |
#hasTarget ⇒ Array
Returns The hasTarget object(s).
70 71 72 73 |
# File 'lib/annotations2triannon/open_annotation.rb', line 70 def hasTarget q = [nil, OA.hasTarget, nil] @graph.query(q).collect {|s| s.object } end |
#hasTarget? ⇒ Boolean
75 76 77 |
# File 'lib/annotations2triannon/open_annotation.rb', line 75 def hasTarget? hasTarget.length > 0 end |
#insert_annotatedAt(datetime = rdf_now) ⇒ Object
202 203 204 |
# File 'lib/annotations2triannon/open_annotation.rb', line 202 def insert_annotatedAt(datetime=rdf_now) @graph.insert([@id, OA.annotatedAt, datetime]) end |
#insert_annotatedBy(annotator = nil) ⇒ Object
184 185 186 |
# File 'lib/annotations2triannon/open_annotation.rb', line 184 def insert_annotatedBy(annotator=nil) @graph.insert([@id, OA.annotatedBy, annotator]) end |
#insert_annotation ⇒ Object
52 53 54 55 56 |
# File 'lib/annotations2triannon/open_annotation.rb', line 52 def insert_annotation s = [@id, RDF.type, OA.Annotation] @graph.delete(s) @graph.insert(s) end |
#insert_hasBody(body) ⇒ Object
79 80 81 82 |
# File 'lib/annotations2triannon/open_annotation.rb', line 79 def insert_hasBody(body) # TODO: raise ValueError when body is outside hasBody range? @graph.insert([@id, OA.hasBody, body]) end |
#insert_hasTarget(target) ⇒ Object
64 65 66 67 |
# File 'lib/annotations2triannon/open_annotation.rb', line 64 def insert_hasTarget(target) # TODO: raise ValueError when target is outside hasTarget range? @graph.insert([@id, OA.hasTarget, target]) end |
#insert_motivatedBy(motivation) ⇒ Object
Insert an ?o for [id, OA.motivatedBy, ?o] where ?o is ‘motivation’
130 131 132 133 134 |
# File 'lib/annotations2triannon/open_annotation.rb', line 130 def insert_motivatedBy(motivation) # TODO: only accept values allowed by OA.motivationBy range? motivation = RDF::URI.parse(motivation) @graph.insert([@id, OA.motivatedBy, motivation]) end |
#insert_motivatedByCommenting ⇒ Object
Insert [id, OA.motivatedBy, OA.commenting]
153 154 155 |
# File 'lib/annotations2triannon/open_annotation.rb', line 153 def insert_motivatedByCommenting insert_motivatedBy OA.commenting end |
#insert_motivatedByTagging ⇒ Object
Insert [id, OA.motivatedBy, OA.tagging]
169 170 171 |
# File 'lib/annotations2triannon/open_annotation.rb', line 169 def insert_motivatedByTagging insert_motivatedBy OA.tagging end |
#is_annotation? ⇒ boolean
Returns true if RDF.type is OA.Annotation.
59 60 61 62 |
# File 'lib/annotations2triannon/open_annotation.rb', line 59 def is_annotation? q = [@id, RDF.type, OA.Annotation] @graph.query(q).size > 0 end |
#motivatedBy(uri = nil) ⇒ Array
Find any matching ?o for ?s OA.motivatedBy ?o where ?o is ‘uri’
139 140 141 142 143 |
# File 'lib/annotations2triannon/open_annotation.rb', line 139 def motivatedBy(uri=nil) uri = RDF::URI.parse(uri) unless uri.nil? q = [nil, OA.motivatedBy, uri] @graph.query(q).collect {|s| s.object } end |
#motivatedBy?(uri = nil) ⇒ boolean
Are there any matching ?o for [?s, OA.motivatedBy, ?o] where ?o is ‘uri’
148 149 150 |
# File 'lib/annotations2triannon/open_annotation.rb', line 148 def motivatedBy?(uri=nil) motivatedBy(uri).length > 0 end |
#motivatedByCommenting ⇒ Object
Find all the matching ?s for [?s, OA.motivatedBy, OA.commenting]
158 159 160 161 |
# File 'lib/annotations2triannon/open_annotation.rb', line 158 def motivatedByCommenting q = [nil, OA.motivatedBy, OA.commenting] @graph.query(q).collect {|s| s.subject } end |
#motivatedByCommenting? ⇒ Boolean
Are there any matching ?s for [?s, OA.motivatedBy, OA.commenting]
164 165 166 |
# File 'lib/annotations2triannon/open_annotation.rb', line 164 def motivatedByCommenting? motivatedByCommenting.length > 0 end |
#motivatedByTagging ⇒ Object
Find all the matching ?s for [?s, OA.motivatedBy, OA.tagging]
174 175 176 177 |
# File 'lib/annotations2triannon/open_annotation.rb', line 174 def motivatedByTagging q = [nil, OA.motivatedBy, OA.tagging] @graph.query(q).collect {|s| s.subject } end |
#motivatedByTagging? ⇒ Boolean
Are there any matching ?s for [?s, OA.motivatedBy, OA.tagging]
180 181 182 |
# File 'lib/annotations2triannon/open_annotation.rb', line 180 def motivatedByTagging? motivatedByTagging.length > 0 end |
#open_annotation? ⇒ boolean
Returns true if RDF.type is OA.Annotation, with OA.hasBody and OA.hasTarget.
43 44 45 46 47 48 49 50 |
# File 'lib/annotations2triannon/open_annotation.rb', line 43 def open_annotation? # TODO: check rules for basic open annotation q = RDF::Query.new q << [@id, RDF.type, OA.Annotation] q << [@id, OA.hasBody, :b] q << [@id, OA.hasTarget, :t] @graph.query(q).size > 0 end |
#provenance ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'lib/annotations2triannon/open_annotation.rb', line 216 def provenance # http://www.openannotation.org/spec/core/core.html#Provenance # When adding the agent, ensure it's not there already, also # an open annotation cannot have more than one oa:serializedAt. @graph.delete([nil,nil,@@agent]) @graph.delete([nil, OA.serializedAt, nil]) @graph << [@id, OA.serializedAt, rdf_now] @graph << [@id, OA.serializedBy, @@agent] end |
#rdf_now ⇒ Object
212 213 214 |
# File 'lib/annotations2triannon/open_annotation.rb', line 212 def rdf_now RDF::Literal.new(Time.now.utc, :datatype => RDF::XSD.dateTime) end |
#to_jsonld(context = nil) ⇒ Object
Returns json-ld representation of graph with default context.
234 235 236 237 238 239 240 241 |
# File 'lib/annotations2triannon/open_annotation.rb', line 234 def to_jsonld(context=nil) provenance if context.nil? @graph.dump(:jsonld, standard_prefixes: true) else @graph.dump(:jsonld, standard_prefixes: true, context: context) end end |
#to_jsonld_iiif ⇒ Object
Returns json-ld representation of graph with IIIF context.
244 245 246 |
# File 'lib/annotations2triannon/open_annotation.rb', line 244 def to_jsonld_iiif to_jsonld IIIF_CONTEXT end |
#to_jsonld_oa ⇒ Object
Returns json-ld representation of graph with OpenAnnotation context.
249 250 251 |
# File 'lib/annotations2triannon/open_annotation.rb', line 249 def to_jsonld_oa to_jsonld OA_CONTEXT end |
#to_ttl ⇒ Object
A turtle string representation of the open annotation
254 255 256 257 |
# File 'lib/annotations2triannon/open_annotation.rb', line 254 def to_ttl provenance @graph.dump(:ttl, standard_prefixes: true) end |