class CjCoupon
Public Class Methods
create_from_link(link, website_id)
click to toggle source
# File lib/app/models/cj_coupon.rb, line 85 def self.create_from_link(link, website_id) coupon = CjCoupon.new() =begin puts website_id puts link.xpath("./advertiser-id/text()") puts link.xpath("./link-id/text()") puts link.xpath("./advertiser-name/text()") puts link.xpath("./category/text()")[0] puts link.xpath("./language/text()") puts link.xpath("./click-commission/text()")[0] puts link.xpath("./lead-commission/text()") puts link.xpath("./link-code-html/text()") puts link.xpath("./link-code-description/text()") puts link.xpath("./link-code-destination/text()") puts link.xpath("./link-name/text()") puts link.xpath("./link-type/text()") puts link.xpath("./performance-incentive/text()") puts link.xpath("./promotion-start-date/text()") puts link.xpath("./promotion-end-date/text()") puts link.xpath("./promotion-type/text()") puts link.xpath("./relationship-status/text()") puts link.xpath("./sale-commission/text()") puts link.xpath("./sale-commission/text()") puts link.xpath("./seven-day-epc/text()").to_s.to_f puts link.xpath("./three-month-epc/text()").to_s.to_f =end coupon.website_id = website_id coupon.advertiser_id = link.xpath("./advertiser-id/text()").to_s.to_i coupon.link_id = link.xpath("./link-id/text()").to_s.to_i coupon.advertiser_name = link.xpath("./advertiser-name/text()").to_s coupon.category = link.xpath("./category/text()").to_s coupon.language = link.xpath("./language/text()").to_s coupon.click_commission = link.xpath("./click-commission/text()").to_s coupon.lead_commission = link.xpath("./lead-commission/text()").to_s coupon.link_code_html = link.xpath("./link-code-html/*").to_s coupon.link_code_description = link.xpath("./link-code-description/text()").to_s coupon.link_code_destination = link.xpath("./link-code-destination/text()").to_s coupon.link_name = link.xpath("./link-name/text()").to_s coupon.link_type = link.xpath("./link-type/text()").to_s coupon.performance_incentive = link.xpath("./performance-incentive/text()").to_s coupon.promotion_start = link.xpath("./promotion-start-date/text()").to_s coupon.promotion_end = link.xpath("./promotion-end-date/text()").to_s coupon.promotion_type = link.xpath("./promotion-type/text()").to_s coupon.ad_relationship = link.xpath("./relationship-status/text()").to_s coupon.sale_commission = link.xpath("./sale-commission/text()").to_s coupon.seven_day_epc = link.xpath("./seven-day-epc/text()").to_s.to_f coupon.three_month_epc = link.xpath("./three-month-epc/text()").to_s.to_f return coupon end
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
update_with_link(link)
click to toggle source
# File lib/app/models/cj_coupon.rb, line 64 def update_with_link(link) self.advertiser_name = link.xpath("./advertiser-name/text()").to_s self.category = link.xpath("./category/text()").to_s self.language = link.xpath("./language/text()").to_s self.click_commission = link.xpath("./click-commission/text()").to_s self.lead_commission = link.xpath("./lead-commission/text()").to_s self.link_code_html = link.xpath("./link-code-html/*").to_s self.link_code_description = link.xpath("./link-code-description/text()").to_s self.link_code_destination = link.xpath("./link-code-destination/text()").to_s self.link_name = link.xpath("./link-name/text()").to_s self.link_type = link.xpath("./link-type/text()").to_s self.performance_incentive = link.xpath("./performance-incentive/text()").to_s self.promotion_start = link.xpath("./promotion-start-date/text()").to_s self.promotion_end = link.xpath("./promotion-end-date/text()").to_s self.promotion_type = link.xpath("./promotion-type/text()").to_s self.ad_relationship = link.xpath("./relationship-status/text()").to_s self.sale_commission = link.xpath("./sale-commission/text()").to_s self.seven_day_epc = link.xpath("./seven-day-epc/text()").to_s.to_f self.three_month_epc = link.xpath("./three-month-epc/text()").to_s.to_f end