Created
February 19, 2020 07:15
-
-
Save arjunrajkumar/6654fc93ecba55a2907517cf7b592311 to your computer and use it in GitHub Desktop.
This file contains 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 ShopifyWrapper | |
class Download | |
attr_reader :response, :error_message | |
def initialize(response, error_message) | |
@response = response | |
@error_message = error_message | |
end | |
def self.all_products(options={}) | |
ShopifyWrapper.activate_session(options[:shop].shopify_domain, options[:shop].shopify_token) | |
begin | |
response = ShopifyAPI::Product.find(:all, params: { fields: 'id,title,vendor,product_type,handle,variants,images', limit: 250 }) | |
if response.present? | |
ShopifyWrapper.process_all_products(response, options[:shop].id) | |
while response.next_page? | |
ShopifyWrapper.process_all_products(response) | |
response = response.fetch_next_page | |
end | |
ShopifyWrapper.deactivate_session | |
new(response, nil) | |
else | |
ShopifyWrapper.deactivate_session | |
new(nil, "There was an error while downloading the products from Shopify.") | |
end | |
rescue | |
new(nil, "There was an error connecting to Shopify.") | |
end | |
end | |
def self.single_product(options={}) | |
ShopifyWrapper.activate_session(options[:shop].shopify_domain, options[:shop].shopify_token) | |
begin | |
response = ShopifyAPI::Product.find(options[:product].shopify_product_id) | |
if response.present? | |
ShopifyWrapper.process_single_product(response, options[:shop].id, options[:product]) | |
ShopifyWrapper.deactivate_session | |
new(response, nil) | |
else | |
ShopifyWrapper.deactivate_session | |
new(nil, "There was an error while downloading #{options[:product].title} from Shopify.") | |
end | |
rescue | |
new(nil, "There was an error connecting to Shopify.") | |
end | |
end | |
def successful? | |
response.present? | |
end | |
end | |
def self.activate_session(shopify_domain, shopify_token) | |
shopify_session = ShopifyAPI::Session.new(domain: shopify_domain, token: shopify_token, api_version: Rails.application.credentials.shopify_api_version) | |
ShopifyAPI::Base.activate_session(shopify_session) | |
end | |
def self.deactivate_session | |
ShopifyAPI::Base.clear_session | |
end | |
def self.process_all_products(shopify_products, shop_id) | |
products = [] | |
shopify_products.each do |shopify_product| | |
product = Product.new(last_shopify_sync: DateTime.now.utc, | |
title: shopify_product.title, | |
vendor: shopify_product.title, | |
product_type: shopify_product.product_type, | |
handle: shopify_product.handle, | |
shopify_product_id: shopify_product.id, | |
last_downloaded_products_success: true, | |
shop_id: shop_id | |
) | |
shopify_product.variants.each do |shopify_variant| | |
product.variants.build(title: shopify_variant.title, | |
option1: shopify_variant.option1, option2: shopify_variant.option2, option3: shopify_variant.option3, | |
shopify_variant_id: shopify_variant.id, | |
price: shopify_variant.price, | |
image_id: shopify_variant.image_id, | |
shop_id: shop_id, | |
last_shopify_sync: DateTime.now.utc | |
) | |
end | |
shopify_product.images.each do |shopify_image| | |
product.images.build(position: shopify_image.position, | |
src: shopify_image.src, | |
shopify_image_id: shopify_image.id, | |
variant_ids: shopify_image.variant_ids, | |
width: shopify_image.width, | |
height: shopify_image.height, | |
product_id: product.id, | |
shop_id: shop_id, | |
last_shopify_sync: DateTime.now.utc | |
) | |
end | |
products << product | |
end | |
Product.import products, recursive: true, on_duplicate_key_ignore: true | |
end | |
def self.process_single_product(shopify_product, shop_id, product) | |
product.images.destroy_all | |
images = [] | |
shopify_product.images.each do |shopify_image| | |
image = Image.new(position: shopify_image.position, | |
src: shopify_image.src, | |
shopify_image_id: shopify_image.id, | |
variant_ids: shopify_image.variant_ids, | |
width: shopify_image.width, | |
height: shopify_image.height, | |
product_id: product.id, | |
shop_id: shop_id, | |
last_shopify_sync: DateTime.now.utc | |
) | |
images << image | |
end | |
Image.import images | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment