mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Import DFC graphs with multiple objects
This commit is contained in:
@@ -20,11 +20,16 @@ module DataFoodConsortium
|
||||
end
|
||||
|
||||
def import(json_string)
|
||||
@subjects = {}
|
||||
|
||||
graph = parse_rdf(json_string)
|
||||
head, *tail = graph.to_a
|
||||
subject = build_subject(head)
|
||||
apply_statements(subject, tail)
|
||||
subject
|
||||
apply_statements(graph)
|
||||
|
||||
if @subjects.size > 1
|
||||
@subjects.values
|
||||
else
|
||||
@subjects.values.first
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
@@ -42,13 +47,15 @@ module DataFoodConsortium
|
||||
clazz.new(id)
|
||||
end
|
||||
|
||||
def apply_statements(subject, statements)
|
||||
def apply_statements(statements)
|
||||
statements.each do |statement|
|
||||
apply_statement(subject, statement)
|
||||
apply_statement(statement)
|
||||
end
|
||||
end
|
||||
|
||||
def apply_statement(subject, statement)
|
||||
def apply_statement(statement)
|
||||
subject = subject_of(statement)
|
||||
|
||||
return unless subject.hasSemanticProperty?(statement.predicate.value)
|
||||
|
||||
prop_name = statement.predicate.fragment
|
||||
@@ -59,6 +66,10 @@ module DataFoodConsortium
|
||||
value = statement.object.object
|
||||
subject.public_send(setter_name, value)
|
||||
end
|
||||
|
||||
def subject_of(statement)
|
||||
@subjects[statement.subject] ||= build_subject(statement)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,6 +5,12 @@ require Rails.root.join('lib/data_food_consortium/connector/connector')
|
||||
|
||||
describe DataFoodConsortium::Connector::Importer, vcr: true do
|
||||
let(:connector) { DataFoodConsortium::Connector::Connector.instance }
|
||||
let(:catalog_item) do
|
||||
DataFoodConsortium::Connector::CatalogItem.new(
|
||||
"https://example.net/tomatoItem",
|
||||
product:,
|
||||
)
|
||||
end
|
||||
let(:product) do
|
||||
DataFoodConsortium::Connector::SuppliedProduct.new(
|
||||
"https://example.net/tomato",
|
||||
@@ -25,6 +31,22 @@ describe DataFoodConsortium::Connector::Importer, vcr: true do
|
||||
expect(result.totalTheoreticalStock).to eq 3
|
||||
end
|
||||
|
||||
it "imports a graph with multiple objects" do
|
||||
result = import(catalog_item, product)
|
||||
|
||||
expect(result).to be_a Array
|
||||
expect(result.size).to eq 2
|
||||
|
||||
item, tomato = result
|
||||
|
||||
expect(item.class).to eq catalog_item.class
|
||||
expect(item.semanticType).to eq catalog_item.semanticType
|
||||
expect(item.semanticId).to eq "https://example.net/tomatoItem"
|
||||
expect(tomato.name).to eq "Tomato"
|
||||
expect(tomato.description).to eq "Awesome tomato"
|
||||
expect(tomato.totalTheoreticalStock).to eq 3
|
||||
end
|
||||
|
||||
def import(*args)
|
||||
json = connector.export(*args)
|
||||
connector.import(json)
|
||||
|
||||
Reference in New Issue
Block a user