-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.rb
56 lines (44 loc) · 1.09 KB
/
app.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
require 'sinatra'
require_relative 'lib/mkr'
class App < Sinatra::Base
IFTTT_ACTIONS = {
'entered' => :clock_in,
'exited' => :clock_out
}.freeze
post '/' do
params = JSON.parse(request.body.read)
action = nil
if card_action?(params['action'])
action = params['action'].to_sym
else
unless valid_ifttt_action?(params['action'])
Mkr.logger.failure("Invalid parameters: #{params.inspect}")
return
end
unless valid_clock?(action)
Mkr.logger.failure("Off hour: `:#{action}`")
raise "Off hour: `:#{action}`"
end
action = IFTTT_ACTIONS[params['action']]
end
Mkr.run(action)
end
private
def card_action?(action)
action == 'card'
end
def valid_ifttt_action?(action)
IFTTT_ACTIONS.keys.include?(action)
end
def valid_clock?(action)
send("validate_#{action}")
end
def validate_clock_in
now = Time.now
now < Time.local(now.year, now.month, now.day, 13, 0)
end
def validate_clock_out
now = Time.now
Time.local(now.year, now.month, now.day, 15, 0) <= now
end
end