s-3po 0.1.1 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: afee73b5c45855b4388b24e856b14949d2610e9f
4
- data.tar.gz: a89e09d0b8a7f4ff3b1317a2f2acc9277cb88d6e
3
+ metadata.gz: 89544fe67ba616e85dd1e1082c660f2fd58c67e0
4
+ data.tar.gz: b24bdffaaaa62a6e9fdf3d36311afcfe6546bc2c
5
5
  SHA512:
6
- metadata.gz: 99868e8215ad11ec5b15fd5ada25b777d94efec8a55ed5831370b9cb19b0fec5e30ca5aeb066a02a536fc1df50f4af8905922a50f0838319e9fee4d6d88ffc4e
7
- data.tar.gz: 90a62b8f876e46ec1fb52a71bf7617d57b193a07de524233ded804f5205ec4184ee6a10494d4e2b01060f3cf5babbfa8f0c068abd279878c102bfae13162fa07
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
- def commands
93
- @commands ||= Parser.commands_from_text(text)
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
- # Copy
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.commands_from_text(text)
39
- commands = []
40
- text.scan(/<!([^>|]*)[^>]*>/) { |m| commands << m[0]}
41
- return commands
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 's-3po'
3
- s.version = '0.1.1'
3
+ s.version = '0.2.0'
4
4
  s.authors = ['Ken J.']
5
5
  s.email = ['kenjij@gmail.com']
6
6
  s.description = %q{Slack message formatter gem}
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.1.1
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-14 00:00:00.000000000 Z
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Slack message formatter gem
14
14
  email: