Skip to content

Instantly share code, notes, and snippets.

@akintner
Last active May 16, 2019 17:29
Show Gist options
  • Save akintner/0caa90fc0066576a75b52b346ccb736e to your computer and use it in GitHub Desktop.
Save akintner/0caa90fc0066576a75b52b346ccb736e to your computer and use it in GitHub Desktop.
class Api::Blueshift::ProductCatalogFlatFile
def generate_products
products = Product.active.marketable_in(SalesChannel.web).includes(:product_family).where.not(country: 'CA')
products.map do |product|
[
product.model_number,
"Tuft & Needle",
product.cost / 100,
product.name,
"products > " + product.product_family.full_name.to_s,
product.tags.map { |tag| tag.name }.join(','),
"in stock",
"https://www.tuftandneedle.com#{product.product_family.path}",
checkout_image_url_for(product),
product.size,
if product.name.include?("Sheet Set")
product.name.split(' - ').take(2).join(' | ')
else
product.name.split(' - ').join(' | ')
end
]
end
end
def checkout_image_url_for(product)
family_name_component = product.family.present? ? "#{product.family.name}_" : ""
product_type_component = product.product_type
product_options_component = ""
if product.options&.has_key?('color')
product_options_component = "_" + product.options['color']
end
url = "https://www.tuftandneedle.com/images/checkout/img_line_items_white_#{family_name_component}#{product_type_component}#{product_options_component}@2x.jpg"
end
def generate_csv
CSV.open('trialproductcatalog.csv', 'w', force_quotes: true) do |csv|
csv << ["product_id", "brand", "msrp", "title", "category", "tags", "availability", "web_link", "image", "size", "product_name"]
generate_products.each do |product|
csv << product
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment