Class: Lita::Handlers::Stackstorm
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::Stackstorm
- Defined in:
- lib/lita/handlers/stackstorm.rb
Class Attribute Summary collapse
-
.expires ⇒ Object
Returns the value of attribute expires.
-
.token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #auth_builder ⇒ Object
- #authenticate ⇒ Object
- #call_alias(msg) ⇒ Object
- #direct_post(channel, message, user) ⇒ Object
- #expired ⇒ Object
- #headers ⇒ Object
- #list(msg) ⇒ Object
- #login(msg) ⇒ Object
- #make_post_request(path, body) ⇒ Object
- #make_request(path, body) ⇒ Object
- #stream_listen(payload) ⇒ Object
- #url_builder ⇒ Object
Class Attribute Details
.expires ⇒ Object
Returns the value of attribute expires.
29 30 31 |
# File 'lib/lita/handlers/stackstorm.rb', line 29 def expires @expires end |
.token ⇒ Object
Returns the value of attribute token.
29 30 31 |
# File 'lib/lita/handlers/stackstorm.rb', line 29 def token @token end |
Class Method Details
.config(config) ⇒ Object
32 33 34 35 |
# File 'lib/lita/handlers/stackstorm.rb', line 32 def self.config(config) self.token = nil self.expires = nil end |
Instance Method Details
#auth_builder ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/lita/handlers/stackstorm.rb', line 91 def auth_builder if Integer(config.auth_port) == 443 and config.url.start_with?('https') "#{config.url}/auth" else "#{config.url}:#{config.auth_port}/v1" end end |
#authenticate ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/lita/handlers/stackstorm.rb', line 107 def authenticate resp = http.post("#{auth_builder()}/tokens") do |req| req.body = {} req.headers['Authorization'] = http.(:basic_auth, config.username, config.password) end self.class.token = JSON.parse(resp.body)['token'] self.class.expires = JSON.parse(resp.body)['expiry'] resp end |
#call_alias(msg) ⇒ Object
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 |
# File 'lib/lita/handlers/stackstorm.rb', line 117 def call_alias(msg) if expired authenticate end command = msg.matches.flatten.first found = "" redis.scan_each do |a| possible = /#{a}/.match(command) if not possible.nil? found = a break end end jobject = JSON.parse(redis.get(found)) payload = { name: jobject['object']['name'], format: jobject['format'], command: command, user: msg.user.name, source_channel: 'chatops', notification_channel: 'lita' } s = make_post_request("/aliasexecution", payload) j = JSON.parse(s.body) if s.success? msg.reply "Got it! Details available at #{config.url}/#/history/#{j['execution']['id']}/general" else msg.reply "Execution failed with message: #{j['faultstring']}" end end |
#direct_post(channel, message, user) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lita/handlers/stackstorm.rb', line 42 def direct_post(channel, , user) case robot.config.robot.adapter when :slack payload = { action: "slack.chat.postMessage", parameters: { username: user, text: , icon_emoji: config.emoji_icon, channel: channel } } make_post_request("/executions", payload) else target = Lita::Source.new(user: user, room: Lita::Room.fuzzy_find(channel)) robot.(target, ) end end |
#expired ⇒ Object
185 186 187 |
# File 'lib/lita/handlers/stackstorm.rb', line 185 def expired self.class.token.nil? || Time.now >= Time.parse(self.class.expires) end |
#headers ⇒ Object
207 208 209 210 211 212 |
# File 'lib/lita/handlers/stackstorm.rb', line 207 def headers headers = {} headers['Content-Type'] = 'application/json' headers['X-Auth-Token'] = "#{self.class.token}" headers end |
#list(msg) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/lita/handlers/stackstorm.rb', line 149 def list(msg) if expired authenticate end redis.keys.each {|k| redis.del k } s = make_request("/actionalias", "") if JSON.parse(s.body).empty? msg.reply "No Action Aliases Registered" else j = JSON.parse(s.body) a = "" extra_params = '(\\s+(\\S+)\\s*=("([\\s\\S]*?)"|\'([\\s\\S]*?)\'|({[\\s\\S]*?})|(\\S+))\\s*)*' j.take_while{|i| i['enabled'] }.each do |command| command['formats'].each do |format| f = format.gsub(/(\s*){{\s*\S+\s*=\s*(?:({.+?}|.+?))\s*}}(\s*)/, '\\s*([\\S]+)?\\s*') f = f.gsub(/\s*{{.+?}}\s*/, '\\s*([\\S]+?)\\s*') f = "^\\s*#{f}#{extra_params}\\s*$" redis.set(f, {format: format, object: command}.to_json) a+= "#{format} -> #{command['description']}\n" end end msg.reply a end end |
#login(msg) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/lita/handlers/stackstorm.rb', line 174 def login(msg) http_resp = authenticate if ![200, 201, 280].index(http_resp.status).nil? msg.reply "login successful\ntoken: #{self.class.token}" elsif http_resp.status == 500 msg.reply "#{http_resp.status}: login failed!!" else msg.reply "#{http_resp.status}: login failed!!" end end |
#make_post_request(path, body) ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/lita/handlers/stackstorm.rb', line 197 def make_post_request(path, body) resp = http.post("#{url_builder()}#{path}") do |req| req.body = {} req.headers = headers req.body = body.to_json end resp end |
#make_request(path, body) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/lita/handlers/stackstorm.rb', line 189 def make_request(path, body) resp = http.get("#{url_builder()}#{path}") do |req| req.headers = headers req.body = body.to_json if not body.empty? end resp end |
#stream_listen(payload) ⇒ Object
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 |
# File 'lib/lita/handlers/stackstorm.rb', line 61 def stream_listen(payload) if expired authenticate end uri = URI("#{url_builder()}/stream") Thread.new { Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| request = Net::HTTP::Get.new uri request['X-Auth-Token'] = headers()['X-Auth-Token'] request['Content-Type'] = 'application/x-yaml' http.request request do |response| io = StringIO.new response.read_body do |chunk| c = chunk.strip if c.length > 0 io.write chunk event = YAML.load(io.string) if event['event'] =~ /st2\.announcement/ direct_post(event['data']['payload']['channel'], event['data']['payload']['message'], event['data']['payload']['user']) end io.reopen("") end end end end } end |
#url_builder ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/lita/handlers/stackstorm.rb', line 99 def url_builder if Integer(config.execution_port) == 443 and config.url.start_with?('https') "#{config.url}/api" else "#{config.url}:#{config.execution_port}/v1" end end |