Created
November 24, 2015 23:01
-
-
Save joelevering/da97323f0822a493313d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Reverb | |
| module Google | |
| module ShoppingV2 | |
| class RemoveEndedOrSoldOutProducts | |
| def self.remove | |
| new.remove | |
| end | |
| def remove | |
| Product.ended.joins(shop: :google_merchant_account).where("merchant_account_id IS NOT NULL AND products.created_at > ?", 2.months.ago).find_in_batches(batch_size: 100) do |batch| | |
| Reverb::Google::ShoppingV2::Request.build(url: url, body: body(batch), method: :post, dry_run: false).run | |
| end | |
| end | |
| private | |
| def body(products) | |
| JSON.generate({entries: entries(products)}) | |
| end | |
| def entries(products) | |
| products.inject(Array.new) do |entries, product| | |
| GoogleShoppingCountry::ACTIVE_COUNTRY_CODES.each do |country_code| | |
| entries << {"batchId" => batch_id(product, country_code), "merchantId" => merchant_id(product), "method" => "delete", "productId" => "online:en:#{country_code}:#{product.id}"} | |
| end | |
| end | |
| end | |
| def batch_id(product, country_code) | |
| product.id + country_code.bytes.join("").to_i | |
| end | |
| def merchant_id(product) | |
| product.shop.google_merchant_account.merchant_account_id | |
| end | |
| def url | |
| "https://www.googleapis.com/content/v2/products/batch" | |
| end | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment