Class: AdopsReportScrapper::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/adops_report_scrapper/base_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(login, secret, options = nil) ⇒ BaseClient

login: username, id, email, or api token secret: password or api secret options:

:date => (optional) default: yesterday



12
13
14
15
16
17
# File 'lib/adops_report_scrapper/base_client.rb', line 12

def initialize(, secret, options = nil)
  @login = 
  @secret = secret
  @options = options || {}
  @date = @options[:date] || Date.today.prev_day
end

Instance Method Details

#before_quit_with_errorObject



66
67
68
# File 'lib/adops_report_scrapper/base_client.rb', line 66

def before_quit_with_error
  @client.save_screenshot
end

#date_supported?(date = nil) ⇒ Boolean

by default only support yesterday

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/adops_report_scrapper/base_client.rb', line 71

def date_supported?(date = nil)
  _date = date || @date
  return true if _date == Date.today.prev_day
  false
end

#get_data(date = nil, options = nil) ⇒ Object

date: (optional) return data in array of array, first array is the headers, no total included



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/adops_report_scrapper/base_client.rb', line 21

def get_data(date = nil, options = nil)
  @date = date if date
  @options = @options.merge(options || {})
  fail "specified date is not supported by this scrapper #{self.class.name}" unless date_supported?
  init_client
  
  begin
    scrap
  rescue Exception => e
    begin
      before_quit_with_error
      logout
    rescue Exception => _e
      # do nothing
    end
    raise e
  end
  logout
  return @data
end

#init_clientObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/adops_report_scrapper/base_client.rb', line 42

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

#loginObject



53
54
55
# File 'lib/adops_report_scrapper/base_client.rb', line 53

def 
  # do nothing by default
end

#logoutObject

logout can be optional



62
63
64
# File 'lib/adops_report_scrapper/base_client.rb', line 62

def logout
  # do nothing by default
end

#scrapObject



57
58
59
# File 'lib/adops_report_scrapper/base_client.rb', line 57

def scrap
  # do nothing by default
end