class HarvestWheelman::HarvestSubmit

Constants

DATEFORMAT

Attributes

from[W]
harvest[R]
to[W]

Public Class Methods

new(settings_file) click to toggle source
# File lib/harvest_submit.rb, line 36
def initialize(settings_file)
  File.open(settings_file) do |f|
    @settings = JSON.parse(f.read)
  end
  if not @settings['pay_period']
    @ppw = 2
    @to = date_to_string Date.nearest_sunday
  end
  determine_time_interval!
end

Public Instance Methods

body() click to toggle source
# File lib/harvest_submit.rb, line 117
    def body
      <<-EOF
Hello,
Attached is my timesheet for pay period #{from} - #{to}

Thank you,
#{whoami}

Generated using http://rubygems.org/gems/harvest_wheelman
Version #{HarvestWheelman::VERSION}
      EOF
    end
date_to_string(input) click to toggle source
# File lib/harvest_submit.rb, line 72
def date_to_string(input)
  input.strftime(DATEFORMAT)
end
date_valid?(input) click to toggle source
# File lib/harvest_submit.rb, line 76
def date_valid?(input)
  return false unless input
  begin
    string_to_date input
  rescue ArgumentError
    false
  end
end
determine_time_interval!() click to toggle source
# File lib/harvest_submit.rb, line 53
def determine_time_interval!
  if date_valid? from
    if date_valid? to
    else
      puts "Setting TO via the FROM based on a #{ppw} week pay period"
      @to = date_to_string(1.day.before(ppw.weeks.after(string_to_date(from))))
    end
  elsif date_valid? to
    puts "Setting FROM via the TO based on a #{ppw} week pay period"
    @from = date_to_string(1.day.after(ppw.weeks.before(string_to_date(to))))
  else
    raise UnknownTimeIntervalError
  end
end
drive_to_pdf() click to toggle source
# File lib/harvest_submit.rb, line 130
    def drive_to_pdf
      begin
        puts "Starting chromedriver"
        @driver = Selenium::WebDriver.for :chrome
        @base_url = "https://#{site}.harvestapp.com/"
        puts "Driving to #{@base_url}"
        @driver.manage.timeouts.implicit_wait = 30
        @verification_errors = []
        @driver.get(@base_url + "/account/login")
        puts "Signing in as #{user}"
        @driver.find_element(:id, "email").clear
        @driver.find_element(:id, "email").send_keys user
        @driver.find_element(:id, "user_password").clear
        @driver.find_element(:id, "user_password").send_keys pass
        @driver.find_element(:id, "sign-in-button").click
        puts "Heading to the Reports area"
        @driver.find_element(:link, "Reports").click
        @driver.find_element(:css, "span.btn-dropdown").click
        @driver.find_element(:link, "Custom").click

        puts "Entering custom dates"
        @driver.find_element(:id, "start_date").clear
        @driver.find_element(:id, "start_date").send_keys from
        @driver.find_element(:id, "end_date").clear
        @driver.find_element(:id, "end_date").send_keys to

        puts "Requesting detailed report"
        @driver.find_element(:link, "Update Report").click
        @driver.find_element(:link, "Detailed Report").click
        @driver.find_elements(:tag_name => 'option').find{|i| i.text == 'Task'}.click
        
        puts "Print as PDF. Filename:\n#{pdf_name}"
        if pbcopy pdf_name
          puts "Filename is currently in your pasteboard for file save dialog..."
        end
        @driver.execute_script('window.print()')
        @driver.find_element(:xpath, "//nav[@id='nav']/div/ul/li[4]/a").click
        @driver.find_element(:link, "Sign Out").click
        puts "Email your PDF."
        pbcopy subject
        puts <<-EOF
        -------------------------------
#{subject}


#{body}
        -------------------------------
        EOF
      rescue Exception => ex
puts <<-EOF
Error: #{ex.message}
------------------
#{ex.backtrace.join("\n")} 
EOF
      ensure
        @driver.quit
      end
    end
from() click to toggle source
# File lib/harvest_submit.rb, line 85
def from
  @from ||= @settings['pay_period']['from'] rescue nil
end
pass() click to toggle source
# File lib/harvest_submit.rb, line 101
def pass
  @settings['harvest']['pass']
end
pdf_name() click to toggle source
# File lib/harvest_submit.rb, line 109
def pdf_name
  "#{subject}.pdf"
end
ppw() click to toggle source

Pay period weeks

# File lib/harvest_submit.rb, line 49
def ppw
  @ppw ||= @settings['pay_period']['weeks'] rescue 2
end
site() click to toggle source
# File lib/harvest_submit.rb, line 93
def site
  @settings["harvest"]["site"]
end
string_to_date(input) click to toggle source
# File lib/harvest_submit.rb, line 68
def string_to_date(input)
  Date.strptime(input, DATEFORMAT)
end
subject() click to toggle source
# File lib/harvest_submit.rb, line 113
def subject
  "#{whoami}'s Timesheet #{from} - #{to}"
end
to() click to toggle source
# File lib/harvest_submit.rb, line 89
def to
  @to ||= @settings['pay_period']['to'] rescue nil
end
user() click to toggle source
# File lib/harvest_submit.rb, line 97
def user
  @settings['harvest']['user']
end
whoami() click to toggle source
# File lib/harvest_submit.rb, line 105
def whoami
  `whoami`.strip.capitalize
end