Class: Gigawatt::Commands::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/gigawatt/commands/log.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, options) ⇒ Log

Returns a new instance of Log.



31
32
33
34
35
36
37
38
# File 'lib/gigawatt/commands/log.rb', line 31

def initialize(settings, options)
  @settings = settings
  @options = options

  @access_key = OAuth.token(@settings.access_key)
  @cache = Cache.new(settings, @access_key)
  @project = Gigawatt::ProjectFile.new.project
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/gigawatt/commands/log.rb', line 4

def options
  @options
end

Class Method Details

.run!(settings) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gigawatt/commands/log.rb', line 6

def self.run!(settings)
  options = Trollop::options do
    banner <<-EOS
88 Miles Command line application - http://88miles.net

Print out the shifts for the linked project

Usage:
  88miles log [options]

options
    EOS
    opt :page, "Page the output", :type => :flag, :default => true
    opt :table, "Outputs a formatted table", :type => :flag, :default => false
  end

  instance = self.new(settings, options)
  begin
    return instance.log
  rescue OAuth2::Error => e
    say "Access to your 88 Miles may have been revoked. Please run <%= color('88miles setup', BOLD) %> again."
    return INVALID_OAUTH_TOKEN_EXIT_CODE
  end
end

Instance Method Details

#logObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gigawatt/commands/log.rb', line 40

def log
  unless @project
    say("No project found.")
    return NO_PROJECT_EXIT_CODE
  end

  $terminal.page_at = :auto if @options[:page]

  if @options[:table]
    log_table
  else
    log_blob
  end
  return OK_EXIT_CODE
end

#log_blobObject



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
# File 'lib/gigawatt/commands/log.rb', line 56

def log_blob
  buffer = ''
  shifts = JSON.parse(@access_key.get("/api/1/projects/#{@project["uuid"]}/shifts.json").body)

  company = @cache.companies(true)[@project["company_uuid"]]
  staff = @cache.staff(true)
  buffer += "#{company["name"]}: #{@project["name"]} shifts:\n\n"

  total = 0
  rows = []
  str = ""
  shifts["response"].each do |shift|
    staff_member = staff[shift["user_uuid"]]
    start = Time.parse(shift["start"])
    stop = Time.parse(shift["stop"] || Time.now.to_s)
    total += (stop - start)

    str += "<%= color('uuid #{shift["uuid"]}', YELLOW) %>\n"

    if staff_member
      str += "Staff:  #{staff_member["first_name"]} #{staff_member["last_name"]} <#{staff_member["email_address"]}>\n"
    else
      str += "Staff:  [Deleted user]\n"
    end

    str += "Start:  #{start.getlocal.strftime('%c %:z')}\n"
    str += "Stop:   #{stop.getlocal.strftime('%c %:z')}\n"
    str += "Total:  #{to_clock_s(stop - start)}\n"
    str += "\n\t"
    if shift["notes"].to_s == ""
      str += "No notes\n"
    else
      str += "#{shift["notes"]}\n"
    end
    str += "\n"
  end

  overdue = @project["grand_total"] > @project["time_limit"] if @project["time_limit"]
  if overdue
    str += "Total: #{HighLine::String.new(to_clock_s(total)).red}"
  else
    str += "Total: #{to_clock_s(total)}"
  end

  say(str)
end

#log_tableObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gigawatt/commands/log.rb', line 103

def log_table
  buffer = ''
  shifts = JSON.parse(@access_key.get("/api/1/projects/#{@project["uuid"]}/shifts.json").body)

  company = @cache.companies(true)[@project["company_uuid"]]
  staff = @cache.staff(true)
  buffer += "#{company["name"]}: #{@project["name"]} shifts:\n\n"

  total = 0
  rows = []
  shifts["response"].each do |shift|
    row = []

    staff_member = staff[shift["user_uuid"]]
    start = Time.parse(shift["start"])
    stop = Time.parse(shift["stop"] || Time.now.to_s)
    total += (stop - start)

    if staff_member
      row << "#{staff_member["first_name"]} #{staff_member["last_name"]}"
    else
      row << "[Deleted user]"
    end
    row << "#{start.getlocal.strftime("%d/%M/%Y %H:%M:%S")} - #{stop.getlocal.strftime("%H:%M:%S")}"
    row << "#{to_clock_s(stop - start)}"

    if shift["notes"].to_s == ""
      row << " No notes"
    else
      row << " #{shift["notes"]}"
    end

    if shift["stop"].nil?
      row = row.map{ |col| HighLine::String.new(col).green }
    end
    rows << row
  end

  rows << :separator
  overdue = @project["grand_total"] > @project["time_limit"] if @project["time_limit"]
  if overdue
    rows << [ 'Total', '', HighLine::String.new(to_clock_s(total)).red, '' ]
  else
    rows << [ 'Total', '', to_clock_s(total), '' ]
  end

  say(Terminal::Table.new(:rows => rows).to_s) if @options[:table]
end

#to_clock_s(time, show_seconds = false) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/gigawatt/commands/log.rb', line 152

def to_clock_s(time, show_seconds = false)
  hour = (time.abs / 3600).floor
  minute = (time.abs / 60 % 60).floor
  seconds = (time.abs % 60).floor if show_seconds

  return (time != 0 && (time / time.abs) == -1 ? "-" : "") + hour.to_s.rjust(2, '0') + ":" + minute.to_s.rjust(2, '0') + (show_seconds ? ":" + seconds.to_s.rjust(2, '0') : '')
end