Class: BBCDataService::RDFParser
- Inherits:
-
Object
- Object
- BBCDataService::RDFParser
- Includes:
- Vocabulary
- Defined in:
- lib/bbc_data_service/rdf_parser.rb
Class Method Summary collapse
- .execute_query(graph, query, mappings) ⇒ Object
- .filter_graph(solutions, filter) ⇒ Object
- .filter_type(filter) ⇒ Object
- .generate_query(mappings, rdf_type) ⇒ Object
- .parse_feed(registered_fixture, options) ⇒ Object
- .process_relationships(graph, fixture_data, current_fixture, rdf_fixture) ⇒ Object
Methods included from Vocabulary
Class Method Details
.execute_query(graph, query, mappings) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/bbc_data_service/rdf_parser.rb', line 101 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 |
.filter_graph(solutions, filter) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/bbc_data_service/rdf_parser.rb', line 68 def self.filter_graph(solutions, filter) if filter[:type] == :uri solutions.filter(filter[:column].to_sym => RDF::URI(filter[:value])) else solutions.filter(filter[:column].to_sym => RDF::Literal(filter[:value], :datatype => self.filter_type(filter[:type]))) end end |
.filter_type(filter) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bbc_data_service/rdf_parser.rb', line 76 def self.filter_type(filter) case filter when :string return "xsd:string" when :xml_string return "http://www.w3.org/2001/XMLSchema#string" else raise "Unsupported type passed" end end |
.generate_query(mappings, rdf_type) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/bbc_data_service/rdf_parser.rb', line 87 def self.generate_query(mappings, rdf_type) query_hash = "" if rdf_type rdf_vocabulary, rdf_value = rdf_type.split(":") query_hash << "RDF.type => self.vocabularies[:#{rdf_vocabulary}].#{rdf_value}," end 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, options) ⇒ Object
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 |
# File 'lib/bbc_data_service/rdf_parser.rb', line 10 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 = [] fixtures = [] if [:filter] solutions = query.execute(graph) fixtures = self.filter_graph(solutions, [:filter]) elsif registered_fixture[:filter] solutions = query.execute(graph) fixtures = self.filter_graph(solutions, registered_fixture[:filter]) else fixtures = query.execute(graph) end fixtures.each do |fixture| current_fixture = BBCDataService::FeedFixture.new(self.instance_eval(self.execute_query(graph, query, mappings))) current_fixture.rdf_object = fixture current_fixture = 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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bbc_data_service/rdf_parser.rb', line 38 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 if values[:foreign_key] foreign_key = self.instance_eval("current_fixture.#{values[:foreign_key]}") query = RDF::Query.new({ RDF::URI(foreign_key) => self.generate_query(values[:mappings], values[:rdf_type]) }) else query = RDF::Query.new({ :fixture => self.generate_query(values[:mappings], values[:rdf_type]) }) end results = [] fixtures = query.execute(graph) fixtures.each do |fixture| relationship_fixture = BBCDataService::FeedFixture.new(self.instance_eval(self.execute_query(graph, query, values[:mappings]))) relationship_fixture.rdf_object = fixture results << relationship_fixture end current_fixture.add_relationship(key, results) end end current_fixture end |