Class: CJAdvertisers

Inherits:
Object
  • Object
show all
Defined in:
lib/cj_advertisers.rb

Instance Method Summary collapse

Constructor Details

#initialize(params, feed = {}) ⇒ CJAdvertisers

Returns a new instance of CJAdvertisers.



8
9
10
11
12
13
# File 'lib/cj_advertisers.rb', line 8

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

Instance Method Details

#calculate_total_pages(response) ⇒ Object



51
52
53
54
55
# File 'lib/cj_advertisers.rb', line 51

def calculate_total_pages(response)
  doc = Nokogiri::XML(CGI.unescapeHTML(response.body))
  records_per_page = doc.xpath("//advertisers/@records-returned")[0].to_s.to_i
  return (doc.xpath("//advertisers/@total-matched")[0].to_s.to_i / records_per_page) + 1
end

#collect_advertisersObject



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
# File 'lib/cj_advertisers.rb', line 15

def collect_advertisers()
  #Setup the models we'll need
  advert_parser = AdvertiserParser.new
  
  uri = URI("https://advertiser-lookup.api.cj.com/v3/advertiser-lookup") #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)

  puts "Total pages to grab: " + total_num.to_s
  
  #Go through each page
  
  (1..total_num).each_with_index do |n, index|
    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)
    
    #Go parse the page!
    advert_parser.create_cj_advertiser_from_xml(response)
  end
  
end