class CouponFactory

Public Class Methods

create_coupon_from_item(item, site_id, vendor_id) click to toggle source

Deprecated

# File lib/coupon_factory.rb, line 53
  def self.create_coupon_from_item(item, site_id, vendor_id)

    site = Site.find(site_id)
    vendor = Vendor.find(vendor_id)

    advertiser_name = item.xpath("./advertiser-name/text()").to_s
    advertiser = self.check_advertiser(advertiser_name, site.id, vendor.id)
    
    coupon = Coupon.new()
    coupon.site_id = site.id
    coupon.vendor_id = vendor.id
    coupon.advertiser_id = advertiser.id
    coupon.code = item.xpath("./couponcode/text()").to_s
    coupon.description = item.xpath("./description/text()").to_s
    coupon.link = item.xpath("./link/*").to_s
    coupon.save
    #set up the categories
    coupon = self.add_categories_to_coupon(coupon,item)
    coupon = self.add_products_to_coupon(coupon,item)
    return coupon
    #set up the products
=begin
    coupon.item_id = item.xpath("./item-id/text()").to_s.to_i
    coupon.category = item.xpath("./category/text()").to_s
    coupon.language = item.xpath("./language/text()").to_s
    coupon.click_commission = item.xpath("./click-commission/text()").to_s
    coupon.lead_commission = item.xpath("./lead-commission/text()").to_s
    coupon.item_code_html = item.xpath("./item-code-html/*").to_s
    coupon.item_code_description = item.xpath("./item-code-description/text()").to_s
    coupon.item_code_destination = item.xpath("./item-code-destination/text()").to_s
    coupon.item_name = item.xpath("./item-name/text()").to_s
    coupon.item_type = item.xpath("./item-type/text()").to_s
    coupon.performance_incentive = item.xpath("./performance-incentive/text()").to_s
    coupon.promotion_start = item.xpath("./promotion-start-date/text()").to_s
    coupon.promotion_end = item.xpath("./promotion-end-date/text()").to_s
    coupon.promotion_type = item.xpath("./promotion-type/text()").to_s
    coupon.ad_relationship = item.xpath("./relationship-status/text()").to_s
    coupon.sale_commission = item.xpath("./sale-commission/text()").to_s
    coupon.seven_day_epc = item.xpath("./seven-day-epc/text()").to_s.to_f
    coupon.three_month_epc = item.xpath("./three-month-epc/text()").to_s.to_f    
=end
  end
update_coupon_with_item(coupon,item,site_id,vendor_id) click to toggle source
# File lib/coupon_factory.rb, line 3
  def self.update_coupon_with_item(coupon,item,site_id,vendor_id)

    site = Site.find(site_id)
    vendor = Vendor.find(vendor_id)

    advertiser_name = item.xpath("./advertiser-name/text()").to_s
    advertiser_id = item.xpath("./advertiser-id/text()").to_s
    advertiser = self.check_advertiser(advertiser_id, advertiser_name, site.id, vendor.id)

    coupon.site_id = site.id
    coupon.vendor_id = vendor.id
    coupon.advertiser_id = advertiser.id
    coupon.code = item.xpath("./couponcode/text()").to_s
    coupon.description = item.xpath("./description/text()").to_s
    coupon.link = item.xpath("./link/*").to_s
    coupon.save
    
    
    #sort out categories
    coupon.categories.clear
    coupon = self.add_categories_to_coupon(coupon,item)
    #sort out products
    coupon.products.clear
    coupon = self.add_products_to_coupon(coupon,item)
    return coupon
=begin
    coupon.advertiser_name = item.xpath("./advertiser-name/text()").to_s
    coupon.category = item.xpath("./category/text()").to_s
    coupon.language = item.xpath("./language/text()").to_s
    coupon.click_commission = item.xpath("./click-commission/text()").to_s
    coupon.lead_commission = item.xpath("./lead-commission/text()").to_s
    coupon.item_code_html = item.xpath("./item-code-html/*").to_s
    coupon.item_code_description = item.xpath("./item-code-description/text()").to_s
    coupon.item_code_destination = item.xpath("./item-code-destination/text()").to_s
    coupon.item_name = item.xpath("./item-name/text()").to_s
    coupon.item_type = item.xpath("./item-type/text()").to_s
    coupon.performance_incentive = item.xpath("./performance-incentive/text()").to_s
    coupon.start_date = item.xpath("./startdate/text()").to_s
    coupon.end_date = item.xpath("./enddate/text()").to_s
    coupon.promotion_type = item.xpath("./promotion-type/text()").to_s
    coupon.ad_relationship = item.xpath("./relationship-status/text()").to_s
    coupon.sale_commission = item.xpath("./sale-commission/text()").to_s
    coupon.seven_day_epc = item.xpath("./seven-day-epc/text()").to_s.to_f
    coupon.three_month_epc = item.xpath("./three-month-epc/text()").to_s.to_f
=end
  end

Private Class Methods

add_categories_to_coupon(coupon,item) click to toggle source
# File lib/coupon_factory.rb, line 97
def self.add_categories_to_coupon(coupon,item)
  item.xpath("./category").each do |category_xml|
    category_name = category_xml.xpath("./text()").to_s
    category_nice_name = category_name.gsub(/[^A-Za-z0-9]+/, '-').strip.gsub(/\ +/, '-').downcase
    
    #Update/Create category
    category = Category.find_or_create_by_nice_name(category_nice_name, :name => category_name)
    
    #Create the relationship
    CategoryCouponLink.find_or_create_by_category_id_and_coupon_id(category.id, coupon.id)
  end
  return coupon
end
add_products_to_coupon(coupon, item) click to toggle source

We don’t actually have any products yet.

# File lib/coupon_factory.rb, line 112
def self.add_products_to_coupon(coupon, item)
  item.xpath("./product").each do |product|
    coupon.products.find_or_create_by_name(product.xpath("./text()").to_s)
  end
  return coupon
end
check_advertiser(advertiser_id, advertiser_name, site_id, vendor_id) click to toggle source
# File lib/coupon_factory.rb, line 119
def self.check_advertiser(advertiser_id, advertiser_name, site_id, vendor_id)
  advertiser = Advertiser.find_or_create_by_nice_name(
    :nice_name => advertiser_name.gsub(/[^A-Za-z0-9]+/, '-').strip.gsub(/\ +/, '-').downcase, 
    :name => advertiser_name, :advertiser_id => advertiser_id)
  AdvertiserVendorLink.find_or_create_by_vendor_id_and_advertiser_id(:vendor_id => vendor_id, :advertiser_id => advertiser.id)
  AdvertiserSiteLink.find_or_create_by_site_id_and_advertiser_id(:site_id => site_id, :advertiser_id => advertiser.id)
  return advertiser
end