Class: Amazon::ExportAmalyticsSnapshot
- Inherits:
-
BaseService
- Object
- BaseService
- Amazon::ExportAmalyticsSnapshot
- Defined in:
- app/services/amazon/export_amalytics_snapshot.rb
Defined Under Namespace
Classes: Result
Constant Summary collapse
- COLUMNS =
Column names for the spreadsheet
[ 'Market', 'ASIN', 'Title', 'Desc.', 'Bullet 1', 'Bullet 2', 'Bullet 3', 'Bullet 4', 'Bullet 5', 'Bullet 6', 'Bullet 7', 'Bullet 8', 'Bullet 9', 'Generic Keywords', 'SKU', 'Variation ASINs', 'tags', 'imageMain', 'image1', 'image2', 'image3', 'image4', 'image5', 'image6', 'image7', 'image8', 'image9', 'image10', 'image11', 'image12', 'image13', 'image14', 'imageSwatch' ]
Instance Method Summary collapse
- #append_catalog_item_to_workbook(worksheet, row_index, catalog_item, locale = nil) ⇒ Object
- #append_header_to_worksheet(worksheet) ⇒ Object
- #catalog_item_to_row_array(catalog_item) ⇒ Object
- #get_images(catalog_item) ⇒ Object
- #get_variation_asins(catalog_item) ⇒ Object
-
#prepare_catalog_items(catalog_items, limit = nil) ⇒ Object
Adds the necessary performance and joins to process our export, by default uses the north american seller catlaogs.
- #process(catalog_items: nil, output_file_path: nil, limit: nil) ⇒ 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
#append_catalog_item_to_workbook(worksheet, row_index, catalog_item, locale = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/services/amazon/export_amalytics_snapshot.rb', line 55 def append_catalog_item_to_workbook(worksheet, row_index, catalog_item, locale = nil) # Determine the locale we will use, because amalytics snapshot seems to be single market country we use the first one # This method allows to pass in a specific locale, thinking in the future if we need to target canadian french fr-CA # specifically locale ||= catalog_item.catalog.locales.first # Now with mobility we get the right attributes Mobility.with_locale(locale) do row = catalog_item_to_row_array(catalog_item) row.each_with_index do |value, col_index| worksheet.add_cell(row_index + 1, col_index, value) # +1 for header offset end end worksheet end |
#append_header_to_worksheet(worksheet) ⇒ Object
49 50 51 52 53 |
# File 'app/services/amazon/export_amalytics_snapshot.rb', line 49 def append_header_to_worksheet(worksheet) COLUMNS.each_with_index do |header, col_index| worksheet.add_cell(0, col_index, header) end end |
#catalog_item_to_row_array(catalog_item) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/services/amazon/export_amalytics_snapshot.rb', line 70 def catalog_item_to_row_array(catalog_item) row = [ catalog_item.amazon_marketplace.country.iso, catalog_item.amazon_asin, catalog_item.amazon_title, catalog_item.amazon_description, catalog_item.amazon_feature_1, catalog_item.amazon_feature_2, catalog_item.amazon_feature_3, catalog_item.amazon_feature_4, catalog_item.amazon_feature_5, catalog_item.amazon_feature_6, catalog_item.amazon_feature_7, catalog_item.amazon_feature_8, catalog_item.amazon_feature_9, catalog_item.amazon_generic_keyword, catalog_item.reported_vendor_sku, get_variation_asins(catalog_item), catalog_item..join(',') ] row += get_images(catalog_item) row end |
#get_images(catalog_item) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'app/services/amazon/export_amalytics_snapshot.rb', line 100 def get_images(catalog_item) # we add 15 image slots, then the swatch images = catalog_item.all_amazon_image_profiles.where.not(image_type: 'AMZ_SWCH').first(15).map(&:image_url) images.fill(nil, images.size...15) # Add the swatch at the end images << catalog_item.all_amazon_image_profiles.find_by(image_type: 'AMZ_SWCH')&.image_url images end |
#get_variation_asins(catalog_item) ⇒ Object
94 95 96 97 98 |
# File 'app/services/amazon/export_amalytics_snapshot.rb', line 94 def get_variation_asins(catalog_item) return unless catalog_item.item.amazon_variation.present? # Get the variation asins catalog_item.item.amazon_variation.catalog_items.active.where(catalog_id: catalog_item.catalog_id).joins(:item).distinct.pluck(Item[:amazon_asin]).map(&:presence).compact.sort.join(',') end |
#prepare_catalog_items(catalog_items, limit = nil) ⇒ Object
Adds the necessary performance and joins to process our export, by default
uses the north american seller catlaogs
44 45 46 47 |
# File 'app/services/amazon/export_amalytics_snapshot.rb', line 44 def prepare_catalog_items(catalog_items, limit = nil) catalog_items = catalog_items.limit(limit) if limit.present? catalog_items.with_asin.includes(:item, { amazon_marketplace: :country }) end |
#process(catalog_items: nil, output_file_path: nil, limit: nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/amazon/export_amalytics_snapshot.rb', line 15 def process(catalog_items: nil, output_file_path: nil, limit: nil) require 'rubyXL' # Create a new workbook workbook = RubyXL::Workbook.new worksheet = workbook[0] # Get the first sheet # Prepare a temp file if no path is specified output_file_path ||= begin tempfile = Tempfile.new(["amalytics_snapshot_#{Time.current.to_i}", '.xlsx']) tempfile.path end # Write headers append_header_to_worksheet(worksheet) # Populate rows with catalog items filtered_catalog_items = prepare_catalog_items(catalog_items, limit) filtered_catalog_items.each_with_index do |catalog_item, row_index| append_catalog_item_to_workbook(worksheet, row_index, catalog_item) end workbook.write(output_file_path) # If we created a tempfile we will flush and fsync it to ensure its written to disk if tempfile tempfile.flush tempfile.fsync end Result.new(success: true, output_file_path:) end |