class AdopsReportScrapper::FacebookaudienceClient

Private Instance Methods

extract_data_from_report(placement) click to toggle source
# File lib/adops_report_scrapper/facebookaudience_client.rb, line 45
def extract_data_from_report(placement)
  if @data.count == 0
    header = @client.find :xpath, '//table/thead/tr'
    @data << ['Placement'] + header.find_css('td,th').map { |td| td.visible_text }
  end
  data_str = @date.strftime '%a %b %d, %Y'
  rows = @client.find_all :xpath, "//table/*/tr[./td[text()=\"#{data_str}\"]]"
  return if rows.count == 0
  row = rows.first
  @data << [placement] + row.find_css('td,th').map { |td| td.visible_text }
end
init_client() click to toggle source
# File lib/adops_report_scrapper/facebookaudience_client.rb, line 7
def init_client
  fail 'please specify facebook app id' unless @options['app_id']
  @app_id = @options['app_id']
  super
end
login() click to toggle source
# File lib/adops_report_scrapper/facebookaudience_client.rb, line 13
def login
  @client.visit "https://developers.facebook.com/apps/#{@app_id}/audience-network/placement"
  @client.fill_in 'email', :with => @login
  @client.fill_in 'pass', :with => @secret
  @client.click_button 'Log In'
  begin
    @client.find :xpath, '//*[text()="Dashboard"]'
  rescue Exception => e
    raise e, 'Facebookaudience login error'
  end
end
request_report(placement) click to toggle source
# File lib/adops_report_scrapper/facebookaudience_client.rb, line 38
def request_report(placement)
  @client.find(:xpath, "//*[text()=\"#{@prev_placement}\"]").click
  @client.find(:xpath, "//*[text()=\"#{placement}\"]").click
  @prev_placement = placement.match(/^(.+) \(\d+\)/).captures[0]
  sleep 1
end
scrap() click to toggle source
# File lib/adops_report_scrapper/facebookaudience_client.rb, line 25
def scrap
  @client.find(:xpath, '//*[text()="All Ad Placements"]').click
  placements = @client.find_all(:xpath, '//span[../../a[@role="menuitem"]]')
  placements = placements.map(&:text)
  @client.find(:xpath, '//*[text()="All Ad Placements"]').click
  @prev_placement = 'All Ad Placements'
  @data = []
  placements.each do |placement|
    request_report placement
    extract_data_from_report placement
  end
end