Class: Annotations2triannon::OpenAnnotation
- Inherits:
-
Object
- Object
- Annotations2triannon::OpenAnnotation
- Defined in:
- lib/annotations2triannon/open_annotation.rb
Overview
class OpenAnnotation < Resource
Constant Summary collapse
- CONTENT =
RDF::Vocab::CNT
- 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_graph ⇒ Object
- #body_semanticTag ⇒ Object
- #body_semanticTag? ⇒ Boolean
- #body_type(uri = nil) ⇒ 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
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/annotations2triannon/open_annotation.rb', line 19 def initialize(graph=RDF::Graph.new, id=nil) @@agent ||= Annotations2triannon::AGENT set_graph(graph) # The set_graph will set @graph and also set @id, using the # graph subject with RDF.type of OA.Annotation. However, the # id parameter for this init can override this default behavior, # but it should be set after calling set_graph so it first has # a chance to extract an ID from the graph. Once the @id is set, # the set_graph method will not touch it again. @id = parse_id(id) end |
Instance Attribute Details
#graph ⇒ Object
an RDF::Graph
14 15 16 |
# File 'lib/annotations2triannon/open_annotation.rb', line 14 def graph @graph end |
#id ⇒ Object
Returns the value of attribute id.
13 14 15 |
# File 'lib/annotations2triannon/open_annotation.rb', line 13 def id @id end |
Instance Method Details
#annotatedAt ⇒ Array<String>|nil
Returns The datetime from the annotatedAt object(s).
210 211 212 213 |
# File 'lib/annotations2triannon/open_annotation.rb', line 210 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).
192 193 194 195 |
# File 'lib/annotations2triannon/open_annotation.rb', line 192 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’.
199 200 201 202 203 |
# File 'lib/annotations2triannon/open_annotation.rb', line 199 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
230 231 232 233 |
# File 'lib/annotations2triannon/open_annotation.rb', line 230 def as_jsonld provenance JSON::LD::API::fromRDF(@graph) end |
#body_contentAsText ⇒ Object
101 102 103 |
# File 'lib/annotations2triannon/open_annotation.rb', line 101 def body_contentAsText body_type CONTENT.ContentAsText end |
#body_contentAsText? ⇒ Boolean
105 106 107 |
# File 'lib/annotations2triannon/open_annotation.rb', line 105 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.
111 112 113 114 115 116 |
# File 'lib/annotations2triannon/open_annotation.rb', line 111 def body_contentChars q = RDF::Query.new q << [:body, RDF.type, CONTENT.ContentAsText] q << [:body, CONTENT.chars, :body_chars] body_graph.query(q).collect {|s| s.body_chars.value } end |
#body_graph ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/annotations2triannon/open_annotation.rb', line 93 def body_graph g = RDF::Graph.new hasBody.each do |b| @graph.query( [b, :p, :o] ).each_statement {|s| g << s} end g end |
#body_semanticTag ⇒ Object
118 119 120 |
# File 'lib/annotations2triannon/open_annotation.rb', line 118 def body_semanticTag body_type OA.SemanticTag end |
#body_semanticTag? ⇒ Boolean
122 123 124 |
# File 'lib/annotations2triannon/open_annotation.rb', line 122 def body_semanticTag? body_semanticTag.size > 0 end |
#body_type(uri = nil) ⇒ Object
126 127 128 129 |
# File 'lib/annotations2triannon/open_annotation.rb', line 126 def body_type(uri=nil) uri = RDF::URI.parse(uri) unless uri.nil? body_graph.query([:body, RDF.type, uri]) end |
#hasBody ⇒ Array
Returns The hasBody object(s).
84 85 86 87 |
# File 'lib/annotations2triannon/open_annotation.rb', line 84 def hasBody q = [nil, OA.hasBody, nil] @graph.query(q).collect {|s| s.object } end |
#hasBody? ⇒ Boolean
89 90 91 |
# File 'lib/annotations2triannon/open_annotation.rb', line 89 def hasBody? hasBody.length > 0 end |
#hasTarget ⇒ Array
Returns The hasTarget object(s).
69 70 71 72 |
# File 'lib/annotations2triannon/open_annotation.rb', line 69 def hasTarget q = [nil, OA.hasTarget, nil] @graph.query(q).collect {|s| s.object } end |
#hasTarget? ⇒ Boolean
74 75 76 |
# File 'lib/annotations2triannon/open_annotation.rb', line 74 def hasTarget? hasTarget.length > 0 end |
#insert_annotatedAt(datetime = rdf_now) ⇒ Object
205 206 207 |
# File 'lib/annotations2triannon/open_annotation.rb', line 205 def insert_annotatedAt(datetime=rdf_now) @graph.insert([@id, OA.annotatedAt, datetime]) end |
#insert_annotatedBy(annotator = nil) ⇒ Object
187 188 189 |
# File 'lib/annotations2triannon/open_annotation.rb', line 187 def insert_annotatedBy(annotator=nil) @graph.insert([@id, OA.annotatedBy, annotator]) end |
#insert_annotation ⇒ Object
51 52 53 54 55 |
# File 'lib/annotations2triannon/open_annotation.rb', line 51 def insert_annotation s = [@id, RDF.type, OA.Annotation] @graph.delete(s) @graph.insert(s) end |
#insert_hasBody(body) ⇒ Object
78 79 80 81 |
# File 'lib/annotations2triannon/open_annotation.rb', line 78 def insert_hasBody(body) # TODO: raise ValueError when body is outside hasBody range? @graph.insert([@id, OA.hasBody, body]) end |
#insert_hasTarget(target) ⇒ Object
63 64 65 66 |
# File 'lib/annotations2triannon/open_annotation.rb', line 63 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’
133 134 135 136 137 |
# File 'lib/annotations2triannon/open_annotation.rb', line 133 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]
156 157 158 |
# File 'lib/annotations2triannon/open_annotation.rb', line 156 def insert_motivatedByCommenting insert_motivatedBy OA.commenting end |
#insert_motivatedByTagging ⇒ Object
Insert [id, OA.motivatedBy, OA.tagging]
172 173 174 |
# File 'lib/annotations2triannon/open_annotation.rb', line 172 def insert_motivatedByTagging insert_motivatedBy OA.tagging end |
#is_annotation? ⇒ boolean
Returns true if RDF.type is OA.Annotation.
58 59 60 61 |
# File 'lib/annotations2triannon/open_annotation.rb', line 58 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’
142 143 144 145 146 |
# File 'lib/annotations2triannon/open_annotation.rb', line 142 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’
151 152 153 |
# File 'lib/annotations2triannon/open_annotation.rb', line 151 def motivatedBy?(uri=nil) motivatedBy(uri).length > 0 end |
#motivatedByCommenting ⇒ Object
Find all the matching ?s for [?s, OA.motivatedBy, OA.commenting]
161 162 163 164 |
# File 'lib/annotations2triannon/open_annotation.rb', line 161 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]
167 168 169 |
# File 'lib/annotations2triannon/open_annotation.rb', line 167 def motivatedByCommenting? motivatedByCommenting.length > 0 end |
#motivatedByTagging ⇒ Object
Find all the matching ?s for [?s, OA.motivatedBy, OA.tagging]
177 178 179 180 |
# File 'lib/annotations2triannon/open_annotation.rb', line 177 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]
183 184 185 |
# File 'lib/annotations2triannon/open_annotation.rb', line 183 def motivatedByTagging? motivatedByTagging.length > 0 end |
#open_annotation? ⇒ boolean
Returns true if RDF.type is OA.Annotation, with OA.hasBody and OA.hasTarget.
42 43 44 45 46 47 48 49 |
# File 'lib/annotations2triannon/open_annotation.rb', line 42 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
219 220 221 222 223 224 225 226 227 |
# File 'lib/annotations2triannon/open_annotation.rb', line 219 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
215 216 217 |
# File 'lib/annotations2triannon/open_annotation.rb', line 215 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.
237 238 239 240 241 242 243 244 |
# File 'lib/annotations2triannon/open_annotation.rb', line 237 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.
247 248 249 |
# File 'lib/annotations2triannon/open_annotation.rb', line 247 def to_jsonld_iiif to_jsonld IIIF_CONTEXT end |
#to_jsonld_oa ⇒ Object
Returns json-ld representation of graph with OpenAnnotation context.
252 253 254 |
# File 'lib/annotations2triannon/open_annotation.rb', line 252 def to_jsonld_oa to_jsonld OA_CONTEXT end |
#to_ttl ⇒ Object
A turtle string representation of the open annotation
257 258 259 260 |
# File 'lib/annotations2triannon/open_annotation.rb', line 257 def to_ttl provenance @graph.dump(:ttl, standard_prefixes: true) end |