Created
July 4, 2012 13:51
-
-
Save ashga/3047449 to your computer and use it in GitHub Desktop.
this is some code that generates a csv for amazon and uses a Resque task so it runs in the background
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
class Spree::Admin::AmazonProduct < ActiveRecord::Base | |
attr_accessible :Description, :FileName, :Title, :template | |
@queue = :amazon_products | |
def self.perform(id,filename,site) | |
puts "#{Time.now}" | |
start_time = Time.now | |
puts "loading products..." | |
prods = Spree::Product.where("count_on_hand > ?",0) | |
puts "loaded #{prods.count} products processing....." | |
File.open("#{Rails.public_path}/#{filename}.csv", 'w+') {|f| | |
f.write("\"short_description\",\"description\",\"sku\",\"categories\",\"product_id\",\"price\",\"qty\",\"manufacturer\",\"image\"\n") | |
prods.each_with_index {|p,i| | |
f.write(fill_template(p,"www.kingsfieldcomputers.co.uk")) | |
puts "#{prods.count}/#{i}: #{p.sku}" | |
} | |
} | |
end_time = Time.now | |
puts "This task ran from #{start_time} - #{end_time}" | |
end | |
def self.fill_template(product,site) | |
template = "\"{short_description}\",\"{description}\",\"{sku}\",\"{categories}\",\"{product_id}\",\"{price}\",\"{qty}\",\"{manufacturer}\",\"{image}\"\n" | |
template = template.gsub("{url}", "http://#{site}/products/" + product.permalink) if template.include? "{url}" | |
template = template.gsub("{short_description}", product.name) if template.include? "{short_description}" | |
template = template.gsub("{description}", product.description) if template.include? "{description}" | |
template = template.gsub("{sku}", product.sku) if template.include? "{sku}" | |
if product.taxons.first.nil? | |
template = template.gsub("<g:product_type>{categories}</g:product_type>", "") | |
else | |
template = template.gsub("{categories}", product.taxons.first.id.to_s) if template.include? "{categories}" | |
end | |
template = template.gsub("{price}", (product.price * 1.08).to_s) if template.include? "{price}" | |
template = template.gsub("{product_id}", product.id.to_s) if template.include? "{product_id}" | |
template = template.gsub("{qty}", product.count_on_hand.to_s) if template.include? "{qty}" | |
template = template.gsub("{manufacturer}", product.name) if template.include? "{manufacturer}" | |
if !product.icecat.nil? | |
template = template.gsub("{image}", "http://images.icecat.biz/thumbs/#{product.icecat}.jpg") if template.include? "{image}" | |
else | |
template = template.gsub("{image}", "") if template.include? "{image}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment