Class: BBCDataService::RDFParser

Inherits:
Object
  • Object
show all
Includes:
Vocabulary
Defined in:
lib/bbc_data_service/rdf_parser.rb

Class Method Summary collapse

Methods included from Vocabulary

included

Class Method Details

.execute_query(graph, query, mappings) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/bbc_data_service/rdf_parser.rb', line 57

def self.execute_query(graph, query, mappings)
  query_values = ""
  mappings.each do |vocabulary, values|
    values.each do |key, value|
      query_values << ":#{key} => fixture.#{key},"
    end
  end
  "{#{query_values}}"
end

.generate_query(mappings, rdf_type) ⇒ Object



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

def self.generate_query(mappings, rdf_type)
  query_hash = ""
  rdf_vocabulary, rdf_value = rdf_type.split(":")
  query_hash << "RDF.type => self.vocabularies[:#{rdf_vocabulary}].#{rdf_value},"
  mappings.each do |vocabulary, values|
    values.each do |key, value|
      query_hash << "self.vocabularies[:#{vocabulary}].#{value} => :#{key},"
    end
  end
  self.instance_eval("{#{query_hash}}")
end

.parse_feed(registered_fixture) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bbc_data_service/rdf_parser.rb', line 9

def self.parse_feed(registered_fixture)
  feed_url = registered_fixture[:feed_url]
  mappings = registered_fixture[:mappings]
  rdf_type = registered_fixture[:rdf_type]
  graph = RDF::Graph.load(feed_url)
  query = RDF::Query.new({
    :fixture => self.generate_query(mappings, rdf_type)
  })
  results = []
  query.execute(graph).each do |fixture|
    current_fixture = BBCDataService::FeedFixture.new(self.instance_eval(self.execute_query(graph, query, mappings)))
    self.process_relationships(graph, registered_fixture, current_fixture, fixture)
    results << current_fixture
  end
  results
end

.process_relationships(graph, fixture_data, current_fixture, rdf_fixture) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bbc_data_service/rdf_parser.rb', line 26

def self.process_relationships(graph, fixture_data, current_fixture, rdf_fixture)
  relationships = fixture_data[:relationships]
  if relationships
    relationships.each do |relationship|
      key = relationship.first
      values = relationship.last
      # Parse this set of RDF, then add to an array on the main object
      query = RDF::Query.new({
        :fixture => self.generate_query(values[:mappings], values[:rdf_type])
      })
      results = []
      query.execute(graph).each do |fixture|
        results << BBCDataService::FeedFixture.new(self.instance_eval(self.execute_query(graph, query, values[:mappings])))
      end
      current_fixture.add_relationship(key, results)
    end
  end
end