mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Split growing class
This commit is contained in:
21
engines/dfc_provider/app/services/image_builder.rb
Normal file
21
engines/dfc_provider/app/services/image_builder.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "private_address_check"
|
||||
require "private_address_check/tcpsocket_ext"
|
||||
|
||||
class ImageBuilder < DfcBuilder
|
||||
def self.import(image_link)
|
||||
url = URI.parse(image_link)
|
||||
filename = File.basename(image_link)
|
||||
|
||||
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
|
||||
end
|
||||
@@ -1,8 +1,5 @@
|
||||
# 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(
|
||||
@@ -91,7 +88,7 @@ class SuppliedProductBuilder < DfcBuilder
|
||||
price: 0, # will be in DFC Offer
|
||||
supplier_id: supplier.id,
|
||||
primary_taxon_id: taxon(supplied_product).id,
|
||||
image: image(supplied_product),
|
||||
image: ImageBuilder.import(supplied_product.image),
|
||||
).tap do |product|
|
||||
QuantitativeValueBuilder.apply(supplied_product.quantity, product)
|
||||
product.ensure_standard_variant
|
||||
@@ -121,20 +118,5 @@ class SuppliedProductBuilder < DfcBuilder
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user