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 = ENV['HARVEST_USERNAME'].nil? ? cli.ask('Harvest Username: ') : ENV['HARVEST_USERNAME'] password = ENV['HARVEST_PASSWORD'].nil? ? cli.ask('Harvest Password: ') { |q| q.echo = false } : ENV['HARVEST_PASSWORD'] interactive_hash[:hours_per_week] = ENV['HARVEST_HOURS_PER_WEEK'].nil? ? cli.ask('Hours per week? Defaults to Full Time (34)') { |q| q.default = 34 }.to_f : ENV['HARVEST_HOURS_PER_WEEK'].to_f year = cli.ask("Year? Defaults to #{Date.today.year}") { |q| q.default = Date.today.year }.to_i accountability_options = { username: (username.nil? ? options[:username] : username), password: (password.nil? ? options[:password] : password), email: options[:email] } q_dates = [ { range: Date.new(year, 1, 1)...Date.new(year, 3, 31), choice: :Q1 }, { range: Date.new(year, 4, 1)...Date.new(year, 6, 30), choice: :Q2 }, { range: Date.new(year, 7, 1)...Date.new(year, 9, 30), choice: :Q3 }, { range: Date.new(year, 10, 1)...Date.new(year, 12, 31), choice: :Q4 } ] default_quarter = q_dates.find{|q_date| q_date[:range].include?(Date.today)} default_choice = default_quarter.nil? ? '' : "Defaults to #{default_quarter[:choice]}" cli.choose do |menu| menu.prompt = "Which Quarter? #{default_choice}" unless default_quarter.nil? menu.default = default_quarter[:choice] end q_dates.each do |q_date| menu.choice(q_date[:choice]) do interactive_hash[:date] = q_date[:range].begin puts 'Fetching data...' Accountability.new(interactive_hash.merge(accountability_options)).print_report end end menu.choice(:Year) do summary = q_dates.map do |q_date| interactive_hash[:date] = q_date[:range].begin puts "Fetching data for #{q_date[:choice]}..." 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(year, 12, 31)).select{|d| (1..5).include?(d.wday)}.size weekdays_so_far = (Date.new(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) if (year == Date.today.year) puts "Business Days Left In Year: #{weekdays}" puts "Current Year End Surplus(+)/Deficit(-): #{diff}" puts "Projected Year End Surplus(+)/Deficit(-): #{projected_diff}" puts "" else puts "#{year} Surplus(+)/Deficit(-): #{diff}" end puts "Total Harvest Hours for #{year}: #{logged}" puts "Average Harvest Hours Per Business Day: #{average_clocked}" if (year == Date.today.year) puts "" puts "Total Hours Needed By End Of Year: #{needed}" puts "Average Hours Per Business Day Needed To Reach Goal: #{average_needed}" end 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