-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase_command.rb
76 lines (62 loc) · 1.13 KB
/
base_command.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
require 'httparty'
require 'active_support/all'
class BaseCommand
attr :params
include HTTParty
SLACK_HOOK_URL = ENV.fetch('SLACK_HOOK_URL', '')
def initialize(params = {})
@params = params
end
def self.perform(params = {})
new(params).perform
end
def respond(text)
if slack?
self.class.post(SLACK_HOOK_URL, response(text))
else
response(text)
end
end
def standard_body(text)
{
text: text,
channel: channel
}
end
def body(text)
result = standard_body(text)
if username.present? && icon_url.present?
result.merge(username: username, icon_url: icon_url)
elsif username.present?
result.merge(username: username)
elsif icon_url.present?
result.merge(icon_url: icon_url)
else
result
end
end
def response(text)
{
body: body(text).to_json
}
end
def channel
if channel_name
"##{channel_name}"
else
'#trivial-pursuits'
end
end
def channel_name
@params[:channel_name]
end
def username
''
end
def icon_url
''
end
def slack?
SLACK_HOOK_URL != ''
end
end