Sha256: bba5edf4b27ab5223640c33462be48cc62ac24bbc1813559038bac255b03f4d1

Contents?: true

Size: 1.44 KB

Versions: 31

Compression:

Stored size: 1.44 KB

Contents

require 'date'
require_relative 'base_client'
require 'open-uri'

class AdopsReportScrapper::BrowsiClient < AdopsReportScrapper::BaseClient
  def date_supported?(date = nil)
    _date = date || @date
    return true if _date >= Date.today - 30
    false
  end

  private

  def login
    @client.visit 'https://reports.brow.si'
    @client.fill_in 'Email', :with => @login
    @client.fill_in 'Password', :with => @secret
    @client.find(:xpath, '//*[text()="Login"]').click
    begin
      @client.find :css, '.ico-calendar'
    rescue Exception => e
      raise e, 'Browsi login error'
    end
  end

  def scrap
    request_report
    extract_data_from_report
  end

  def request_report
    date_range_obj = {
      startDate: @date.to_time.utc.strftime('%FT%T.000Z'),
      endDate: (@date.to_time+86400-1).utc.strftime('%FT%T.999Z')
    }
    @client.visit "https://reports.brow.si/client/app/index.html#/report/home?dateRange=#{URI::encode(date_range_obj.to_json)}"
    sleep 10
  end

  def extract_data_from_report
    rows = @client.find_all :xpath, '//li[@ng-class="{opened:site.isGraphOpen}"]'
    @data = [['Site', 'Revenues', 'Page Views']]
    @data += rows.map do |row|
      site = row.find_css('.header-full').first.visible_text
      rev = @client.find_all(:xpath, row.path+'//*[../span[text()="Revenues"]]').first.text
      pv = @client.find_all(:xpath, row.path+'//*[../span[text()="Page Views"]]').first.text
      [site, rev, pv]
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
adops_report_scrapper-0.1.41 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.40 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.38 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.37 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.36 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.35 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.34 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.33 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.32 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.31 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.30 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.29 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.28 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.27 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.26 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.25 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.24 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.23 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.22 lib/adops_report_scrapper/browsi_client.rb
adops_report_scrapper-0.1.21 lib/adops_report_scrapper/browsi_client.rb