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.1.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



96
97
98
99
100
101
# File 'lib/milton.rb', line 96

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
77
78
79
80
81
82
# File 'lib/milton.rb', line 33

def self.parse_args argv
  options = {
    'date'  => nil,
    'debug' => false,
    '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('--debug',
           'Print out URLs we visit') do |debug|
      options['debug'] = debug
    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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/milton.rb', line 84

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



106
107
108
109
110
111
112
113
114
115
# File 'lib/milton.rb', line 106

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/milton.rb', line 243

def extract_timesheet
  @page.parser.css(".ErrorText").each do |n|
    puts "ERROR: #{n.text}"
  end

  timesheet = parse_timesheet

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

  puts "ERROR: #{error}" if error

  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



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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/milton.rb', line 190

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/milton.rb', line 120

def  username, password
  @username = username

  @page = @page.form('Login') { |form|
    form['txtUserID']     = username
    form['txtPassword']   = password
    form['__EVENTTARGET'] = 'btnLogin'
  }.submit
  change_password password if @page.body =~ /Old Password/

  if @page.body =~ /Supervisor Services/ then
    warn "switching to employee page"
    option = @page.parser.css("select#Banner1_ddlServices").children.find { |n| n["value"] =~ /EmployeeServicesStart/ }
    @page = @agent.get option["value"]
  end

  @page = @page.link_with(:text => 'Time Sheet').click
end

#rows_per_day=(rows = 2) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/milton.rb', line 146

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/milton.rb', line 155

def run config
  @debug = config['debug']
  if @debug then
    @agent.log = Logger.new $stderr
    @agent.log.formatter = proc do |s, t, p, m| "#{m}\n" end
  end

  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
rescue => e
  $stderr.puts @page.parser if @debug and @page
  raise
end

#select_current_weekObject

Selects the current week’s timesheet



142
143
144
# File 'lib/milton.rb', line 142

def select_current_week
  select_week_of Date.today
end

#select_month_of(date) ⇒ Object



289
290
291
292
293
# File 'lib/milton.rb', line 289

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



283
284
285
286
287
# File 'lib/milton.rb', line 283

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