Class: Amazon::ImportAmalyticsSnapshot
- Inherits:
-
BaseService
- Object
- BaseService
- Amazon::ImportAmalyticsSnapshot
- Defined in:
- app/services/amazon/import_amalytics_snapshot.rb
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#worker ⇒ Object
readonly
Returns the value of attribute worker.
Instance Method Summary collapse
- #at(num, message) ⇒ Object
-
#initialize(options = {}) ⇒ ImportAmalyticsSnapshot
constructor
A new instance of ImportAmalyticsSnapshot.
- #process(file_path:, force: false, trial_run: false) ⇒ Object
- #total(num) ⇒ Object
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
#initialize(options = {}) ⇒ ImportAmalyticsSnapshot
Returns a new instance of ImportAmalyticsSnapshot.
8 9 10 11 |
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 8 def initialize( = {}) @worker = [:worker] super end |
Instance Attribute Details
#worker ⇒ Object (readonly)
Returns the value of attribute worker.
6 7 8 |
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 6 def worker @worker end |
Instance Method Details
#at(num, message) ⇒ Object
19 20 21 22 23 |
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 19 def at(num, ) return unless @worker worker.at num, end |
#process(file_path:, force: false, trial_run: false) ⇒ Object
25 26 27 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 |
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 25 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
13 14 15 16 17 |
# File 'app/services/amazon/import_amalytics_snapshot.rb', line 13 def total(num) return unless @worker worker.total num end |