Sha256: 8651ea3e610dbab28338eea3245ad4bd32a3852413996b254910b14a4d6dbbc5

Contents?: true

Size: 1.64 KB

Versions: 59

Compression:

Stored size: 1.64 KB

Contents

require 'date'
require 'phantomjs'
require 'capybara'
require 'capybara/poltergeist'

class AdopsReportScrapper::BaseClient
  # login: username, id, email, or api token
  # secret: password or api secret
  # options: {
  #   :date => (optional) default: yesterday
  # }
  def initialize(login, secret, options = nil)
    @login = login
    @secret = secret
    @options = options || {}
    @date = @options[:date] || Date.today.prev_day
  end

  # date: (optional)
  # return data in array of array, first array is the headers, no total included
  def get_data(date = nil)
    @date = date if date
    fail "specified date is not supported by this scrapper #{self.class.name}" unless date_supported?
    init_client
    login
    begin
      scrap
    rescue Exception => e
      begin
        before_quit_with_error
        logout
      rescue Exception => _e
        # do nothing
      end
      raise e
    end
    logout
    return @data
  end

  def init_client
    Capybara.register_driver :poltergeist do |app|
      Capybara::Poltergeist::Driver.new(app, :phantomjs => Phantomjs.path)
    end
    Capybara.default_max_wait_time = 10
    @client = Capybara::Session.new(:poltergeist)
    @client.driver.browser.js_errors = false
    @client.driver.resize(1920, 985)
  end

  def login
    # do nothing by default
  end

  def scrap
    # do nothing by default
  end

  # logout can be optional
  def logout
    # do nothing by default
  end

  def before_quit_with_error
    @client.save_screenshot
  end

  # by default only support yesterday
  def date_supported?(date = nil)
    _date = date || @date
    return true if _date == Date.today.prev_day
    false
  end
end

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
adops_report_scrapper-0.1.18 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.17 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.16 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.15 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.14 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.13 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.12 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.11 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.10 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.9 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.8 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.7 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.6 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.5 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.4 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.3 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.2 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.1 lib/adops_report_scrapper/base_client.rb
adops_report_scrapper-0.1.0 lib/adops_report_scrapper/base_client.rb