s-3po 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/s-3po/events.rb +27 -9
- data/lib/s-3po/parser.rb +12 -9
- data/lib/s-3po.rb +3 -2
- data/s-3po.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89544fe67ba616e85dd1e1082c660f2fd58c67e0
|
4
|
+
data.tar.gz: b24bdffaaaa62a6e9fdf3d36311afcfe6546bc2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eda02a76d6de6c43feb16692efe305c70078fb97dafb1dca8b008ab7d4f4a71aaecd45fc709ea7676eeb6e83356bd8f1470d5aa0232cf695cb087ed25b884dba
|
7
|
+
data.tar.gz: f1110abfaf267702766c3ffb24011938e5a14b62bea80cf8f551fc297699a4b50c8965e4d3862442720cb1a9cb03e527f5e6e3e343cd757926d4000ac1e053c5
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# S-3PO
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/s-3po.svg)](http://badge.fury.io/rb/s-3po) [![security](https://hakiri.io/github/kenjij/s-3po/master.svg)](https://hakiri.io/github/kenjij/s-3po/master) [![Code Climate](https://codeclimate.com/github/kenjij/s-3po/badges/gpa.svg)](https://codeclimate.com/github/kenjij/s-3po)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/s-3po.svg)](http://badge.fury.io/rb/s-3po) [![security](https://hakiri.io/github/kenjij/s-3po/master.svg)](https://hakiri.io/github/kenjij/s-3po/master) [![Code Climate](https://codeclimate.com/github/kenjij/s-3po/badges/gpa.svg)](https://codeclimate.com/github/kenjij/s-3po) [![Dependency Status](https://gemnasium.com/kenjij/s-3po.svg)](https://gemnasium.com/kenjij/s-3po)
|
4
4
|
|
5
|
-
A protocol droid made by Cybot Galactica for Slack.
|
5
|
+
A protocol droid made by [Cybot Galactica](http://starwars.wikia.com/wiki/Cybot_Galactica) for [Slack](https://slack.com).
|
6
6
|
|
7
7
|
This gem parses, generates, and manupilates various Slack events and messages.
|
8
8
|
|
@@ -27,7 +27,7 @@ require 's-3po'
|
|
27
27
|
Your bot would connect to Slack via [RTM API](https://api.slack.com/rtm). Then process events like this.
|
28
28
|
|
29
29
|
```ruby
|
30
|
-
event = S3PO.parse_event(data_from_slack)
|
30
|
+
event = S3PO.parse_event(data_from_slack, {botid: bot_id})
|
31
31
|
puts "#{event.type} : #{event.subtype}"
|
32
32
|
puts event.plain if event.is_simplemessage?
|
33
33
|
# => "@U123ABC: hello you!"
|
data/lib/s-3po/events.rb
CHANGED
@@ -4,10 +4,12 @@ module S3PO
|
|
4
4
|
class Event
|
5
5
|
|
6
6
|
attr_reader :object
|
7
|
+
attr_reader :options
|
7
8
|
|
8
|
-
def initialize(event = {})
|
9
|
+
def initialize(event = {}, opts = {})
|
9
10
|
fail 'Must be a Hash.' unless event.class == Hash
|
10
11
|
@object = event
|
12
|
+
@options = opts
|
11
13
|
end
|
12
14
|
|
13
15
|
# @return [Symbol]
|
@@ -39,6 +41,12 @@ module S3PO
|
|
39
41
|
object[:user]
|
40
42
|
end
|
41
43
|
|
44
|
+
# Is is an IM, direct channel?
|
45
|
+
# @return [Boolean]
|
46
|
+
def is_im?
|
47
|
+
!object[:channel].nil? && channel.start_with?('D')
|
48
|
+
end
|
49
|
+
|
42
50
|
def channel
|
43
51
|
object[:channel]
|
44
52
|
end
|
@@ -59,11 +67,6 @@ module S3PO
|
|
59
67
|
# Message Event class comes from Slack, or you would create one to send a message.
|
60
68
|
class Message < Event
|
61
69
|
|
62
|
-
def initialize(event = {})
|
63
|
-
event[:type] = :message
|
64
|
-
super(event)
|
65
|
-
end
|
66
|
-
|
67
70
|
def text
|
68
71
|
object[:text]
|
69
72
|
end
|
@@ -88,9 +91,24 @@ module S3PO
|
|
88
91
|
@mentions ||= Parser.mentions_from_text(text)
|
89
92
|
end
|
90
93
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
+
# Is the message directed to me and/or prefixed with bot username?
|
95
|
+
# @return [Boolean]
|
96
|
+
def is_instruction?
|
97
|
+
return false unless plain
|
98
|
+
withprefix = /^@#{options[:botid]}[!:;,.-]*\s+\S/
|
99
|
+
withoutprefix = /^[^@]/
|
100
|
+
return true if (withprefix =~ plain) == 0
|
101
|
+
if is_im?
|
102
|
+
return true if (withoutprefix =~ plain) == 0
|
103
|
+
end
|
104
|
+
return false
|
105
|
+
end
|
106
|
+
|
107
|
+
# Break up the message text into an Arrary per word when prefixed with bot username; e.g., "@bot echo hello"
|
108
|
+
# @return [Array] excluding the prefixed bot username
|
109
|
+
def instruction
|
110
|
+
return nil unless is_instruction?
|
111
|
+
@instruction ||= Parser.instruction_from_plain(plain, options[:botid])
|
94
112
|
end
|
95
113
|
|
96
114
|
end
|
data/lib/s-3po/parser.rb
CHANGED
@@ -5,11 +5,11 @@ module S3PO
|
|
5
5
|
|
6
6
|
class Parser
|
7
7
|
|
8
|
-
def self.parse_event(event)
|
8
|
+
def self.parse_event(event, opts = {})
|
9
9
|
obj = JSON.parse(event, {symbolize_names: true})
|
10
|
-
return Response.new(obj) if obj[:type].nil?
|
11
|
-
return Message.new(obj) if obj[:type] == 'message'
|
12
|
-
return Event.new(obj)
|
10
|
+
return Response.new(obj, opts) if obj[:type].nil?
|
11
|
+
return Message.new(obj, opts) if obj[:type] == 'message'
|
12
|
+
return Event.new(obj, opts)
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.mentions_from_text(text)
|
@@ -19,7 +19,7 @@ module S3PO
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.plain_from_text(text)
|
22
|
-
#
|
22
|
+
# copy
|
23
23
|
plain = String.new(text)
|
24
24
|
# remove labels within brackets
|
25
25
|
plain.gsub!(/<([^>|]*)[^>]*>/, '<\1>')
|
@@ -35,10 +35,13 @@ module S3PO
|
|
35
35
|
return plain
|
36
36
|
end
|
37
37
|
|
38
|
-
def self.
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
def self.instruction_from_plain(plain, botid)
|
39
|
+
a = plain.split()
|
40
|
+
if a[0] && a[0].start_with?("@#{botid}")
|
41
|
+
prefix = a.shift
|
42
|
+
end
|
43
|
+
return nil if a.empty?
|
44
|
+
return a
|
42
45
|
end
|
43
46
|
|
44
47
|
end
|
data/lib/s-3po.rb
CHANGED
@@ -7,9 +7,10 @@ module S3PO
|
|
7
7
|
|
8
8
|
# Parse Slack event into an S3PO Event object.
|
9
9
|
# @param event [String] event from Slack in JSON string
|
10
|
+
# @param opts [Hash] options
|
10
11
|
# @return [Object] an S3PO::Event object
|
11
|
-
def self.parse_event(event)
|
12
|
-
return Parser.parse_event(event)
|
12
|
+
def self.parse_event(event, opts = {})
|
13
|
+
return Parser.parse_event(event, opts)
|
13
14
|
end
|
14
15
|
|
15
16
|
# Generate JSON message to send to Slack.
|
data/s-3po.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s-3po
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ken J.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Slack message formatter gem
|
14
14
|
email:
|