/
owner_email_editor.rb
50 lines (40 loc) · 1.26 KB
/
owner_email_editor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'net/http'
class PushmiPullyu::AIP::OwnerEmailEditor
OWNER_PREDICATE = RDF::URI('http://purl.org/ontology/bibo/owner').freeze
class NoOwnerPredicate < StandardError; end
def initialize(rdf_string)
@document = rdf_string
end
def run
setup_db_connection
is_modified = false
prefixes = nil
# Read once to load prefixes (the @things at the top of an n3 file)
RDF::N3::Reader.new(input = @document) do |reader|
reader.each_statement { |_statement| }
prefixes = reader.prefixes
end
new_body = RDF::N3::Writer.buffer(prefixes: prefixes) do |writer|
RDF::N3::Reader.new(input = @document) do |reader|
reader.each_statement do |statement|
if statement.predicate == OWNER_PREDICATE
user = PushmiPullyu::AIP::User.find(statement.object.to_i)
writer << [statement.subject, statement.predicate, user.email]
is_modified = true
else
writer << statement
end
end
end
end
return new_body if is_modified
raise NoOwnerPredicate
end
private
def setup_db_connection
ActiveRecord::Base.establish_connection(database_configuration)
end
def database_configuration
PushmiPullyu.options[:database][:url]
end
end