Class: ODRL::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/odrl/base.rb

Direct Known Subclasses

Action, Asset, Constraint, Party, Policy, Rule

Constant Summary collapse

@@repository =
RDF::Repository.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uid:, type:, title: nil, creator: nil, description: nil, issued: nil, subject: nil, baseURI: "http://example.org", id: nil, label: nil, **_) ⇒ Base

Returns a new instance of Base.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/odrl/base.rb', line 115

def initialize(
  uid:, type:, title: nil,
  creator: nil,
  description: nil,
  issued: nil,
  subject: nil,
  baseURI: "http://example.org",
  id: nil,
  label: nil,
  **_
)

  @title = title
  @creator = creator
  @issued = issued
  @description = description
  @subject = subject
  @baseURI = baseURI || ODRL::Base.baseURI
  @uid = uid
  @type = type
  @label = label || @title
  @id = @uid

  raise "Every object must have a uid - attempt to create #{@type}" unless @uid
  raise "Every object must have a type - " unless @type

  $g = RDF::Graph.new
  $format = if ENV["TRIPLES_FORMAT"]
              ENV["TRIPLES_FORMAT"].to_sym
            else
              :jsonld
            end
end

Instance Attribute Details

#baseURIObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def baseURI
  @baseURI
end

#creatorObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def creator
  @creator
end

#descriptionObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def description
  @description
end

#idObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def id
  @id
end

#issuedObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def issued
  @issued
end

#labelObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def label
  @label
end

#subjectObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def subject
  @subject
end

#titleObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def title
  @title
end

#typeObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def type
  @type
end

#uidObject

If you add an attribute, you mustr also add it to the constructor, and to the @attribute list andn to the .load_graph



96
97
98
# File 'lib/odrl/base.rb', line 96

def uid
  @uid
end

Class Method Details

.baseURIObject



98
99
100
# File 'lib/odrl/base.rb', line 98

def self.baseURI
  ENV["ODRL_BASEURI"] || "http://example.org"
end

.clear_repositoryObject



110
111
112
113
# File 'lib/odrl/base.rb', line 110

def self.clear_repository
  @@repository.clear!
  true
end

.getuuidObject



205
206
207
# File 'lib/odrl/base.rb', line 205

def self.getuuid
  Time.now.to_f.to_s.gsub(".", "")[1..14]
end

.repositoryObject



102
103
104
# File 'lib/odrl/base.rb', line 102

def self.repository
  @@repository
end

Instance Method Details

#get_writer(type:) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/odrl/base.rb', line 149

def get_writer(type:)
  RDF::Writer.for(type)
  # w.prefix(:foaf, RDF::URI.new("http://xmlns.com/foaf/0.1/"))
  # w.prefix(:dc, RDF::URI.new("http://purl.org/dc/terms/"))
  # w.prefix(:rdf, RDF::URI.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
  # w.prefix(:rdfs, RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#"))
  # w.prefix(:vcard, RDF::URI.new("http://www.w3.org/2006/vcard/ns#"))
  # w.prefix(:odrl, RDF::URI.new("http://www.w3.org/ns/odrl/2/"))
  # w.prefix(:this, RDF::URI.new("http://w3id.org/FAIR_Training_LDP/DAV/home/LDP/DUC-CCE/IPGB#"))
  # w.prefix(:obo, RDF::URI.new("http://purl.obolibrary.org/obo/"))
  # w.prefix(:xsd, RDF::URI.new("http://www.w3.org/2001/XMLSchema#"))
  # w.prefix(:orcid, RDF::URI.new("https://orcid.org/"))
  # warn "W"
  # warn w.prefixes.inspect
end

#load_graphObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/odrl/base.rb', line 209

def load_graph
  %i[title label issued creator description subject uid id type].each do |method|
    next unless send(method)
    next if send(method).empty?

    subject = uid # me!
    predicate = PROPERTIES[method] # look up the predicate for this property
    # warn "prediate #{predicate} for method #{method}"
    object = send(method) # get the value of this property from self
    # warn "value #{object.to_s}"
    repo = repository
    triplify(subject, predicate, object, repo)
  end
end

#repositoryObject



106
107
108
# File 'lib/odrl/base.rb', line 106

def repository
  @@repository
end

#serialize(format: $format) ⇒ Object



224
225
226
227
228
# File 'lib/odrl/base.rb', line 224

def serialize(format: $format)
  format = format.to_sym
  w = get_writer(type: format)
  w.dump(repository)
end

#triplify(s, p, o, repo) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/odrl/base.rb', line 165

def triplify(s, p, o, repo)
  s = s.strip if s.instance_of?(String)
  p = p.strip if p.instance_of?(String)
  o = o.strip if o.instance_of?(String)

  unless s.respond_to?("uri")

    raise "Subject #{s} must be a URI-compatible thingy #{s}, #{p}, #{o}" unless s.to_s =~ %r{^\w+:/?/?[^\s]+}

    s = RDF::URI.new(s.to_s)

  end

  unless p.respond_to?("uri")

    raise "Predicate #{p} must be a URI-compatible thingy #{s}, #{p}, #{o}" unless p.to_s =~ %r{^\w+:/?/?[^\s]+}

    p = RDF::URI.new(p.to_s)

  end
  unless o.respond_to?("uri")
    o = if o.to_s =~ %r{^\w+:/?/?[^\s]+}
          RDF::URI.new(o.to_s)
        elsif o.to_s =~ /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d/
          RDF::Literal.new(o.to_s, datatype: RDF::XSD.date)
        elsif o.to_s =~ /^\d\.\d/
          RDF::Literal.new(o.to_s, datatype: RDF::XSD.float)
        elsif o.to_s =~ /^[0-9]+$/
          RDF::Literal.new(o.to_s, datatype: RDF::XSD.int)
        else
          RDF::Literal.new(o.to_s, language: :en)
        end
  end

  triple = RDF::Statement(s, p, o)
  repo.insert(triple)

  true
end