Skip to content

Instantly share code, notes, and snippets.

@willamesoares
Last active August 17, 2016 18:56
Show Gist options
  • Save willamesoares/3051b413298a24c303f6d79c70ce2dfb to your computer and use it in GitHub Desktop.
Save willamesoares/3051b413298a24c303f6d79c70ce2dfb to your computer and use it in GitHub Desktop.
# Configurar Seller, Marketplace, bucket e nome da planilha
seller_id = 923
marketplace = MarketPlace.b2w
bucket = "aleap-willsoares"
sheet_name = "millevine.xls"
# Buscar Seller e AchieveLeap Seller no Marketplace correspondente
s = Seller.find seller_id
as = AchieveLeap::Seller.new s, market_place: marketplace
# Baixar planilha da S3 e criar arquivo temporário
# obs: verificar permissões da planilha no S3
Net::HTTP.start("s3-us-west-2.amazonaws.com") do |http|
resp = http.get("/#{bucket}/#{sheet_name}")
open("/tmp/#{sheet_name}", "wb") do |file|
file.write(resp.body)
end
end
# Popular sku_map com dados da planilha
sku_map = []
sheet = Spreadsheet.open("/tmp/#{sheet_name}").worksheet(0)
sheet.each do |row|
if row[0].present?
sku_map << [row[0], row[1]]
end
end
# Opcional: remover non-breaking spaces no início dos SKUs
# s.gsub(/^\p{Assigned}/, '')
not_found_prod_skus = []
Rails.logger.tagged('RESYNC', "Seller ##{s.id}", "Marketplace: #{marketplace.name}") do |logger|
sku_map[1..-1].each do |sku, mpp_sku|
p = s.products.find_by seller_sku: sku
if p
mpp = p.market_place_products.find_by market_place: marketplace
# Evitar criar um novo MarketPlaceProduct caso ele já exista
if mpp
mpp.update market_place_product_sku: mpp_sku,
status: 'waiting_update',
updated_fields: %w(price original_price quantity)
else
MarketPlaceProduct.create(
market_place: marketplace,
product: p,
market_place_product_sku: mpp_sku,
status: 'waiting_update',
updated_fields: %w(price original_price quantity))
logger.info("SKU created with success | #{sku} => #{mpp_sku}")
end
else
not_found_prod_skus << sku
logger.info "SKU's not found #{not_found_prod_skus}"
end
end
end
#sku_map = [
# ['505', '25296488'],
# ['5056', '25296499'],
# ['4176', '25283340'],
# ['2654', '25282001'],
# ['2655', '25282002'],
# ['3571', '25283925'],
# ['3525', '25283874'],
# ['3554', '25283878'],
# ['3573', '25283880'],
# ['1188', '25296348'],
# ['7637', '25328917'],
# ['141', '25336431']
#]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment