Class: HarvestReport::Runner

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/harvest_report/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner



15
16
17
18
19
20
21
22
23
# File 'lib/harvest_report/runner.rb', line 15

def initialize(args)
  @options = {
    :download_dir     => '/tmp/harvest_report',
    :download_timeout => 60
  }

  parse_options!(args)
  validate_options!
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/harvest_report/runner.rb', line 25

def options
  @options
end

Class Method Details

.run(*args) ⇒ Object



11
12
13
# File 'lib/harvest_report/runner.rb', line 11

def self.run(*args)
  new(ARGV).run
end

Instance Method Details

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/harvest_report/runner.rb', line 27

def run
  Capybara.register_driver :chrome do |app|
    profile = ::Selenium::WebDriver::Chrome::Profile.new
    profile["download.default_directory"] = options[:download_dir]
    Capybara::Selenium::Driver.new(app, :browser => :chrome, :profile => profile)
  end
  Capybara.current_driver = :chrome
  Capybara.reset_sessions!

  page.visit "https://#{options[:domain]}.harvestapp.com/account/login"
  page.click_link "Or sign in with your Harvest account"

  page.fill_in "Email", :with => options[:email]
  page.fill_in "Password", :with => options[:password]
  page.click_button "Sign In"

  element = page.find_link("My Profile")
  user_id = element[:href].scan(/\d+/).first

  export_url = "https://#{options[:domain]}.harvestapp.com/xlsx/export/user/#{user_id}"
  export_url << "?"
  export_url << "start_date=#{CGI.escape(options[:start_date])}&"
  export_url << "end_date=#{CGI.escape(options[:stop_date])}"

  page.visit export_url

  wait_for_download

  puts "Report downloaded to #{options[:download_dir]}"

  sleep 1
end