Class: Milton

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

Overview

Milton fills out timesheets for the ADP ezLaborManager.

Constant Summary collapse

VERSION =
'1.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Milton

Returns a new instance of Milton.

Yields:

  • (_self)

Yield Parameters:

  • _self (Milton)

    the object that the method was called on



90
91
92
93
94
95
# File 'lib/milton.rb', line 90

def initialize &block
  @agent = WWW::Mechanize.new
  @page = nil
  @username = nil
  yield self if block_given?
end

Class Method Details

.load_config(config_file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/milton.rb', line 17

def self.load_config config_file
  unless File.exist? config_file then
    open config_file, 'wb' do |f|
      f.write YAML.dump({
        'client_name' => 'Your client name',
        'username'    => 'Your username',
        'password'    => 'Your password',
      })
    end

    raise "Please fill out #{config_file}. We've created a template there for you to edit."
  end

  YAML.load_file config_file
end

.parse_args(argv) ⇒ Object



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
# File 'lib/milton.rb', line 33

def self.parse_args argv
  options = {
    'date' => nil,
    'view' => false
  }

  opts = OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = Milton::VERSION
    opt.release = nil
    opt.banner = <<-EOF
Usage: #{opt.program_name} [options]

Milton fills out your ADP timesheet for you.  By default it fills it out for
the current week with eight hours/day.
    EOF

    opt.separator nil

    opt.on('--view',
           'Only view your current timesheet') do |value|
      options['view'] = value
    end

    opt.on('--date=DATE', Date,
           'Select week by day') do |value|
      options['date'] = value
    end

    opt.on('--month [DATE]', Date, 'Select month by day') do |value|
      options['view'] = true  # For your safety, you can only view months
      options['month'] = value || Date.today
    end

    opt.on('--fuck-the-man',
           'Do not include lunch in your timesheet') do |value|
      options['rows_per_day'] = 1
    end
  end

  opts.parse! argv

  options
end

.run(argv = ARGV) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/milton.rb', line 78

def self.run argv = ARGV
  config_file = File.join Gem.user_home, '.milton'

  options = parse_args argv

  config = load_config config_file

  options.merge! config

  new.run options
end

Instance Method Details

#client_name=(name) ⇒ Object

Sets the client name name



100
101
102
103
104
105
106
107
108
109
# File 'lib/milton.rb', line 100

def client_name= name
  page = @agent.get('http://workforceportal.elabor.com/ezLaborManagerNetRedirect/clientlogin.aspx')
  @page = page.form('ClientLoginForm') { |form|
    form.txtClientName = name
    form.hdnTimeZone = 'Pacific Standard Time'
    form['__EVENTTARGET'] = 'btnSubmit'
  }.submit
  @page = @page.form_with(:action => /ezlmportaldc2.adp.com/).submit
  @page = @page.form_with(:action => /adp\.com/).submit
end

#extract_timesheetObject

Prints out your timesheet for the selected time frame



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/milton.rb', line 221

def extract_timesheet
  timesheet = parse_timesheet

  department  = timesheet.first[6]
  employee_id = timesheet.first[7]

  puts "Employee #{@username} id #{employee_id}, department #{department}"

  puts "-" * 80

  date      = nil
  last_date = nil

  timesheet.each do |row|
    date = row[2]

    if row[0] == '0' and date == last_date then
      # do nothing
    elsif row[0] == '0' then
      puts "#{row[2]} no time entered"
    elsif date == last_date then
      puts "           %s to %s for %3s hours %s" % row.values_at(3, 4, 5, 8)
    else
      puts "%s %s to %s for %3s hours %s" % row.values_at(2, 3, 4, 5, 8)
    end

    last_date = date
  end
end

#fill_timesheetObject

Fills in timesheet rows that don’t already have data



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/milton.rb', line 168

def fill_timesheet
  rows = []
  last_date = nil
  filled_in = false

  parse_timesheet.each do |data|
    department  = data[6]
    employee_id = data[7]
    date        = Date.parse(CGI.unescape(data[1])).strftime('%m/%d/%Y')

    # skip days with data already filled (like holidays)
    if data[0].to_i > 0 or (filled_in and date == last_date) then
      filled_in = true
      last_date = date
      next
    end

    filled_in = false

    start, finish = starting_and_ending_timestamp date, last_date

    rows << [
      '0', '', 'False', 'True', 'False', 'False', 'False',
      "#{date} 12:00:00 AM",
      start, '',
      finish,
      '8', '',
      department,
      employee_id,
      '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
      '', '', '', 'EDIT', '', '', '', '', '2', '', '0', 'False'
    ]

    # This reset is for the timestamp calculations.
    last_date = date
  end

  @page = @page.form('Form1') { |form|
    ## FIXME: Fill out this form
    form['hdnRETURNDATA'] = rows.map { |row|
      row.map { |value|
        CGI.escape(value)
      }.join('~~')
    }.join('~||~')

    form['__EVENTTARGET'] = 'TG:btnSubmitTop'
    form['__PageDirty']   = 'True'
  }.submit
end

#login(username, password) ⇒ Object

Logs in username with password



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/milton.rb', line 114

def  username, password
  @username = username

  @page = @page.form('Login') { |form|
    form['txtUserID']     = username
    form['txtPassword']   = password
    form['__EVENTTARGET'] = 'btnLogin'
  }.submit
  change_password if @page.body =~ /Old Password/
  @page = @page.link_with(:text => 'Time Sheet').click
end

#rows_per_day=(rows = 2) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/milton.rb', line 133

def rows_per_day= rows = 2
  @rows_per_day = rows
  @page = @page.form('Form1') { |form|
    form['__EVENTTARGET'] = 'SETNOOFROWS'
    form['__EVENTARGUMENT'] = rows.to_s
    form['__PageDirty']   = 'False'
  }.submit
end

#run(config) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/milton.rb', line 142

def run config
  self.client_name = config['client_name']
   config['username'], config['password']

  date  = config['date']
  month = config['month']

  if date then
    select_week_of date
  elsif month
    select_month_of month
  else
    select_current_week
  end

  unless config['view'] then
    self.rows_per_day = config['rows_per_day'] || 2
    fill_timesheet
  end

  extract_timesheet
end

#select_current_weekObject

Selects the current week’s timesheet



129
130
131
# File 'lib/milton.rb', line 129

def select_current_week
  select_week_of Date.today
end

#select_month_of(date) ⇒ Object



260
261
262
263
264
# File 'lib/milton.rb', line 260

def select_month_of date
  first = date - date.mday + 1
  last  = Date.new(first.year, first.month, -1)
  select_range(first, last)
end

#select_week_of(date) ⇒ Object

Selects the timesheet for the week containing date



254
255
256
257
258
# File 'lib/milton.rb', line 254

def select_week_of date
  monday = date - date.wday + 1
  friday = monday + 4
  select_range(monday, friday)
end