Split growing class

This commit is contained in:
Maikel Linke
2024-10-10 16:41:19 +11:00
parent 260e7ba817
commit cde757efbd
2 changed files with 22 additions and 19 deletions

View 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

View File

@@ -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