class CouponAggregator

Constants

CJ

apparently there are no enums in ruby

FMTC

Public Class Methods

new(params, feed) click to toggle source

params for fmtc is just a “key” => “api key” params for cj involve website-id (ours, assigned by CJ), records-per-page (100 max), promotion-type (coupon), and page-number

# File lib/coupon_aggregator.rb, line 23
def initialize(params, feed)
  #I think we will probably move towards a place where params can be replaced by
  # a db object
  @params = params
  @feed = feed
end

Public Instance Methods

aggregate(site_id) click to toggle source

@params site_id int Our site id for the site we are parsing coupons for.

# File lib/coupon_aggregator.rb, line 31
def aggregate(site_id)
  case @feed
    when CouponAggregator::CJ
      aggregate_cj_coupons(site_id)
    when CouponAggregator::FMTC
      aggregate_fmtc_coupons()
    else
      aggregate_all_coupons_for_site(site_id)
  end
end

Private Instance Methods

aggregate_all_coupons_for_site(site_id) click to toggle source
# File lib/coupon_aggregator.rb, line 92
def aggregate_all_coupons_for_site(site_id)
  #this will call each of the aggregate methods.
end
aggregate_cj_coupons(site_id) click to toggle source
# File lib/coupon_aggregator.rb, line 43
def aggregate_cj_coupons(site_id)

  vendor_id = Vendor.find_by_nice_name("commission-junction").id
  feed_parser = FeedParser.new(site_id, vendor_id)

  #Find out what site ID commission junction has assigned the site we are pulling for
  relationship_id = SiteVendorLink.find_by_site_id_and_vendor_id(site_id,vendor_id).relationship_id
  temp_params = {"website-id" => relationship_id}
  @params.merge!(temp_params)

  uri = URI("https://linksearch.api.cj.com/v2/link-search") #This can go into vendors table
  headers = {"authorization" => "00b0786d8cc156bc642d3b3531b858efbf445f0b9fac809ce739506268f388170e14eb47e411c5a7fd2b6eebfc333e09821e6a4748fc068e7ebe545f7a43171029/009d71a493cb3f3e6fc627280b8724da29f4af33e8aef84484cb8edb6e35b0fa97592c51e20eea0f63a36101cbe92f47073a28acbb29a293ed27c5884ef3d5a09d"} #this might go into some headings table

  require 'net/https'
  http          = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl  = true if uri.port == 443
  request       = HtmlConnectionHelper.build_request(uri,@params,headers)
  response      = http.request(request)

  total_num = calculate_total_pages(response)

  (1..total_num).each do |n|
    
    puts "Working on page: " + n.to_s
    
    page_number = {"page-number" => n}
    @params.merge!(page_number)
    request   = HtmlConnectionHelper.build_request(uri,@params,headers)
    response  = http.request(request)
    response  = CGI.unescapeHTML(response.body)
    feed_parser.create_coupons_from_xml(response)
  end
end
aggregate_fmtc_coupons() click to toggle source
# File lib/coupon_aggregator.rb, line 77
def aggregate_fmtc_coupons()

  vendor_id   = Vendor.find_by_vendor_name("for me to coupon").id
  site        = Site.find_by_site_name("All");
  feed_parser = FeedParser.new(site.id,vendor_id)

  #need to open the file?
  uri       = URI("http://www.formetocoupon.com/services/getDeals") #this can go into vendors table
  headers   = nil
  request   = HtmlConnectionHelper.build_request(uri,@params,headers)
  response  = http.request(request)
  
  feed_parser.create_coupons_from_xml(response.body)
end
calculate_total_pages(response) click to toggle source
# File lib/coupon_aggregator.rb, line 96
def calculate_total_pages(response)
  doc = Nokogiri::XML(CGI.unescapeHTML(response.body))
  records_per_page = doc.xpath("//links/@records-returned")[0].to_s.to_i
  return (doc.xpath("//links/@total-matched")[0].to_s.to_i / records_per_page) + 1
end