Class: Amazon::ImageExporter
- Inherits:
-
BaseService
- Object
- BaseService
- Amazon::ImageExporter
- Defined in:
- app/services/amazon/image_exporter.rb
Overview
Bulk Image Upload Preparer for Amazon
https://sellercentral.amazon.com/imaging/upload
Or for specific country:
https://sellercentral.amazon.com/imaging/upload/country
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
- #process(items: nil, catalog_items: nil, item_skus: nil, item_asins: nil, directory_path: nil) ⇒ Object
- #process_item_images(item, directory_path) ⇒ Object
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Method Details
#process(items: nil, catalog_items: nil, item_skus: nil, item_asins: nil, directory_path: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/services/amazon/image_exporter.rb', line 17 def process(items: nil, catalog_items: nil, item_skus: nil, item_asins: nil, directory_path: nil) target_items = items target_items ||= Item.where(sku: item_skus) target_items ||= Item.where(amazon_asin: item_asins) if catalog_items.present? target_items ||= Item.joins(store_items: :catalog_item).merge(catalog_items) raise ArgumentError, 'All catalog items must belong to the same marketplace' if catalog_items.map(&:amazon_marketplace).uniq.count > 1 end raise ArgumentError, 'Provide either items, item_skus, item_asins or catalog_items' if target_items.blank? target_items = target_items.active.where.not(amazon_asin: nil) packet_name = "amazon_image_export_#{Time.now.strftime('%Y%m%d%H%M%S')}" directory_path ||= Rails.root.join(Rails.application.config.x.temp_storage_path.to_s, 'amazon_assets', packet_name) FileUtils.mkdir_p(directory_path) asins = [] item_skus = [] target_items.find_each do |item| asins << item.amazon_asin item_skus << item.sku process_item_images(item, directory_path) end target_zip_file_path = Rails.root.join(Rails.application.config.x.temp_storage_path.to_s, 'amazon_assets', "#{packet_name}.zip") # Now zip the directory ZipFileGenerator.new(directory_path, target_zip_file_path, cleanup: true).write Result.new(result: :ok, zip_file_path: target_zip_file_path, directory_path: directory_path, asins: asins, item_skus: item_skus) end |
#process_item_images(item, directory_path) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/services/amazon/image_exporter.rb', line 45 def process_item_images(item, directory_path) # Find the item's images image_profiles = item.all_amazon_image_profiles image_profiles.each do |image_profile| carousel_image_url = image_profile.image_url # What will be our filename? filename = [item.amazon_asin, image_profile.amazon_image_type, 'jpg'].join('.') destination = File.join(directory_path, filename) # Download it, we go through ImageKit which does the image processing for us Down.download(carousel_image_url, destination:) end end |