-
Notifications
You must be signed in to change notification settings - Fork 0
/
hours.rb
245 lines (210 loc) · 7.85 KB
/
hours.rb
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
require 'sinatra'
require 'pony'
require 'base64'
require 'bigdecimal'
require 'date'
require 'net/http'
require 'net/https'
require 'time'
require 'json'
require 'net/smtp'
# setting up Harvest
class Harvest
SUBDOMAIN = ENV["SUBDOMAIN"] || #subdomain
ACCOUNT_EMAIL = ENV["EMAIL"] || #account_email
ACCOUNT_PASSWORD = ENV["PASSWORD"] || #account_password
USER_AGENT = ""
HAS_SSL = true
def initialize
@company = SUBDOMAIN
@preferred_protocols = [HAS_SSL, ! HAS_SSL]
connect!
end
# HTTP headers you need to send with every request.
def headers
{
# Declare that you expect response in XML after a _successful_
# response.
"Accept" => "application/json",
# Promise to send XML.
"Content-Type" => "application/json; charset=utf-8",
# All requests will be authenticated using HTTP Basic Auth, as
# described in rfc2617. Your library probably has support for
# basic_auth built in, I've passed the Authorization header
# explicitly here only to show what happens at HTTP level.
"Authorization" => "Basic #{auth_string}",
# Tell Harvest a bit about your application.
"User-Agent" => USER_AGENT
}
end
def auth_string
Base64.encode64("#{ACCOUNT_EMAIL}:#{ACCOUNT_PASSWORD}").delete("\r\n")
end
def request path, method = :get, body = ""
response = send_request( path, method, body)
if response.class < Net::HTTPSuccess
# response in the 2xx range
on_completed_request
return response
elsif response.class == Net::HTTPServiceUnavailable
# response status is 503, you have reached the API throttle
# limit. Harvest will send the "Retry-After" header to indicate
# the number of seconds your boot needs to be silent.
raise "Got HTTP 503 three times in a row" if retry_counter > 3
sleep(response['Retry-After'].to_i + 5)
request(path, method, body)
elsif response.class == Net::HTTPFound
# response was a redirect, most likely due to protocol
# mismatch. Retry again with a different protocol.
@preferred_protocols.shift
raise "Failed connection using http or https" if @preferred_protocols.empty?
connect!
request(path, method, body)
else
dump_headers = response.to_hash.map { |h,v| [h.upcase,v].join(': ') }.join("\n")
raise "#{response.message} (#{response.code})\n\n#{dump_headers}\n\n#{response.body}\n"
end
end
private
def connect!
port = has_ssl ? 443 : 80
@connection = Net::HTTP.new("#{@company}.harvestapp.com", port)
@connection.use_ssl = has_ssl
@connection.verify_mode = OpenSSL::SSL::VERIFY_NONE if has_ssl
end
def has_ssl
@preferred_protocols.first
end
def send_request path, method = :get, body = ''
case method
when :get
@connection.get(path, headers)
when :post
@connection.post(path, body, headers)
when :put
@connection.put(path, body, headers)
when :delete
@connection.delete(path, headers)
end
end
def on_completed_request
@retry_counter = 0
end
def retry_counter
@retry_counter ||= 0
@retry_counter += 1
end
end
########end Harvest Setup############
class Hours
@users = {} #user_name" => user_id
@users_time = {}
@bill_option = ['yes', 'no']
@harvest = Harvest.new
def self.get_hours_daily
start = (Date.today).strftime("%Y/%m/%d")
finish = (Date.today+1).strftime("%Y/%m/%d")
@team_daily_billable_hours = 0
@team_daily_non_billable_hours = 0
@users.each do |key, value|
@total_daily_billable_hours = 0
@total_daily_non_billable_hours = 0
@bill_option.each do |b|
request = @harvest.request "/people/#{value}/entries?from=#{start}&to=#{finish}&billable=#{b}", :get
response = JSON.parse(request.body)
x=0
if b == 'yes'
while x < response.length do
@total_daily_billable_hours += response[x]["day_entry"]["hours"]
x += 1
end
else
while x < response.length do
@total_daily_non_billable_hours += response[x]["day_entry"]["hours"]
x += 1
end
end
end
@team_daily_billable_hours += @total_daily_billable_hours
@team_daily_non_billable_hours += @total_daily_non_billable_hours
@users_time["#{key} Daily Billable"] = @total_daily_billable_hours.round(2).to_s
@users_time["#{key} Daily Non-Billable"] = @total_daily_non_billable_hours.round(2).to_s
end
end
def self.get_hours_weekly
days_of_week = [1,2,3,4,5,6,7]
current_day =Date.today
if current_day.monday?
@start = (DateTime.now).strftime("%Y/%m/%d")
@finish = (DateTime.now).strftime("%Y/%m/%d")
elsif current_day.tuesday?
@start = (DateTime.now-1).strftime("%Y/%m/%d")
@finish = (DateTime.now ).strftime("%Y/%m/%d")
elsif current_day.wednesday?
@start = (DateTime.now-2).strftime("%Y/%m/%d")
@finish = (DateTime.now ).strftime("%Y/%m/%d")
elsif current_day.thursday?
@start = (DateTime.now-3).strftime("%Y/%m/%d")
@finish = (DateTime.now ).strftime("%Y/%m/%d")
elsif current_day.friday?
@start = (DateTime.now-4).strftime("%Y/%m/%d")
@finish = (DateTime.now ).strftime("%Y/%m/%d")
elsif current_day.saturday?
@start = (DateTime.now-5).strftime("%Y/%m/%d")
@finish = (DateTime.now).strftime("%Y/%m/%d")
else
@start = (DateTime.now-6).strftime("%Y/%m/%d")
@finish = (DateTime.now).strftime("%Y/%m/%d")
end
@team_weekly_billable = 0
@team_non_billable_weekly = 0
@users.each do |key, value|
@total_billable_hours = 0
@total_non_billable_hours = 0
@bill_option.each do |b|
request = @harvest.request "/people/#{value}/entries?from=#{@start}&to=#{@finish}&billable=#{b}", :get
response = JSON.parse(request.body)
x=0
if b == 'yes'
while x < response.length do
@total_billable_hours += response[x]["day_entry"]["hours"]
x += 1
end
else
while x < response.length do
@total_non_billable_hours += response[x]["day_entry"]["hours"]
x += 1
end
end
end
@team_weekly_billable += @total_billable_hours
@team_non_billable_weekly += @total_non_billable_hours
@users_time["#{key} Weekly Billable"] = @total_billable_hours.round(2).to_s
@users_time["#{key} Weekly Non-Billable"] = @total_non_billable_hours.round(2).to_s
end
@total_logged_weekly = @team_weekly_billable+ @team_non_billable_weekly
end
def self.users_time
@why = ""
@users_time.each do |key, value|
@why += "<p> #{key} = #{value} </p>"
end
end
def self.send_email
Pony.mail({
:to => '',
:from => '',
:subject => "#{Time.now.strftime("%m/%d/%Y")} Hours Update",
:html_body => "<h2> Our Hours: </h2>#{@why} <h2> Team Weekly Billable: #{@team_weekly_billable.round(2)}</h2> <h2>Team Daily Billable: #{@team_daily_billable_hours.round(2)}</h2>",
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:user_name => '',
:password => '',
:authentication => :plain,
:domain => ""
}
})
end
end