Upload product images using the Bootic Ruby client library
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
# Full tutorial: https://developers.bootic.net/guides/ruby-upload-product-images/ | |
require 'bootic_client' | |
# Configure the client with your credentials | |
BooticClient.configure do |c| | |
c.client_id = 'MY_CLIENT_ID' | |
c.client_secret = 'MY_CLIENT_SECRET' | |
c.logger = Logger.new(STDOUT) # optional | |
c.logging = true # optional | |
c.cache_store = Rails.cache # optional | |
end | |
# Instantiate a client | |
client = BooticClient.client(:client_credentials, scope: 'admin') | |
# Fetch the root resource and your default shop | |
root = client.root | |
shop = root.shops.first | |
# all products in your shop | |
products = shop.products(status: 'all').full_set | |
# Iterate products and upload matching images. | |
# | |
products.each do |product| | |
# ex. "apple-iphone-6-plus.jpg" | |
file_name = "#{product.slug}.jpg" | |
file_path = File.expand_path(File.join(".", "images", file_name)) | |
if File.exists?(file_path) && product.has?(:create_product_asset) | |
# An instance of File | |
# http://ruby-doc.org/core-2.2.0/File.html | |
image_file = File.new(file_path) | |
# Pass the file to #create_product_asset | |
# https://developers.bootic.net/rels/create_product_asset/ | |
image = product.create_product_asset( | |
file_name: file_name, | |
data: image_file, | |
title: product.title | |
) | |
if image.has?(:errors) | |
puts "Error uploading image for #{product.slug}: #{image.errors.first.messages.first}" | |
else | |
puts "Uploaded image for #{product.slug}" | |
end | |
else | |
puts "No images for product #{product.slug}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment