class AdopsReportScrapper::SovrnClient

Public Instance Methods

date_supported?(date = nil) click to toggle source
# File lib/adops_report_scrapper/sovrn_client.rb, line 6
def date_supported?(date = nil)
  _date = date || @date
  return true if _date >= Date.today - 7
  false
end
init_client() click to toggle source
# File lib/adops_report_scrapper/sovrn_client.rb, line 12
def init_client
  Capybara.register_driver :selenium do |app|
    profile = Selenium::WebDriver::Firefox::Profile.new
    @download_dir = '/tmp/sovrn'
    profile['browser.download.dir'] = @download_dir
    clean_up_download_dir
    profile['browser.download.folderList'] = 2
    profile['browser.helperApps.neverAsk.saveToDisk'] = 'application/octet-stream'
    Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
  end
  @client = Capybara::Session.new(:selenium)
end

Private Instance Methods

clean_up_download_dir() click to toggle source
# File lib/adops_report_scrapper/sovrn_client.rb, line 89
def clean_up_download_dir
  FileUtils.rm_rf(Dir.glob("#{@download_dir}/*"))
end
extract_data_from_report() click to toggle source
# File lib/adops_report_scrapper/sovrn_client.rb, line 84
def extract_data_from_report
  rows = CSV.parse File.read("#{@download_dir}/adstats_all_traffic.csv")
  @data = rows[6..-1]
end
login() click to toggle source
# File lib/adops_report_scrapper/sovrn_client.rb, line 27
def login
  @client.visit 'https://meridian.sovrn.com/#welcome'
  sleep 1
  if @client.find_all(:css, '#user-menu-trigger').count > 0
    @client.find_all(:css, '#user-menu-trigger').first.click
    sleep 1
    @client.find(:xpath, '//li[@data-value="logout"]').click
  end
  if @client.find_all(:xpath, '//*[contains(text(),"Go to Login")]').count > 0
    @client.find(:xpath, '//*[contains(text(),"Go to Login")]').click
    sleep 1
  end
  @client.fill_in 'login_username', :with => @login
  @client.fill_in 'login_password', :with => @secret
  @client.click_button 'Log In'
  sleep 5

  begin
    @client.find :xpath, '//*[text()="Account"]'
  rescue Exception => e
    raise e, 'sovrn login error'
  end
end
request_report() click to toggle source
# File lib/adops_report_scrapper/sovrn_client.rb, line 56
def request_report
  @client.visit 'https://meridian.sovrn.com/#account/my_downloads'
  sleep 5
  if @client.find_all(:xpath, '//input[@value="domestic_and_international"]').count == 0
    login
    @client.visit 'https://meridian.sovrn.com/#account/my_downloads'
    sleep 5
  end

  @client.fill_in 'adstats-date-range-start-month', :with => @date.strftime('%m')
  @client.find(:xpath, '//input[@value="domestic_and_international"]').set(true)
  @client.fill_in 'adstats-date-range-start-day', :with => @date.strftime('%d')
  @client.find(:xpath, '//input[@value="domestic_and_international"]').set(true)
  @client.fill_in 'adstats-date-range-start-year', :with => @date.strftime('%Y')
  @client.find(:xpath, '//input[@value="domestic_and_international"]').set(true)

  @client.fill_in 'adstats-date-range-end-month', :with => @date.strftime('%m')
  @client.find(:xpath, '//input[@value="domestic_and_international"]').set(true)
  @client.fill_in 'adstats-date-range-end-day', :with => @date.strftime('%d')
  @client.find(:xpath, '//input[@value="domestic_and_international"]').set(true)
  @client.fill_in 'adstats-date-range-end-year', :with => @date.strftime('%Y')
  @client.find(:xpath, '//input[@value="domestic_and_international"]').set(true)

  @client.find_all(:xpath, '//button[text()=" Download "]').first.click

  sleep 2
end
scrap() click to toggle source
# File lib/adops_report_scrapper/sovrn_client.rb, line 51
def scrap
  request_report
  extract_data_from_report
end