Skip to content

Instantly share code, notes, and snippets.

@tanelj
Last active August 22, 2023 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanelj/96606cb88df6a78c2b30e0b3374b23e7 to your computer and use it in GitHub Desktop.
Save tanelj/96606cb88df6a78c2b30e0b3374b23e7 to your computer and use it in GitHub Desktop.
Voog CMS Ecommerce template upluloader
# This script uploads all templates and layouts from local disk to Voog CMS.
#
# Read more about Voog CMS Ecommerce Templates: https://www.voog.com/developers/markup/basic-examples/ecommerce-templates
# Read more about Ecommerce Templates API https://www.voog.com/developers/api/ecommerce/templates
# Example files: https://github.com/Voog/voog-liquid-examples/tree/main/ecommerce/templates
# Requierements
# gem install voog_api
require 'voog_api'
# Initialize API client
RESOURCE = 'ecommerce/v1/templates'
host = 'TEST.voog.com'
api_token = 'TOKEN'
protocol = :https # :http
client = Voog::Client.new(host, api_token, protocol: protocol)
# List templates
templates = client.get(RESOURCE)
# Read files from local disk
Dir['components/*.tpl'].each do |filename|
# Read file content
content = File.read(filename)
# Prepare data in JSON format
data = {
enabled: true,
content_type: 'component',
component_name: File.basename(filename, '.tpl'),
content: content
}
# Check if component exists
component = templates.find { |t| t['component_name'] == data[:component_name] }
if component.nil?
# Create new component
client.post(data, RESOURCE)
else
# Update existing component
client.put("#{RESOURCE}/#{component['id']}", data)
end
end
# Read layout files from local disk and update
Dir['layouts/*.tpl'].each do |filename|
# Read file content
content = File.read(filename)
# Prepare data in JSON format
data = {
enabled: true,
content_type: File.basename(filename, '.tpl'),
content: content
}
# Check if layout exists
layout = templates.find { |t| t['content_type'] == data[:content_type] }
if layout.nil?
# Create new layout
client.post(data, RESOURCE)
else
# Update existing layout
client.put("#{RESOURCE}/#{component['id']}", data)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment