require 'time_cop/option_parser' require 'highline' require 'active_support/core_ext/date_and_time/calculations' module TimeCop class Runner def self.invoke options_parser = OptionParser.new(ARGV) options = options_parser.parse interactive_hash = {} if (options[:interactive]) cli = HighLine.new username = cli.ask('Harvest Username: ') password = cli.ask('Harvest Password: ') { |q| q.echo = false } interactive_hash[:hours_per_week] = cli.ask('Hours per week? (Full Time 34)') { |q| q.default = 34 }.to_f accountability_options = { username: (username.nil? ? options[:username] : username), password: (password.nil? ? options[:password] : password), email: options[:email] } q_dates = [ Date.new(Date.today.year, 1, 1), Date.new(Date.today.year, 4, 1), Date.new(Date.today.year, 7, 1), Date.new(Date.today.year, 10, 1) ] cli.choose do |menu| menu.prompt = 'Which Quarter? ' menu.choice(:Q1) do interactive_hash[:date] = q_dates[0] Accountability.new(interactive_hash.merge(accountability_options)).print_report end menu.choice(:Q2) do interactive_hash[:date] = q_dates[1] Accountability.new(interactive_hash.merge(accountability_options)).print_report end menu.choice(:Q3) do interactive_hash[:date] = q_dates[2] Accountability.new(interactive_hash.merge(accountability_options)).print_report end menu.choice(:Q4) do interactive_hash[:date] = q_dates[3] Accountability.new(interactive_hash.merge(accountability_options)).print_report end menu.choice(:Year) do summary = q_dates.map do |d| interactive_hash[:date] = d Accountability.new(interactive_hash.merge(accountability_options)).summary_hash end logged = summary.inject(0){|sum, s| sum + s[:hours][:charged]} needed = summary.inject(0){|sum, s| sum + s[:hours][:needed]}.round(2 ) diff = (logged - needed).round(2) weekdays = (Date.today..Date.new(Date.today.year, 12, 31)).select{|d| (1..5).include?(d.wday)}.size weekdays_so_far = (Date.new(Date.today.year, 1, 1)..Date.today).select{|d| (1..5).include?(d.wday)}.size average_needed = (-1 * diff / weekdays).round(2) average_clocked = (logged / weekdays_so_far).round(2) projected_diff = ((logged + (weekdays * average_clocked)) - needed).round(2) puts "Business Days Left In Year: #{weekdays}" puts "Current Year End Surplus(+)/Deficit(-): #{diff}" puts "Projected Year End Surplus(+)/Deficit(-): #{projected_diff}" puts "" puts "Total Harvest Hours This Year: #{logged}" puts "Average Harvest Hours Per Business Day: #{average_clocked}" puts "" puts "Total Hours Needed By End Of Year: #{needed}" puts "Average Hours Per Business Day Needed To Reach Goal: #{average_needed}" end end else accountability_options = { username: (options[:username]), password: (options[:password]), email: options[:email] } accountability = Accountability.new( accountability_options ) accountability.print_report end rescue Harvest::AuthenticationFailed puts 'Unable to authenticate to Harvest. Check username/password.' rescue Harvest::HTTPError => e puts 'Harvest API Error' puts e.to_s end end end