Class: RdfSyncService

Inherits:
Object
  • Object
show all
Defined in:
app/services/rdf_sync_service.rb

Defined Under Namespace

Modules: Helper

Constant Summary collapse

ADAPTORS =

XXX: inappropriate?

{ # XXX: inappropriate?
  'virtuoso' => lambda do  |host_url, options|
    require 'iq_triplestorage/virtuoso_adaptor'
    return IqTriplestorage::VirtuosoAdaptor.new(host_url, options)
  end,
  'sesame' => lambda do |host_url, options|
    require 'iq_triplestorage/sesame_adaptor'
    host_url, _, repo = host_url.rpartition('/repositories/')
    if host_url.blank? || repo.blank?
     raise ArgumentError, 'missing repository in Sesame URL'
    end
    options[:repository] = repo
    return IqTriplestorage::SesameAdaptor.new(host_url, options)
  end
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, target_url, options) ⇒ RdfSyncService

Returns a new instance of RdfSyncService.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
# File 'app/services/rdf_sync_service.rb', line 22

def initialize(base_url, target_url, options)
  @base_url = base_url
  @target_url = target_url
  @username = options[:username]
  @password = options[:password]
  @batch_size = options[:batch_size] || 100
  @view_context = options[:view_context] # XXX: not actually optional
  raise(ArgumentError, 'missing view context') unless @view_context # XXX: smell (see above)
end

Class Method Details

.candidates(klass = nil) ⇒ Object

returns one or multiple ActiveRecord::RelationS, depending on whether ‘klass` is provided



102
103
104
105
# File 'app/services/rdf_sync_service.rb', line 102

def self.candidates(klass=nil)
  return klass ? klass.published.unsynced :
      Iqvoc::Sync.syncable_classes.map { |klass| candidates(klass) }
end

Instance Method Details

#allObject

TODO: rename



32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/rdf_sync_service.rb', line 32

def all # TODO: rename
  timestamp = Time.now
  errors = false

  gather_candidates do |records|
     success = sync(records, timestamp)
     errors = true unless success
  end

  return !errors
end

#gather_candidatesObject

yields batches of candidates for synchronization



92
93
94
95
96
97
98
# File 'app/services/rdf_sync_service.rb', line 92

def gather_candidates # TODO: rename
  Iqvoc::Sync.syncable_classes.each do |klass|
    self.class.candidates(klass).find_in_batches(batch_size: @batch_size) do |records|
      yield records
    end
  end
end

#push(records) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/rdf_sync_service.rb', line 57

def push(records)
  data = records.inject({}) do |memo, record|
    graph_uri = url_helpers.rdf_url(record.origin,
        host: URI.parse(@base_url).host, format: nil, lang: nil)
    memo[graph_uri] = serialize(record)
    memo
  end

  adaptor_type = 'sesame' # XXX: hard-coded
  adaptor = ADAPTORS[adaptor_type].call(@target_url, username: @username,
      password: @password)
  return adaptor.batch_update(data)
end

#serialize(record) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/rdf_sync_service.rb', line 71

def serialize(record)
  # while this method is really fugly, iQvoc essentially requires us to mock a
  # view in order to get to the RDF serialization

  doc = IqRdf::Document.new(@base_url)
  RdfNamespacesHelper.instance_methods.each do |meth|
    namespaces = @view_context.send(meth)
    doc.namespaces(namespaces) if namespaces.is_a?(Hash)
  end

  rdf_helper = Object.new.extend(RdfHelper)
  if record.is_a? Iqvoc::Concept.base_class
    rdf_helper.render_concept(doc, record)
  else # XXX: must be extensible
    raise NotImplementedError, "unable to render RDF for #{record.class}"
  end

  return doc.to_ntriples
end

#sync(records, timestamp = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/rdf_sync_service.rb', line 44

def sync(records, timestamp=nil)
  timestamp ||= Time.now

  success = push(records)
  if success
    records.each do |record|
      record.update_attribute(:rdf_updated_at, timestamp)
    end
  end

  return success
end