Class: Iqvoc::RDFSync

Inherits:
Object
  • Object
show all
Defined in:
lib/iqvoc/rdf_sync.rb

Defined Under Namespace

Modules: Helper

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, target_host, *args) ⇒ RDFSync

Returns a new instance of RDFSync.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/iqvoc/rdf_sync.rb', line 8

def initialize(base_url, target_host, *args)
  @base_url = base_url
  @target_host = target_host
  options = args.extract_options!
  @target_port = options[:port]
  @username = options[:username]
  @password = options[:password]
  @batch_size = options[:batch_size]
  @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



88
89
90
91
# File 'lib/iqvoc/rdf_sync.rb', line 88

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



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/iqvoc/rdf_sync.rb', line 20

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



78
79
80
81
82
83
84
# File 'lib/iqvoc/rdf_sync.rb', line 78

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



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/iqvoc/rdf_sync.rb', line 45

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 = IqTriplestorage::VirtuosoAdaptor.new(@target_host, @target_port,
      @username, @password)
  return adaptor.batch_update(data)
end

#serialize(record) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/iqvoc/rdf_sync.rb', line 58

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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/iqvoc/rdf_sync.rb', line 32

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