repp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8d7c022e0eece174a0e7cd1e0e0d569307353982586fca0a71001d27425cad48
4
+ data.tar.gz: 7e8aaeea3a3b61bfa29846c4ba248ef3c7e4f2f15a9de728d72e80bf84657f90
5
+ SHA512:
6
+ metadata.gz: 3aee6a054338de1436ba5297f85a3c9f60770de504e64f38eb3f376bd898a244911479b5e6393c39dac4a820dc5650fd0ba55b496cba2ec816fbeaa3a71e15d5
7
+ data.tar.gz: 39f65759bbdcc3d61c8cb70a427bdde0e1920d09e944505d952670dc936f1a1e7e836accd041a948f1051b0d0ff0758c5558960426eeae46759b0333111322a3
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 kinoppyd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Repp
2
+
3
+ Repp is a Ruby bot framework interface inspired by Rack
4
+
5
+ # Interface
6
+
7
+ TBD
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "repp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ module Repp
2
+ module Event
3
+ module Attributes
4
+ EVENT_TYPE = :event_type
5
+ RECERVED_WORDS = [EVENT_TYPE]
6
+
7
+ def event_type(setting)
8
+ define_method(EVENT_TYPE) { setting }
9
+ end
10
+
11
+ def interface(*names)
12
+ names.each do |name|
13
+ name = name.to_s
14
+ next if RECERVED_WORDS.include?(name.to_sym)
15
+ writer = (name.end_with?("?") ? name[0...-1] : name) + "="
16
+ define_method(name) { @attributes[name] }
17
+ define_method(writer) { |val| @attributes[name] }
18
+ end
19
+ end
20
+ end
21
+
22
+ class Base
23
+ extend Attributes
24
+ def initialize(params)
25
+ @attributes = {}
26
+ return unless params.respond_to?(:each_pair)
27
+ params.each_pair do |k, v|
28
+ @attributes[k.to_s] = v
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ module Repp
2
+ module Event
3
+ class Receive < Base
4
+ event_type :message
5
+ interface :body
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Repp
2
+ module Event
3
+ class Ticker < Base
4
+ event_type :ticker
5
+ interface :body, :reply_to, :bot?
6
+ end
7
+ end
8
+ end
data/lib/repp/event.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Repp
2
+ module Event
3
+ require 'repp/event/base'
4
+ require 'repp/event/receive'
5
+ require 'repp/event/ticker'
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ require 'eventmachine'
2
+
3
+ module Repp
4
+ module Handler
5
+ class Shell
6
+ module KeyboardHandler
7
+ include EM::Protocols::LineText2
8
+ def initialize(app) @app = app.new; end
9
+ def receive_line(data)
10
+ reply_to = /@\w+/.match(data)&.[](1)
11
+ message = Event::Receive.new(type: :message, body: data, reply_to: reply_to, bot?: false)
12
+ res = @app.call(message)
13
+ if res.any?
14
+ $stdout.puts res.first
15
+ end
16
+ end
17
+ end
18
+
19
+ def self.run(app, options = {})
20
+ yield self if block_given?
21
+
22
+ EM.run { EM.open_keyboard(KeyboardHandler, app) }
23
+ end
24
+
25
+ def self.stop
26
+ EM.run { EM.stop }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,57 @@
1
+ module Repp
2
+ module Handler
3
+ class Slack
4
+ require 'slack-ruby-client'
5
+
6
+ class SlackReceive < Event::Receive
7
+ interface :channel, :user, :type, :ts
8
+ end
9
+
10
+ module SlackMessageHandler
11
+ def self.handle(client, web_client, app)
12
+ client.on :message do |data|
13
+ receive = SlackReceive.new(
14
+ body: data.text,
15
+ channel: data.channel,
16
+ user: data.user,
17
+ type: data.type,
18
+ ts: data.ts
19
+ )
20
+ res = app.call(receive)
21
+ if res.first
22
+ channel_to_post = res.last&.[](:channel) || receive.channel
23
+ web_client.chat_postMessage(text: res.first, channel: channel_to_post, as_user: true)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ class << self
30
+ def run(app, options = {})
31
+ yield self if block_given?
32
+
33
+ ::Slack.configure do |config|
34
+ config.token = detect_token
35
+ end
36
+ @client = ::Slack::RealTime::Client.new
37
+ @web_client = ::Slack::Web::Client.new
38
+ SlackMessageHandler.handle(@client, @web_client, app.new)
39
+ @client.start!
40
+ end
41
+
42
+ def stop!
43
+ @client.stop!
44
+ end
45
+
46
+ private
47
+
48
+ def detect_token
49
+ return ENV['SLACK_TOKEN'] if ENV['SLACK_TOKEN']
50
+ token_file = "#{ENV['HOME']}/.slack/token"
51
+ return File.read(token_file).chomp if File.exist?(token_file)
52
+ fail "Can't find Slack token"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,65 @@
1
+ module Repp
2
+ module Handler
3
+ class << self
4
+ def get(service)
5
+ return unless service
6
+ service = service.to_s
7
+
8
+ unless @handlers[service]
9
+ load_error = try_require('repp/handler', service)
10
+ end
11
+
12
+ if klass = @handlers[service]
13
+ klass.split('::').inject(Object) { |o, x| o.const_get(x) }
14
+ else
15
+ const_get(service, false)
16
+ end
17
+
18
+ rescue => name_error
19
+ raise load_error || name_error
20
+ end
21
+
22
+ def self.pick(service_names)
23
+ service_names = Array(service_names)
24
+ service_names.each do |service_name|
25
+ begin
26
+ return get(service_name.to_s)
27
+ rescue LoadError, NameError
28
+ end
29
+ end
30
+
31
+ raise LoadError, "Couldn't find handler for: #{service_names.join(', ')}."
32
+ end
33
+
34
+ def self.default
35
+ # Guess.
36
+ if ENV.include?("REPP_HANDLER")
37
+ self.get(ENV["REPP_HANDLER"])
38
+ else
39
+ pick ['shell']
40
+ end
41
+ end
42
+
43
+ def self.try_require(prefix, const_name)
44
+ file = const_name.gsub(/^[A-Z]+/) { |pre| pre.downcase }.
45
+ gsub(/[A-Z]+[^A-Z]/, '_\&').downcase
46
+
47
+ require(::File.join(prefix, file))
48
+ nil
49
+ rescue LoadError => error
50
+ error
51
+ end
52
+
53
+ def register(service, klass)
54
+ @handlers ||= {}
55
+ @handlers[service.to_s] = klass.to_s
56
+ end
57
+ end
58
+
59
+ autoload :Shell, "repp/handler/shell"
60
+ register 'shell', 'Repp::Handler::Shell'
61
+
62
+ autoload :Slack, "repp/handler/slack"
63
+ register 'slack', 'Repp::Handler::Slack'
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Repp
2
+ VERSION = "0.1.0"
3
+ end
data/lib/repp.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "repp/version"
2
+ require "repp/event"
3
+ require 'repp/handler'
data/repp.gemspec ADDED
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "repp/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "repp"
8
+ spec.version = Repp::VERSION
9
+ spec.authors = ["kinoppyd"]
10
+ spec.email = ["WhoIsDissolvedGirl+github@gmail.com"]
11
+
12
+ spec.summary = %q{Repp is a chat service interface}
13
+ spec.description = %q{Repp is a chat service interface}
14
+ spec.homepage = "https://github.com/kinoppyd/repp"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "eventmachine", "~> 1.2"
25
+ spec.add_dependency "slack-ruby-client", "~> 0.11"
26
+ spec.add_dependency "faye-websocket", "~> 0.10"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "minitest", "~> 5.0"
31
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kinoppyd
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eventmachine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: slack-ruby-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.11'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faye-websocket
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: Repp is a chat service interface
98
+ email:
99
+ - WhoIsDissolvedGirl+github@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - bin/console
110
+ - bin/setup
111
+ - lib/repp.rb
112
+ - lib/repp/event.rb
113
+ - lib/repp/event/base.rb
114
+ - lib/repp/event/receive.rb
115
+ - lib/repp/event/ticker.rb
116
+ - lib/repp/handler.rb
117
+ - lib/repp/handler/shell.rb
118
+ - lib/repp/handler/slack.rb
119
+ - lib/repp/version.rb
120
+ - repp.gemspec
121
+ homepage: https://github.com/kinoppyd/repp
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.7.6
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Repp is a chat service interface
145
+ test_files: []