Class: Amazon::ImportAmalyticsSnapshot

Inherits:
BaseService show all
Defined in:
app/services/amazon/import_amalytics_snapshot.rb

Overview

Service object: import amalytics snapshot.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ ImportAmalyticsSnapshot

Returns a new instance of ImportAmalyticsSnapshot.



11
12
13
14
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 11

def initialize(options = {})
  @worker = options[:worker]
  super
end

Instance Attribute Details

#workerObject (readonly)

Returns the value of attribute worker.



9
10
11
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 9

def worker
  @worker
end

Instance Method Details

#at(num, message) ⇒ Object



22
23
24
25
26
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 22

def at(num, message)
  return unless @worker

  worker.at num, message
end

#process(file_path:, force: false, trial_run: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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/import_amalytics_snapshot.rb', line 28

def process(file_path:, force: false, trial_run: false)
  require 'roo'
  xlsx = Roo::Spreadsheet.open(file_path)
  # Access the first sheet (or iterate through sheets)
  sheet = xlsx.sheet(0)
  # Loop through rows
  data = sheet.parse(headers: true).drop(1)
  # Do US First
  sorted_data = data.sort_by do |entry|
    entry['Market'] == 'US' ? 0 : 1
  end
  items_modified = []
  items_result = []

  Item.transaction do
    total sorted_data.size
    sorted_data.each_with_index do |row, index|
      asin = row['ASIN']
      country_iso = row['Market'] || row['MarketplaceTitle'] # Because amalytics varies
      title = row['Title'] || row['Product Title'] # Because amalytics varies
      at index + 1, "#{country_iso} #{asin} #{title}"
      # Find the item
      item = Item.find_by(amazon_asin: asin)

      # We modify the item itself, it's based on locale, first set the locale
      locale = case country_iso
               when 'US'
                 :en
               when 'CA'
                 :'en-CA'
               else
                 # Stop here, but should be easy to add more case
                 raise "#{country_iso} requires a default locale mapping, feature not supported yet"
               end

      Mobility.with_locale(locale) do
        logger.info "Modify #{item.sku} in locale #{locale}"
        attributes = {}
        item_result = { locale: locale, sku: item.sku, item_id: item.id }

        attributes[:amazon_title]       = title if title.present? && (item.amazon_title.blank? || force)
        attributes[:amazon_feature_1]   = row['Bullet 1'] if row['Bullet 1'].present? && (item.amazon_feature_1.blank? || force)
        attributes[:amazon_feature_2]   = row['Bullet 2'] if row['Bullet 2'].present? && (item.amazon_feature_2.blank? || force)
        attributes[:amazon_feature_3]   = row['Bullet 3'] if row['Bullet 3'].present? && (item.amazon_feature_3.blank? || force)
        attributes[:amazon_feature_4]   = row['Bullet 4'] if row['Bullet 4'].present? && (item.amazon_feature_4.blank? || force)
        attributes[:amazon_feature_5]   = row['Bullet 5'] if row['Bullet 5'].present? && (item.amazon_feature_5.blank? || force)
        attributes[:amazon_feature_6]   = row['Bullet 6'] if row['Bullet 6'].present? && (item.amazon_feature_6.blank? || force)
        attributes[:amazon_feature_7]   = row['Bullet 7'] if row['Bullet 7'].present? && (item.amazon_feature_7.blank? || force)
        attributes[:amazon_feature_8]   = row['Bullet 8'] if row['Bullet 8'].present? && (item.amazon_feature_8.blank? || force)
        attributes[:amazon_feature_9]   = row['Bullet 9'] if row['Bullet 9'].present? && (item.amazon_feature_9.blank? || force)
        attributes[:amazon_generic_keyword] = row['Generic Keywords'] if row['Generic Keywords'].present? && (item.amazon_generic_keyword.blank? || force)
        attributes[:amazon_description] = row['Desc.'] if row['Desc.'].present? && (item.amazon_description.blank? || force)
        attributes.compact.each { |attr, val| item.send(:"#{attr}=", val) }
        item.save!
        items_modified << item
        item_result.merge!(attributes)
        items_result << item_result
      end
    end
    raise ActiveRecord::Rollback if trial_run
  end
  items_modified = items_modified.uniq
  items_modified.each(&:async_update_rendered_product_specifications) unless trial_run
  Result.new(success: true, items_result: items_result)
end

#total(num) ⇒ Object



16
17
18
19
20
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 16

def total(num)
  return unless @worker

  worker.total num
end