class CjCoupon

Public Class Methods

update_website_coupons(website_id, type_of_promotion = "coupon", records_per_page = 100) click to toggle source
# File lib/app/models/cj_coupon.rb, line 11
def self.update_website_coupons (website_id, type_of_promotion = "coupon", records_per_page = 100)
  #Start off with setting up the request.
  uri = URI("https://linksearch.api.cj.com/v2/link-search")
  params = {"website-id" => website_id, 
    "advertiser-ids" => "joined", 
    "records-per-page" => records_per_page, 
    "promotion-type" => type_of_promotion,
    "page-number" => 1}
  headers = {"authorization" => "00b0786d8cc156bc642d3b3531b858efbf445f0b9fac809ce739506268f388170e14eb47e411c5a7fd2b6eebfc333e09821e6a4748fc068e7ebe545f7a43171029/009d71a493cb3f3e6fc627280b8724da29f4af33e8aef84484cb8edb6e35b0fa97592c51e20eea0f63a36101cbe92f47073a28acbb29a293ed27c5884ef3d5a09d"}
  
  #set up the request
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.port == 443
  request = build_request(uri, params, headers)
  response = http.request(request)
  firstResponse = CGI.unescapeHTML(response.body)

  #Massage Data Here
  #first_response = CjFeedMassager.massage_feed(firstResponse)

  doc = Nokogiri::XML(firstResponse)

  total_num = doc.xpath("//links/@total-matched")[0].to_s
  total_num = total_num.to_i + 1

  (1..total_num/records_per_page).each do |n|
    page_number = {"page-number" => n}
    params.merge(page_number)

    #rebuild the request.
    request = build_request(uri,params,headers)
    response = http.request(request)
    response = CGI.unescapeHTML(response.body)
    doc = Nokogiri::XML(response)
    #parse and save all records per loop
    (doc.xpath("//link")).each do |link|
      advertiser_id = link.xpath("./advertiser-id/text()")
      link_id = link.xpath("./link-id/text()")
      
      coupon = CjCoupon.find_by_advertiser_id_and_link_id(advertiser_id.to_s.to_i, link_id.to_s.to_i)
      if(!coupon.nil?)
        coupon.update_with_link(link)
      else
        coupon = CjCoupon.create_from_link(link, website_id)
      end
      coupon.save
    end
  end
  #Hit the url
  #Parse out total-matched
  #set up loop
end

Private Class Methods

build_request(uri, params, headers) click to toggle source
# File lib/app/models/cj_coupon.rb, line 137
 def self.build_request(uri, params, headers)
   
   request = Net::HTTP::Get.new(uri.path)
 
   #load the params in
   request.set_form_data(params)

   #load the headers in
   return Net::HTTP::Get.new(uri.path+'?' + request.body, headers)
end

Public Instance Methods