Skip to content

Commit 4d613fd

Browse files
committedMay 12, 2018
Extract common functionality into RocketChat base class
1 parent 0a245a3 commit 4d613fd

File tree

7 files changed

+145
-123
lines changed

7 files changed

+145
-123
lines changed
 

‎lib/xify.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def self.run(args)
2020
puts "Loading config from #{config_file}"
2121
config = YAML::load_file config_file
2222

23-
config.keys.each do |c|
24-
config[c].map! do |handler|
23+
config.keys.each do |section|
24+
ns = section[0...-1].capitalize
25+
config[section].map! do |handler|
2526
next unless handler['enabled']
26-
Object.const_get(handler['class']).new(handler)
27+
Object.const_get("#{ns}::#{handler['class']}").new handler
2728
end.compact!
2829
end
2930

‎lib/xify/base/rocket_chat.rb

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'json'
2+
require 'net/https'
3+
require 'time'
4+
5+
module Base
6+
class RocketChat
7+
def initialize(config)
8+
@config = config
9+
10+
uri = URI.parse config['uri']
11+
@http = Net::HTTP.new uri.host, uri.port
12+
@http.use_ssl = true
13+
14+
working_dir = "#{ENV['HOME']}/.xify/RocketChat"
15+
Dir.mkdir working_dir rescue Errno::EEXIST
16+
@auth_file = "#{working_dir}/#{@config['user']}.json"
17+
end
18+
19+
def request(method, path, &block)
20+
login unless @auth_data
21+
22+
req = Object.const_get("Net::HTTP::#{method.capitalize}").new path,
23+
'X-User-Id' => @auth_data['userId'],
24+
'X-Auth-Token' => @auth_data['authToken']
25+
26+
yield req if block_given?
27+
28+
res = @http.request req
29+
30+
case res
31+
when Net::HTTPUnauthorized
32+
relogin
33+
request method, path, &block
34+
when Net::HTTPSuccess
35+
# nothing
36+
else
37+
raise "Error on #{method.upcase} #{@config['uri']}#{path}: #{res.code} #{res.message}\n#{res.body}"
38+
end
39+
40+
res
41+
end
42+
43+
private
44+
45+
def login
46+
if File.exists? @auth_file
47+
@auth_data = JSON.parse File.read @auth_file
48+
return
49+
end
50+
51+
req = Net::HTTP::Post.new '/api/v1/login',
52+
'Content-Type' => 'application/json'
53+
req.body = {
54+
username: @config['user'],
55+
password: @config['pass']
56+
}.to_json
57+
58+
res = @http.request req
59+
60+
raise "Error while authenticating to #{@config['uri']}: #{res.code} #{res.message}\n#{res.body}" unless res.is_a? Net::HTTPSuccess
61+
62+
@auth_data = JSON.parse(res.body)['data']
63+
File.write @auth_file, @auth_data.to_json
64+
end
65+
66+
def relogin
67+
File.delete @auth_file
68+
login
69+
end
70+
end
71+
end

‎lib/xify/input/pipe.rb

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
class Pipe
2-
def initialize(config)
3-
@author = config['author']
4-
end
1+
module Input
2+
class Pipe
3+
def initialize(config)
4+
@author = config['author']
5+
end
56

6-
def updates
7-
out = ARGF.read.chomp
8-
yield Event.new @author, out if out && out.length != 0
7+
def updates
8+
out = ARGF.read.chomp
9+
yield Event.new @author, out if out && out.length != 0
10+
end
911
end
10-
end
12+
end

‎lib/xify/input/prompt.rb

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
require 'xify/event'
22

3-
class Prompt
4-
def initialize(config)
5-
@author = config['author']
6-
end
3+
module Input
4+
class Prompt
5+
def initialize(config)
6+
@author = config['author']
7+
end
78

8-
def updates
9-
loop do
10-
begin
11-
input = prompt
9+
def updates
10+
loop do
11+
begin
12+
input = prompt
1213

13-
unless input
14-
# Stop on CTRL+D
14+
unless input
15+
# Stop on CTRL+D
16+
puts
17+
break
18+
end
19+
20+
if input.length != 1
21+
yield Event.new @author, input.chomp
22+
end
23+
rescue Interrupt
24+
# Stop on CTRL+C
1525
puts
1626
break
1727
end
18-
19-
if input.length != 1
20-
yield Event.new @author, input.chomp
21-
end
22-
rescue Interrupt
23-
# Stop on CTRL+C
24-
puts
25-
break
2628
end
2729
end
28-
end
2930

30-
private
31+
private
3132

32-
def prompt
33-
print '> '
33+
def prompt
34+
print '> '
3435

35-
gets
36+
gets
37+
end
3638
end
3739
end

‎lib/xify/input/shell.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
class Shell
2-
def initialize(config)
3-
@config = config
4-
end
1+
module Input
2+
class Shell
3+
def initialize(config)
4+
@config = config
5+
end
56

6-
def updates
7-
out = `#{@config['shell']}`.chomp
8-
yield Event.new @config['author'], out, parent: @config['shell'] if out && out.length != 0
7+
def updates
8+
out = `#{@config['shell']}`.chomp
9+
yield Event.new @config['author'], out, parent: @config['shell'] if out && out.length != 0
10+
end
911
end
1012
end

‎lib/xify/output/rocket_chat.rb

+19-77
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,25 @@
22
require 'net/https'
33
require 'time'
44

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
8224
end
8325
end
8426
end

‎lib/xify/output/stdout.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
require 'time'
22

3-
class Stdout
4-
def initialize(config)
5-
end
3+
module Output
4+
class Stdout
5+
def initialize(config)
6+
end
67

7-
def process(event)
8-
puts "[#{event.args[:time] || Time.now.iso8601}] #{event.author}:\n#{event.message}"
8+
def process(event)
9+
puts "[#{event.args[:time] || Time.now.iso8601}] #{event.author}:\n#{event.message}"
10+
end
911
end
1012
end

0 commit comments

Comments
 (0)
Please sign in to comment.