Skip to content

Commit

Permalink
Import DFC product images
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Sep 25, 2024
1 parent 7f62b49 commit 66f0802
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 18 deletions.
21 changes: 20 additions & 1 deletion engines/dfc_provider/app/services/supplied_product_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require "private_address_check"
require "private_address_check/tcpsocket_ext"

class SuppliedProductBuilder < DfcBuilder
def self.supplied_product(variant)
id = urls.enterprise_supplied_product_url(
Expand Down Expand Up @@ -66,7 +69,8 @@ def self.import_product(supplied_product, supplier)
description: supplied_product.description,
price: 0, # will be in DFC Offer
supplier_id: supplier.id,
primary_taxon_id: taxon(supplied_product).id
primary_taxon_id: taxon(supplied_product).id,
image: image(supplied_product),
).tap do |product|
QuantitativeValueBuilder.apply(supplied_product.quantity, product)
product.ensure_standard_variant
Expand Down Expand Up @@ -96,5 +100,20 @@ def self.taxon(supplied_product)
Spree::Taxon.find_by(dfc_id:) || Spree::Taxon.first
end

def self.image(supplied_product)
url = URI.parse(supplied_product.image)
filename = File.basename(supplied_product.image)

Spree::Image.new.tap do |image|
PrivateAddressCheck.only_public_connections do
image.attachment.attach(io: url.open, filename:)
end
end
rescue StandardError
# Any URL parsing or network error shouldn't impact the product import
# at all. Maybe we'll add UX for error handling later.
nil
end

private_class_method :product_type, :taxon
end
7 changes: 5 additions & 2 deletions engines/dfc_provider/lib/dfc_provider/supplied_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ class SuppliedProduct < DataFoodConsortium::Connector::SuppliedProduct
attr_accessor :spree_product_id, :spree_product_uri, :image

def initialize(
semantic_id, spree_product_id: nil, spree_product_uri: nil, image_url: nil, **properties
semantic_id, spree_product_id: nil, spree_product_uri: nil,
image: nil, image_url: nil, **properties
)
super(semantic_id, **properties)

self.spree_product_id = spree_product_id
self.spree_product_uri = spree_product_uri
self.image = image_url
self.image = image || image_url

# This is now replaced by spree_product_uri, keeping it for backward compatibility
register_ofn_property("spree_product_id")
register_ofn_property("spree_product_uri")
# Temporary solution, will be replaced by "dfc_b:image" in future version of the DFC connector
register_ofn_property("image")
registerSemanticProperty("dfc-b:image", &method(:image))
.valueSetter = method("image=")
end

def register_ofn_property(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

describe ".import_product" do
let(:supplied_product) do
DataFoodConsortium::Connector::SuppliedProduct.new(
DfcProvider::SuppliedProduct.new(
"https://example.net/tomato",
name: "Tomato",
description: "Awesome tomato",
Expand All @@ -95,6 +95,7 @@
value: 2,
),
productType: product_type,
image: "https://cd.net/tomato.png?v=5",
)
end
let(:product_type) { DfcLoader.connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE }
Expand All @@ -106,13 +107,23 @@
)
}

before do
stub_request(:get, "https://cd.net/tomato.png?v=5").to_return(
status: 200,
body: black_logo_path.read,
)
end

it "creates a new Spree::Product" do
product = builder.import_product(supplied_product, supplier)

expect(product).to be_a(Spree::Product)
expect(product.name).to eq("Tomato")
expect(product.description).to eq("Awesome tomato")
expect(product.variant_unit).to eq("weight")
expect(product.image).to be_present
expect(product.image.attachment).to be_attached
expect(product.image.url(:product)).to match /^http.*tomato\.png/
end

describe "taxon" do
Expand Down

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions spec/system/admin/dfc_product_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@
}

expect(page).to have_content "Importing a DFC product catalog"

product = Spree::Product.last

expect(product.variants[0].semantic_links).to be_present
expect(product.image).to be_present
end
end

0 comments on commit 66f0802

Please sign in to comment.