Class: TimeCop::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/time_cop/runner.rb

Class Method Summary collapse

Class Method Details

.invokeObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/time_cop/runner.rb', line 7

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