class AdopsReportScrapper::AdtechusClient

Public Instance Methods

init_client() click to toggle source
# File lib/adops_report_scrapper/adtechus_client.rb, line 5
def init_client
  Capybara.register_driver :selenium do |app|
    Capybara::Selenium::Driver.new(app, :browser => :firefox)
  end
  @client = Capybara::Session.new(:selenium)
end

Private Instance Methods

extract_data_from_report() click to toggle source
# File lib/adops_report_scrapper/adtechus_client.rb, line 48
def extract_data_from_report
  rows = @client.find_all :xpath, '//table[@class="table"]/tbody/tr'
  rows = rows.to_a
  rows.pop
  @data = rows.map { |tr| tr.find_css('td,th').map { |td| td.visible_text } }
end
login() click to toggle source
# File lib/adops_report_scrapper/adtechus_client.rb, line 14
def login
  @client.visit 'http://marketplace.adtechus.com'
  @client.fill_in 'Username', :with => @login
  @client.find_all(:button).first.click
  sleep 10
  @client.fill_in 'Password', :with => @secret
  @client.find_all(:button).first.click
  sleep 10
  begin
    @client.find :xpath, '//*[text()="REPORTING"]'
  rescue Exception => e
    raise e, 'Adtechus login error'
  end
end
request_report() click to toggle source
# File lib/adops_report_scrapper/adtechus_client.rb, line 33
def request_report
  @client.find(:xpath, '//*[text()="REPORTING"]').click
  wait_for_loading
  @client.visit(@client.find(:css, '#mainwindow')[:src])
  wait_for_loading
  report_id = @client.find_all(:xpath, '//tr[./td/div/span[text()="Placement fill rate report"]]')[-1][:id]
  report_id = report_id.tr 'row_', ''
  @client.visit "https://console.onedisplaymp.aol.com/h2/reporting/showReport.do?action=showreportpage._._.#{report_id}"
  @client.within_frame @client.find(:css, '#reportwindow') do
    @client.within_frame @client.find(:xpath, '//iframe[@name="uid_2"]') do
      extract_data_from_report
    end
  end
end
scrap() click to toggle source
# File lib/adops_report_scrapper/adtechus_client.rb, line 29
def scrap
  request_report
end
wait_for_loading() click to toggle source
# File lib/adops_report_scrapper/adtechus_client.rb, line 55
def wait_for_loading
  18.times do |_i| # wait 3 min
    begin
      @client.find(:xpath, '//*[contains(text(),"loading")]')
    rescue Exception => e
      break
    end
    sleep 3
  end
  sleep 1
end