scamp 0.0.3 → 0.0.4
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.
- data/README.md +3 -3
- data/examples/bot.rb +0 -10
- data/lib/scamp/channels.rb +1 -1
- data/lib/scamp/connection.rb +11 -4
- data/lib/scamp/matcher.rb +2 -13
- data/lib/scamp/version.rb +1 -1
- data/lib/scamp.rb +3 -2
- data/spec/lib/scamp_spec.rb +151 -2
- data/spec/spec_helper.rb +1 -0
- metadata +12 -12
data/README.md
CHANGED
@@ -41,15 +41,15 @@ Matchers are tested in order and all that satisfy the match and conditions will
|
|
41
41
|
#
|
42
42
|
# Limit the match to certain channels, users or both.
|
43
43
|
#
|
44
|
-
match /^Lets match (.+)$/, :conditions => {:channel =>
|
44
|
+
match /^Lets match (.+)$/, :conditions => {:channel => "Some Channel"} do
|
45
45
|
say "Only said if channel name mathces /someregex/"
|
46
46
|
end
|
47
47
|
|
48
|
-
match "some text", :conditions => {:user =>
|
48
|
+
match "some text", :conditions => {:user => "Some User"} do
|
49
49
|
say "Only said if user name mathces /someregex/"
|
50
50
|
end
|
51
51
|
|
52
|
-
match /some other text/, :conditions => {:user =>
|
52
|
+
match /some other text/, :conditions => {:user => "Some User", :channel => 123456} do
|
53
53
|
say "You can mix conditions"
|
54
54
|
end
|
55
55
|
|
data/examples/bot.rb
CHANGED
@@ -18,16 +18,6 @@ scamp.behaviour do
|
|
18
18
|
say "Limit a match to a channel condition based on a string"
|
19
19
|
end
|
20
20
|
|
21
|
-
# Limit a match to a channel condition based on a regex
|
22
|
-
match /^channel regex (.+)$/, :conditions => {:channel => /someregex/} do
|
23
|
-
say "Limit a match to a channel condition based on a regex"
|
24
|
-
end
|
25
|
-
|
26
|
-
# Limit a match to a user condition based on a regex
|
27
|
-
match /^user regex (.+)$/, :conditions => {:user => /someregex/} do
|
28
|
-
say "Limit a match to a user condition based on a regex"
|
29
|
-
end
|
30
|
-
|
31
21
|
# Limit a match to a user condition based on a string
|
32
22
|
match /^user name (.+)$/, :conditions => {:user => "Will Jessop"} do
|
33
23
|
say "Limit a match to a user condition based on a string"
|
data/lib/scamp/channels.rb
CHANGED
@@ -100,7 +100,7 @@ class Scamp
|
|
100
100
|
# Timeout per https://github.com/igrigorik/em-http-request/wiki/Redirects-and-Timeouts
|
101
101
|
http = EventMachine::HttpRequest.new(url, :connect_timeout => 20, :inactivity_timeout => 0).get :head => {'authorization' => [api_key, 'X']}
|
102
102
|
http.errback { logger.error "Couldn't stream channel #{channel_id} at url #{url}" }
|
103
|
-
http.callback { logger.error "Disconnected from #{url}" }
|
103
|
+
http.callback { logger.error "Disconnected from #{url}"; channels_to_join << channel_id}
|
104
104
|
http.stream {|chunk| json_parser << chunk }
|
105
105
|
end
|
106
106
|
|
data/lib/scamp/connection.rb
CHANGED
@@ -2,14 +2,21 @@ class Scamp
|
|
2
2
|
module Connection
|
3
3
|
private
|
4
4
|
|
5
|
-
def connect(api_key,
|
5
|
+
def connect(api_key, channel_list)
|
6
6
|
EventMachine.run do
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
|
8
|
+
# Check for channels to join, and join them
|
9
|
+
EventMachine::add_periodic_timer(5) do
|
10
|
+
while id = @channels_to_join.pop
|
10
11
|
join_and_stream(id)
|
11
12
|
end
|
12
13
|
end
|
14
|
+
|
15
|
+
populate_channel_list do
|
16
|
+
logger.debug "Adding #{channel_list.join ', '} to list of channels to join"
|
17
|
+
@channels_to_join = channel_list.map{|c| channel_id(c) }
|
18
|
+
end
|
19
|
+
|
13
20
|
end
|
14
21
|
end
|
15
22
|
|
data/lib/scamp/matcher.rb
CHANGED
@@ -43,18 +43,10 @@ class Scamp
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def conditions_satisfied_by(msg)
|
46
|
-
|
47
|
-
bot.logger.info "Checking message against #{conditions.inspect}"
|
48
|
-
|
49
|
-
# nick
|
50
|
-
# channel name
|
51
|
-
# nick regex
|
52
|
-
# channel regex
|
53
|
-
|
54
|
-
#{"room_id":1,"created_at":"2009-12-01 23:44:40","body":"hello","id":1,"user_id":1,"type":"TextMessage"}
|
46
|
+
bot.logger.debug "Checking message against #{conditions.inspect}"
|
55
47
|
|
56
48
|
# item will be :nick or :channel
|
57
|
-
# cond is the
|
49
|
+
# cond is the int or string value.
|
58
50
|
conditions.each do |item, cond|
|
59
51
|
bot.logger.debug "Checking #{item} against #{cond}"
|
60
52
|
bot.logger.debug "msg is #{msg.inspect}"
|
@@ -69,9 +61,6 @@ class Scamp
|
|
69
61
|
return false unless bot.username_for(msg[:user_id]) == cond
|
70
62
|
end
|
71
63
|
bot.logger.error "Don't know how to deal with a match item of #{item}, cond #{cond}"
|
72
|
-
elsif cond.is_a? Regexp
|
73
|
-
return false
|
74
|
-
return false unless msg[item].match(cond)
|
75
64
|
end
|
76
65
|
end
|
77
66
|
true
|
data/lib/scamp/version.rb
CHANGED
data/lib/scamp.rb
CHANGED
@@ -15,7 +15,7 @@ class Scamp
|
|
15
15
|
include Channels
|
16
16
|
include Users
|
17
17
|
|
18
|
-
attr_accessor :channels, :user_cache, :channel_cache, :matchers, :api_key, :subdomain, :logger, :verbose, :first_match_only
|
18
|
+
attr_accessor :channels, :user_cache, :channel_cache, :matchers, :api_key, :subdomain, :logger, :verbose, :first_match_only, :channels_to_join
|
19
19
|
|
20
20
|
def initialize(options = {})
|
21
21
|
options ||= {}
|
@@ -30,7 +30,8 @@ class Scamp
|
|
30
30
|
logger.warn "Scamp initialized with #{k.inspect} => #{v.inspect} but NO UNDERSTAND!"
|
31
31
|
end
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
|
+
@channels_to_join = []
|
34
35
|
@channels = {}
|
35
36
|
@user_cache = {}
|
36
37
|
@channel_cache = {}
|
data/spec/lib/scamp_spec.rb
CHANGED
@@ -3,6 +3,8 @@ require "spec_helper"
|
|
3
3
|
describe Scamp do
|
4
4
|
before do
|
5
5
|
@valid_params = {:api_key => "6124d98749365e3db2c9e5b27ca04db6", :subdomain => "oxygen"}
|
6
|
+
@valid_user_cache_data = {123 => {"name" => "foo"}, 456 => {"name" => "bar"}}
|
7
|
+
@valid_channel_cache_data = {123 => {"name" => "foo"}, 456 => {"name" => "bar"}}
|
6
8
|
end
|
7
9
|
|
8
10
|
describe "#initialize" do
|
@@ -86,8 +88,155 @@ describe Scamp do
|
|
86
88
|
end
|
87
89
|
end
|
88
90
|
end
|
89
|
-
|
90
|
-
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "matching" do
|
94
|
+
|
95
|
+
context "with conditions" do
|
96
|
+
it "should limit matches by channel id" do
|
97
|
+
canary = mock
|
98
|
+
canary.expects(:lives).once
|
99
|
+
canary.expects(:bang).never
|
100
|
+
|
101
|
+
bot = a Scamp
|
102
|
+
bot.behaviour do
|
103
|
+
match("a string", :conditions => {:channel => 123}) {canary.lives}
|
104
|
+
match("a string", :conditions => {:channel => 456}) {canary.bang}
|
105
|
+
end
|
106
|
+
|
107
|
+
bot.send(:process_message, {:room_id => 123, :body => "a string"})
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should limit matches by channel name" do
|
111
|
+
canary = mock
|
112
|
+
canary.expects(:lives).once
|
113
|
+
canary.expects(:bang).never
|
114
|
+
|
115
|
+
bot = a Scamp
|
116
|
+
bot.behaviour do
|
117
|
+
match("a string", :conditions => {:channel => "foo"}) {canary.lives}
|
118
|
+
match("a string", :conditions => {:channel => "bar"}) {canary.bang}
|
119
|
+
end
|
120
|
+
|
121
|
+
bot.channel_cache = @valid_channel_cache_data
|
122
|
+
|
123
|
+
bot.send(:process_message, {:room_id => 123, :body => "a string"})
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should limit matches by user id" do
|
127
|
+
canary = mock
|
128
|
+
canary.expects(:lives).once
|
129
|
+
canary.expects(:bang).never
|
130
|
+
|
131
|
+
bot = a Scamp
|
132
|
+
bot.behaviour do
|
133
|
+
match("a string", :conditions => {:user => 123}) {canary.lives}
|
134
|
+
match("a string", :conditions => {:user => 456}) {canary.bang}
|
135
|
+
end
|
136
|
+
|
137
|
+
bot.send(:process_message, {:user_id => 123, :body => "a string"})
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should limit matches by user name" do
|
141
|
+
canary = mock
|
142
|
+
canary.expects(:lives).once
|
143
|
+
canary.expects(:bang).never
|
144
|
+
|
145
|
+
bot = a Scamp
|
146
|
+
bot.behaviour do
|
147
|
+
match("a string", :conditions => {:user => "foo"}) {canary.lives}
|
148
|
+
match("a string", :conditions => {:user => "bar"}) {canary.bang}
|
149
|
+
end
|
150
|
+
|
151
|
+
bot.user_cache = @valid_user_cache_data
|
152
|
+
|
153
|
+
bot.send(:process_message, {:user_id => 123, :body => "a string"})
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should limit matches by channel and user" do
|
157
|
+
canary = mock
|
158
|
+
canary.expects(:lives).once
|
159
|
+
canary.expects(:bang).never
|
160
|
+
|
161
|
+
bot = a Scamp
|
162
|
+
bot.behaviour do
|
163
|
+
match("a string", :conditions => {:channel => 123, :user => 123}) {canary.lives}
|
164
|
+
match("a string", :conditions => {:channel => 456, :user => 456}) {canary.bang}
|
165
|
+
end
|
166
|
+
|
167
|
+
bot.channel_cache = @valid_channel_cache_data
|
168
|
+
bot.user_cache = @valid_user_cache_data
|
169
|
+
bot.send(:process_message, {:room_id => 123, :user_id => 123, :body => "a string"})
|
170
|
+
bot.send(:process_message, {:room_id => 123, :user_id => 456, :body => "a string"})
|
171
|
+
bot.send(:process_message, {:room_id => 456, :user_id => 123, :body => "a string"})
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe "strings" do
|
176
|
+
it "should match an exact string" do
|
177
|
+
canary = mock
|
178
|
+
canary.expects(:lives).once
|
179
|
+
canary.expects(:bang).never
|
180
|
+
|
181
|
+
bot = a Scamp
|
182
|
+
bot.behaviour do
|
183
|
+
match("a string") {canary.lives}
|
184
|
+
match("another string") {canary.bang}
|
185
|
+
match("a string like no other") {canary.bang}
|
186
|
+
end
|
187
|
+
|
188
|
+
bot.send(:process_message, {:body => "a string"})
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
describe "regexes" do
|
193
|
+
it "should match a regex" do
|
194
|
+
canary = mock
|
195
|
+
canary.expects(:ping).twice
|
196
|
+
|
197
|
+
bot = a Scamp
|
198
|
+
bot.behaviour do
|
199
|
+
match /foo/ do
|
200
|
+
canary.ping
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
bot.send(:process_message, {:body => "something foo other thing"})
|
205
|
+
bot.send(:process_message, {:body => "foomaster"})
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should make named captures vailable as methods" do
|
209
|
+
canary = mock
|
210
|
+
canary.expects(:one).with("first")
|
211
|
+
canary.expects(:two).with("the rest of it")
|
212
|
+
|
213
|
+
bot = a Scamp
|
214
|
+
bot.behaviour do
|
215
|
+
match /^please match (?<yousaidthis>\w+) and (?<andthis>.+)$/ do
|
216
|
+
canary.one(yousaidthis)
|
217
|
+
canary.two(andthis)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
bot.send(:process_message, {:body => "please match first and the rest of it"})
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should make matches available in an array" do
|
225
|
+
canary = mock
|
226
|
+
canary.expects(:one).with("first")
|
227
|
+
canary.expects(:two).with("the rest of it")
|
228
|
+
|
229
|
+
bot = a Scamp
|
230
|
+
bot.behaviour do
|
231
|
+
match /^please match (\w+) and (.+)$/ do
|
232
|
+
canary.one(matches[0])
|
233
|
+
canary.two(matches[1])
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
bot.send(:process_message, {:body => "please match first and the rest of it"})
|
238
|
+
end
|
239
|
+
end
|
91
240
|
end
|
92
241
|
|
93
242
|
def a klass, params={}
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scamp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-20 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152335860 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.0.0.beta.4
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152335860
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: yajl-ruby
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152335100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.8.3
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152335100
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: em-http-request
|
38
|
-
requirement: &
|
38
|
+
requirement: &2152334240 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.0.0.beta.4
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2152334240
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &2152333240 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 2.6.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2152333240
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: mocha
|
60
|
-
requirement: &
|
60
|
+
requirement: &2152332420 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 0.10.0
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2152332420
|
69
69
|
description: Eventmachine based Campfire bot framework
|
70
70
|
email:
|
71
71
|
- will@willj.net
|