Class: Iqvoc::RDFSync
- Inherits:
-
Object
- Object
- Iqvoc::RDFSync
- Defined in:
- lib/iqvoc/rdf_sync.rb
Defined Under Namespace
Modules: Helper
Constant Summary collapse
- ADAPTORS =
XXX: inappropriate?
{ # XXX: inappropriate? "virtuoso" => lambda do |host_url, | require 'iq_triplestorage/virtuoso_adaptor' return IqTriplestorage::VirtuosoAdaptor.new(host_url, ) end, "sesame" => lambda do |host_url, | 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 [:repository] = repo return IqTriplestorage::SesameAdaptor.new(host_url, ) end }
Class Method Summary collapse
-
.candidates(klass = nil) ⇒ Object
returns one or multiple ActiveRecord::RelationS, depending on whether ‘klass` is provided.
Instance Method Summary collapse
-
#all ⇒ Object
TODO: rename.
-
#gather_candidates ⇒ Object
yields batches of candidates for synchronization.
-
#initialize(base_url, target_url, options) ⇒ RDFSync
constructor
A new instance of RDFSync.
- #push(records) ⇒ Object
- #serialize(record) ⇒ Object
- #sync(records, timestamp = nil) ⇒ Object
Constructor Details
#initialize(base_url, target_url, options) ⇒ RDFSync
Returns a new instance of RDFSync.
22 23 24 25 26 27 28 29 30 |
# File 'lib/iqvoc/rdf_sync.rb', line 22 def initialize(base_url, target_url, ) @base_url = base_url @target_url = target_url @username = [:username] @password = [:password] @batch_size = [:batch_size] || 100 @view_context = [: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
101 102 103 104 |
# File 'lib/iqvoc/rdf_sync.rb', line 101 def self.candidates(klass=nil) return klass ? klass.published.unsynced : Iqvoc::Sync.syncable_classes.map { |klass| candidates(klass) } end |
Instance Method Details
#all ⇒ Object
TODO: rename
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/iqvoc/rdf_sync.rb', line 32 def all # TODO: rename = Time.now errors = false gather_candidates do |records| success = sync(records, ) errors = true unless success end return !errors end |
#gather_candidates ⇒ Object
yields batches of candidates for synchronization
91 92 93 94 95 96 97 |
# File 'lib/iqvoc/rdf_sync.rb', line 91 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 'lib/iqvoc/rdf_sync.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 |
# File 'lib/iqvoc/rdf_sync.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) Iqvoc.default_rdf_namespace_helper_methods.each do |meth| doc.namespaces(@view_context.send(meth)) 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 'lib/iqvoc/rdf_sync.rb', line 44 def sync(records, =nil) ||= Time.now success = push(records) if success records.each do |record| record.update_attribute(:rdf_updated_at, ) end end return success end |