xify 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c33e22276a4a7300eb6173fc59c138ab023273a3f8707228c44f671daf95be0d
4
+ data.tar.gz: 54c6954be8ce6ff1ae785155659525ccedfab5172d83bf4e8874feeb2e350bfe
5
+ SHA512:
6
+ metadata.gz: 61a2e697065f2156bf1981b27605b4d321d675f1ca56718675c3f023c2b68049bc9aca927131a6307970a25205907c7363960ecd029edd29ae91d81e7cc54c60
7
+ data.tar.gz: f3fda430b30f731daa6e52cd357d607a6df4c8c0477c98903125cf8cdeea639565d84bbc6f1c3a98ddda1b477597a15f9d562aabef154e1f807fc794ea7e1efe
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
4
+
5
+ require 'xify'
6
+
7
+ Xify.run ARGV
@@ -0,0 +1,36 @@
1
+ require 'yaml'
2
+
3
+ require 'xify/input/stdin'
4
+ require 'xify/output/stdout'
5
+ require 'xify/output/rocket_chat'
6
+
7
+ class Xify
8
+ def self.run(args)
9
+ working_dir = "#{ENV['HOME']}/.xify"
10
+ Dir.mkdir working_dir rescue Errno::EEXIST
11
+
12
+ config_file = args.shift || "#{working_dir}/config.yml"
13
+ puts "Loading config from #{config_file}"
14
+ config = YAML::load_file config_file
15
+
16
+ config.keys.each do |c|
17
+ config[c].map! do |handler|
18
+ next unless handler['enabled']
19
+ Object.const_get(handler['class']).new(handler)
20
+ end
21
+ end
22
+
23
+ puts 'Looking for updates'
24
+ config['inputs'].each do |i|
25
+ i.updates do |u|
26
+ config['outputs'].each do |o|
27
+ begin
28
+ o.process u
29
+ rescue => e
30
+ $stderr.puts e.message
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require 'xify/item'
2
+
3
+ class Stdin
4
+ def initialize(config)
5
+ end
6
+
7
+ def updates
8
+ loop do
9
+ begin
10
+ input = prompt
11
+
12
+ unless input
13
+ # Stop on CTRL+D
14
+ puts
15
+ break
16
+ end
17
+
18
+ if input.length != 1
19
+ yield Item.new link: 'http://localhost', message: input, source: 'stdin'
20
+ end
21
+ rescue Interrupt
22
+ # Stop on CTRL+C
23
+ puts
24
+ break
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def prompt
32
+ print '> '
33
+
34
+ gets
35
+ end
36
+ end
@@ -0,0 +1,12 @@
1
+ class Item
2
+ attr_accessor :link, :message, :source, :author, :parent, :parent_link, :time
3
+ def initialize(link:, message:, source:, author: 'Anonymous', parent: nil, parent_link: nil, time: Time.now.iso8601)
4
+ self.author = author
5
+ self.link = link
6
+ self.message = message
7
+ self.parent = parent
8
+ self.parent_link = parent_link
9
+ self.source = source
10
+ self.time = time
11
+ end
12
+ end
@@ -0,0 +1,87 @@
1
+ require 'json'
2
+ require 'net/https'
3
+ require 'time'
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(item)
60
+ res = @http.request(authenticated_request do |req|
61
+ req.body = {
62
+ channel: @channel,
63
+ alias: item.author,
64
+ attachments: [
65
+ {
66
+ author_name: item.source,
67
+ ts: item.time,
68
+ message_link: item.link,
69
+ title: item.parent,
70
+ title_link: item.parent_link,
71
+ text: item.message
72
+ }
73
+ ]
74
+ }.to_json
75
+ end)
76
+
77
+ case res
78
+ when Net::HTTPUnauthorized
79
+ reset_auth
80
+ process(item)
81
+ when Net::HTTPSuccess
82
+ # nothing
83
+ else
84
+ $stderr.puts "Error: #{res.code} #{res.message}\n#{res.body}"
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,10 @@
1
+ require 'time'
2
+
3
+ class Stdout
4
+ def initialize(config)
5
+ end
6
+
7
+ def process(item)
8
+ puts "[#{item.time}] #{item.message}"
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Finn Glöe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Cross-post content from one service to another.
14
+ email: fgloee@united-internet.de
15
+ executables:
16
+ - xify
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/xify
21
+ - lib/xify.rb
22
+ - lib/xify/input/stdin.rb
23
+ - lib/xify/item.rb
24
+ - lib/xify/output/rocket_chat.rb
25
+ - lib/xify/output/stdout.rb
26
+ homepage:
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.7.6
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Cross-post content from one service to another.
49
+ test_files: []