class AdopsReportScrapper::AdsparcClient

Public Instance Methods

date_supported?(date = nil) click to toggle source
# File lib/adops_report_scrapper/adsparc_client.rb, line 5
def date_supported?(date = nil)
  _date = date || @date
  return true if _date >= Date.today - 2
  false
end

Private Instance Methods

extract_data_from_report() click to toggle source
# File lib/adops_report_scrapper/adsparc_client.rb, line 47
def extract_data_from_report
  header = @client.find(:xpath, '//*[contains(@class, "widget-item") and .//*[text()="Publisher Earnings Report - by Day"]]//table/thead/tr').find_css('td,th').map { |td| td.visible_text }
  @data = [header]
  date_str = @date.strftime '%d %b %Y'
  rows = @client.find_all(:xpath, "//*[contains(@class, \"widget-item\") and .//*[text()=\"Publisher Earnings Report - by Day\"]]//table/tbody/tr[./td[text()=\"#{date_str}\"]]")
  rows = rows.to_a
  @data.concat(rows.map { |tr| tr.find_css('td,th').map { |td| td.visible_text } })
end
login() click to toggle source
# File lib/adops_report_scrapper/adsparc_client.rb, line 13
def login
  @client.visit 'http://publisher.adsparc.com/login.php'
  @client.fill_in 'Email', :with => @login
  @client.fill_in 'Password', :with => @secret
  @client.click_button 'Login'
  begin
    @client.find :xpath, '//*[text()="Logout"]'
  rescue Exception => e
    raise e, 'Adsparc login error'
  end
end
request_report() click to toggle source
# File lib/adops_report_scrapper/adsparc_client.rb, line 30
def request_report
  sleep 5
  url = @client.find(:css, '#iframe').base.attributes['src']
  @client.visit url
  sleep 5
  @client.find(:css, '#topCalendar').click
  begin
    @client.find(:xpath, '//*[text()="Month to date "]').click
  rescue Exception => e
    @client.find(:xpath, '//*[text()="Custom Range "]').click
  end
  @client.find(:xpath, '//*[text()="Last 7 Days"]').click
  @client.click_button 'Apply'
  sleep 0.5
  wait_for_loading
end
scrap() click to toggle source
# File lib/adops_report_scrapper/adsparc_client.rb, line 25
def scrap
  request_report
  extract_data_from_report
end
wait_for_loading() click to toggle source
# File lib/adops_report_scrapper/adsparc_client.rb, line 56
def wait_for_loading
  30.times do |_i| # wait 5 min
    begin
      @client.find(:xpath, '//*[text()="Day"]')
      break
    rescue Exception => e
      sleep 10
    end
  end
end