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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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 = 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 = {
      harvest_client: Harvest.client(
        username: (username.nil? ? options[:username] : username),
        password: (password.nil? ? options[:password] : password),
        subdomain: 'wildland'
      ),
      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
        puts ""
        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)
        puts "Total Hours for #{year}: #{logged}"
        puts "Average Hours Per Business Day: #{average_clocked}"
        if (year == Date.today.year)
          puts "Total Hours Needed By End Of Year: #{needed}"
          puts ""
          puts "Business Days Left In Year: #{weekdays}"
          puts "Average Hours Per Business Day Needed To Reach Goal: #{average_needed}"
          puts ""
          puts "Current Year End Surplus(+)/Deficit(-): #{Accountability.paint_number(diff)}"
          puts "Projected Year End Surplus(+)/Deficit(-): #{Accountability.paint_number(projected_diff)}"
        else
          puts "#{year} Surplus(+)/Deficit(-): #{Accountability.paint_number(diff)}"
        end
      end
    end
  else
    accountability_options = {
      harvest_client: Harvest.client(username: options[:username], password: options[:password], subdomain: 'wildland'),
      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