Class: CjCoupon

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/app/models/cj_coupon.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

Instance Method Details



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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