1
2
3
4
5
6
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
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
|
#!/usr/bin/env ruby
require 'rubygems'
require 'date'
require 'highline/import'
require 'hpricot'
require 'net/http'
require 'net/https'
require 'net/http/post/multipart'
require 'uri'
@parameters={
"time_in"=>"",
"time_out"=>"",
"time_break"=>"",
"weekend_days"=>[],
"login_url"=>'',
"timesheet_path"=>'',
}
# You normally don't need to do any change below this line
def create_timesheet_metadata(data)
editts_csrf_token = (Hpricot(data)/"input"/"#editTimesheet__csrf_token").first[:value]
editts_tsId = (Hpricot(data)/"input"/"#editTimesheet_txtTimesheetId").first[:value]
editts_employeeNumber = (Hpricot(data)/"input"/"#editTimesheet_employeeNumber").first[:value]
editts_yearChoices = (Hpricot(data)/"select"/"#editTimesheet_yearChoices/option[@selected=selected]").first[:value]
editts_monthChoices = (Hpricot(data)/"select"/"#editTimesheet_monthChoices/option[@selected=selected]").first[:value]
metadata_hash = Hash.new
metadata_hash["editTimesheet[_csrf_token]"] = editts_csrf_token
metadata_hash["editTimesheet[txtTimesheetId]"] = editts_tsId
metadata_hash["editTimesheet[show]"] = ""
metadata_hash["editTimesheet[txtAction]"] = "save"
metadata_hash["editTimesheet[employeeNumber]"] = editts_employeeNumber
metadata_hash["editTimesheet[yearChoices]"] = editts_yearChoices
metadata_hash["editTimesheet[monthChoices]"] = editts_monthChoices
metadata_hash["editTimesheet[submitComment]"] = ""
metadata_hash
end
def create_repost_data_input_hash(data,field)
i=0
last_id = field + i.to_s
field_hash = Hash.new
while last_id_elem = (Hpricot(data)/last_id).first
field_hash[last_id_elem[:name]]= last_id_elem[:value] || ""
i+=1
last_id = field + i.to_s
end
field_hash
end
def create_repost_data_select_hash(data,field)
i=0
last_id = field + i.to_s
field_hash = Hash.new
while last_id_elem = (Hpricot(data)/last_id).first
if selected = (last_id_elem/"option[@selected=selected]").first
field_hash[last_id_elem[:name]] = selected[:value]
else
field_hash[last_id_elem[:name]] = "1"
end
i+=1
last_id = field + i.to_s
end
field_hash
end
# Gererates the timesheet data (in_time, out_time, break_duration)
def generate_timesheet_data_hash(year,month)
ts_hash = Hash.new
i = 0
day=Date.new(year,month,1)
while day.month == month
if @parameters["weekend_days"].include? day.wday
ts_hash["in_time[" + i.to_s + "]"] = ""
ts_hash["out_time[" + i.to_s + "]"] = ""
ts_hash["break_duration[" + i.to_s + "]"] = ""
ts_hash["row_total_duration[" + i.to_s + "]"] = ""
i += 1
day += 1
next
end
ts_hash["in_time[" + i.to_s + "]"] = @parameters["time_in"]
ts_hash["out_time[" + i.to_s + "]"] = @parameters["time_out"]
ts_hash["break_duration[" + i.to_s + "]"] = @parameters["time_break"]
i+=1
day += 1
end
ts_hash
end
data_hash = Hash.new
headers = Hash.new
username = ask("Enter your username: ") { |q| q.echo = true }
password = ask("Enter your password: " ) { |q| q.echo = "*"}
uri = URI(@parameters['login_url'])
# Start session for all the transactions
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Get.new uri.request_uri
# Get the login page and login
response = http.request request
cookie = response['set-cookie']
login_csrf_token = (Hpricot(response.body)/"input"/"#login__csrf_token").first[:value]
data_hash = Hash.new
data_hash["login[txtUserName]"] = username
data_hash["login[txtUserPassword]"] = password
data_hash["login[_csrf_token]"] = login_csrf_token
headers = {
'Cookie' => cookie,
'Referer' => @parameters['login_url'],
'Content-Type' => 'multipart/form-data'
}
res = http.request(Net::HTTP::Post::Multipart.new('/', data_hash, headers))
# Get the timesheet page and create post data
res = http.get(@parameters['timesheet_path'], headers)
data_hash=data_hash.merge(create_timesheet_metadata(res.body))
data_hash=data_hash.merge(create_repost_data_input_hash(res.body,"#original_record_ids_"))
data_hash=data_hash.merge(create_repost_data_input_hash(res.body,"#record_id_"))
data_hash=data_hash.merge(create_repost_data_input_hash(res.body,"#date_"))
data_hash=data_hash.merge(create_repost_data_input_hash(res.body,"#medical_checkup_duration_"))
data_hash=data_hash.merge(create_repost_data_input_hash(res.body,"#comment_"))
data_hash=data_hash.merge(create_repost_data_select_hash(res.body,"#classification_id_"))
data_hash=data_hash.merge(generate_timesheet_data_hash(data_hash["editTimesheet[yearChoices]"].to_i,data_hash["editTimesheet[monthChoices]"].to_i))
# Save the timesheets
res = http.request(Net::HTTP::Post::Multipart.new((Hpricot(res.body)/"form"/"#editTimesheet").first[:action], data_hash, headers))
end
print "The timesheets should have been saved. Please do a manual check before submitting!\nPlease report bugs, comments and improvements at ctrianta@rh.c.\n"
|