|
2 | 2 | require 'net/https'
|
3 | 3 | require 'time'
|
4 | 4 |
|
5 |
| -class RocketChat |
6 |
| - def initialize(config) |
7 |
| - @channel = config['channel'] |
8 |
| - uri = URI.parse config['uri'] |
9 |
| - @http = Net::HTTP.new uri.host, uri.port |
10 |
| - @http.use_ssl = true |
11 |
| - |
12 |
| - @user = config['user'] |
13 |
| - @pass = config['pass'] |
14 |
| - |
15 |
| - working_dir = "#{ENV['HOME']}/.xify/RocketChat" |
16 |
| - Dir.mkdir working_dir rescue Errno::EEXIST |
17 |
| - @auth_file = "#{working_dir}/#{@user}.json" |
18 |
| - end |
19 |
| - |
20 |
| - def login |
21 |
| - if File.exists? @auth_file |
22 |
| - @auth_data = JSON.parse File.read @auth_file |
23 |
| - return |
24 |
| - end |
25 |
| - |
26 |
| - req = Net::HTTP::Post.new '/api/v1/login', |
27 |
| - 'Content-Type' => 'application/json' |
28 |
| - req.body = { |
29 |
| - username: @user, |
30 |
| - password: @pass |
31 |
| - }.to_json |
32 |
| - |
33 |
| - res = @http.request req |
34 |
| - |
35 |
| - raise "Error: #{res.code} #{res.message}\n#{res.body}" unless res.is_a? Net::HTTPSuccess |
36 |
| - |
37 |
| - @auth_data = JSON.parse(res.body)['data'] |
38 |
| - File.write @auth_file, @auth_data.to_json |
39 |
| - end |
40 |
| - |
41 |
| - def reset_auth |
42 |
| - @auth_data = nil |
43 |
| - File.delete @auth_file |
44 |
| - end |
45 |
| - |
46 |
| - def authenticated_request |
47 |
| - login unless @auth_data |
48 |
| - |
49 |
| - req = Net::HTTP::Post.new '/api/v1/chat.postMessage', |
50 |
| - 'Content-Type' => 'application/json', |
51 |
| - 'X-User-Id' => @auth_data['userId'], |
52 |
| - 'X-Auth-Token' => @auth_data['authToken'] |
53 |
| - |
54 |
| - yield req |
55 |
| - |
56 |
| - req |
57 |
| - end |
58 |
| - |
59 |
| - def process(event) |
60 |
| - res = @http.request(authenticated_request do |req| |
61 |
| - req.body = { |
62 |
| - channel: @channel, |
63 |
| - alias: event.author, |
64 |
| - attachments: [ |
65 |
| - { |
66 |
| - title: event.args[:parent], |
67 |
| - title_link: event.args[:parent_link], |
68 |
| - text: event.args[:link] ? "#{event.message.chomp} ([more](#{event.args[:link]}))" : event.message.chomp |
69 |
| - } |
70 |
| - ] |
71 |
| - }.to_json |
72 |
| - end) |
73 |
| - |
74 |
| - case res |
75 |
| - when Net::HTTPUnauthorized |
76 |
| - reset_auth |
77 |
| - process event |
78 |
| - when Net::HTTPSuccess |
79 |
| - # nothing |
80 |
| - else |
81 |
| - $stderr.puts "Error: #{res.code} #{res.message}\n#{res.body}" |
| 5 | +require 'xify/base/rocket_chat' |
| 6 | + |
| 7 | +module Output |
| 8 | + class RocketChat < Base::RocketChat |
| 9 | + def process(event) |
| 10 | + request :post, '/api/v1/chat.postMessage' do |req| |
| 11 | + req['Content-Type'] = 'application/json' |
| 12 | + req.body = { |
| 13 | + channel: @config['channel'], |
| 14 | + alias: event.author, |
| 15 | + attachments: [ |
| 16 | + { |
| 17 | + title: event.args[:parent], |
| 18 | + title_link: event.args[:parent_link], |
| 19 | + text: event.args[:link] ? "#{event.message.chomp} ([more](#{event.args[:link]}))" : event.message.chomp |
| 20 | + } |
| 21 | + ] |
| 22 | + }.to_json |
| 23 | + end |
82 | 24 | end
|
83 | 25 | end
|
84 | 26 | end
|
0 commit comments