chatterbot 0.7.1 → 0.9.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.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/Gemfile +7 -5
  4. data/README.markdown +1 -1
  5. data/Rakefile +8 -0
  6. data/bin/chatterbot-blacklist +2 -0
  7. data/bin/chatterbot-register +2 -0
  8. data/bin/chatterbot-status +2 -0
  9. data/chatterbot.gemspec +6 -6
  10. data/examples/streaming_bot.rb +42 -0
  11. data/lib/chatterbot.rb +16 -1
  12. data/lib/chatterbot/blacklist.rb +3 -1
  13. data/lib/chatterbot/bot.rb +4 -0
  14. data/lib/chatterbot/client.rb +21 -26
  15. data/lib/chatterbot/config.rb +26 -30
  16. data/lib/chatterbot/db.rb +2 -0
  17. data/lib/chatterbot/dsl.rb +117 -3
  18. data/lib/chatterbot/favorite.rb +23 -0
  19. data/lib/chatterbot/followers.rb +9 -0
  20. data/lib/chatterbot/helpers.rb +3 -1
  21. data/lib/chatterbot/logging.rb +4 -3
  22. data/lib/chatterbot/profile.rb +40 -0
  23. data/lib/chatterbot/reply.rb +10 -2
  24. data/lib/chatterbot/retweet.rb +10 -4
  25. data/lib/chatterbot/search.rb +12 -6
  26. data/lib/chatterbot/skeleton.rb +6 -0
  27. data/lib/chatterbot/streaming.rb +67 -0
  28. data/lib/chatterbot/streaming_handler.rb +96 -0
  29. data/lib/chatterbot/tweet.rb +2 -0
  30. data/lib/chatterbot/ui.rb +2 -1
  31. data/lib/chatterbot/utils.rb +11 -0
  32. data/lib/chatterbot/version.rb +1 -1
  33. data/spec/blacklist_spec.rb +24 -23
  34. data/spec/bot_spec.rb +12 -0
  35. data/spec/client_spec.rb +36 -32
  36. data/spec/config_spec.rb +135 -85
  37. data/spec/db_spec.rb +5 -5
  38. data/spec/dsl_spec.rb +74 -27
  39. data/spec/favorite_spec.rb +34 -0
  40. data/spec/followers_spec.rb +32 -11
  41. data/spec/helpers_spec.rb +25 -20
  42. data/spec/logging_spec.rb +16 -15
  43. data/spec/profile_spec.rb +65 -0
  44. data/spec/reply_spec.rb +24 -24
  45. data/spec/retweet_spec.rb +17 -9
  46. data/spec/search_spec.rb +26 -26
  47. data/spec/skeleton_spec.rb +6 -6
  48. data/spec/spec_helper.rb +44 -32
  49. data/spec/streaming_handler_spec.rb +78 -0
  50. data/spec/streaming_spec.rb +172 -0
  51. data/spec/tweet_spec.rb +18 -18
  52. data/spec/utils_spec.rb +29 -0
  53. data/specs.watchr +1 -13
  54. metadata +27 -16
@@ -0,0 +1,78 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Chatterbot::StreamingHandler" do
4
+ let(:bot) { test_bot }
5
+ let(:handler) { StreamingHandler.new(test_bot) }
6
+
7
+ def apply_to_handler(&block)
8
+ handler.apply block
9
+ end
10
+
11
+ describe "endpoint" do
12
+ it "should default to :user" do
13
+ expect(handler.endpoint).to eql(:user)
14
+ end
15
+
16
+ it "should pull from opts" do
17
+ expect(StreamingHandler.new(test_bot, {:endpoint => :bar}).endpoint).to eql(:bar)
18
+ end
19
+ end
20
+
21
+ it "should set search query" do
22
+ apply_to_handler { search 'foo' }
23
+ expect(handler.search_filter).to eql('foo')
24
+ end
25
+
26
+ it "should set tweet_handler" do
27
+ apply_to_handler { replies {'bar'} }
28
+ expect(handler.tweet_handler.call).to eql('bar')
29
+ end
30
+
31
+ it "should set favorited_handler" do
32
+ apply_to_handler { favorited {'bar'} }
33
+ expect(handler.favorite_handler.call).to eql('bar')
34
+ end
35
+
36
+ it "should set dm_handler" do
37
+ apply_to_handler { direct_message {'bar'} }
38
+ expect(handler.dm_handler.call).to eql('bar')
39
+ end
40
+
41
+ it "should set follow_handler" do
42
+ apply_to_handler { followed {'bar'} }
43
+ expect(handler.follow_handler.call).to eql('bar')
44
+ end
45
+
46
+ it "should set delete_handler" do
47
+ apply_to_handler { delete {'bar'} }
48
+ expect(handler.delete_handler.call).to eql('bar')
49
+ end
50
+
51
+ it "should set friends_handler" do
52
+ apply_to_handler { friends {'bar'} }
53
+ expect(handler.friends_handler.call).to eql('bar')
54
+ end
55
+
56
+ it "should pull config from bot" do
57
+ expect(handler.bot).to receive(:config).and_return('config')
58
+ expect(handler.config).to eql('config')
59
+ end
60
+
61
+ it "limits to one main block type" do
62
+ apply_to_handler { replies {'bar'} }
63
+ expect{
64
+ apply_to_handler { replies {'bar'} }
65
+ }.to raise_error(RuntimeError)
66
+ end
67
+
68
+ describe "connection_params" do
69
+ it "has defaults" do
70
+ expect(handler.connection_params[:stall_warnings]).to eql("false")
71
+ end
72
+
73
+ it "applies search filter" do
74
+ apply_to_handler { search 'foo' }
75
+ expect(handler.connection_params[:track]).to eql("foo")
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,172 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class StreamingHandler
4
+ attr_accessor :last_object
5
+ end
6
+
7
+ describe "Chatterbot::Streaming" do
8
+ let(:bot) { test_bot }
9
+ let(:user) { fake_user('user', 100) }
10
+ let(:handler) { StreamingHandler.new(test_bot) }
11
+ let(:tweet) { fake_tweet(12345) }
12
+
13
+ def apply_to_handler(&block)
14
+ handler.apply block
15
+ end
16
+
17
+ describe "authenticated_user" do
18
+ it "should get user from client" do
19
+ expect(bot.client).to receive(:user).and_return('user')
20
+ expect(bot.authenticated_user).to eql('user')
21
+ end
22
+ end
23
+
24
+ describe "do_streaming" do
25
+
26
+ end
27
+
28
+ describe "handle_streaming_object" do
29
+ before(:each) {
30
+ allow(bot.client).to receive(:user).and_return(user)
31
+ }
32
+
33
+ describe "Twitter::Tweet" do
34
+ it "works if no handler" do
35
+ bot.handle_streaming_object(tweet, handler)
36
+ end
37
+
38
+ context "with handler" do
39
+ before(:each) do
40
+ apply_to_handler { replies { |t| @last_object = t } }
41
+ end
42
+
43
+ it "ignores tweets from authenticated user" do
44
+ expect(tweet).to receive(:user).and_return(user)
45
+ bot.handle_streaming_object(tweet, handler)
46
+ expect(handler.last_object).to be_nil
47
+ end
48
+
49
+ it "passes to handler" do
50
+ bot.handle_streaming_object(tweet, handler)
51
+ expect(handler.last_object).to eql(tweet)
52
+ end
53
+
54
+ it "ignores tweets from blacklist" do
55
+ bot.blacklist = ['chatterbot']
56
+ bot.handle_streaming_object(tweet, handler)
57
+ expect(handler.last_object).to be_nil
58
+ end
59
+
60
+ it "ignores tweets if skip_me is true" do
61
+ bot.exclude = ['tweet']
62
+ bot.handle_streaming_object(tweet, handler)
63
+ expect(handler.last_object).to be_nil
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "Twitter::Streaming::DeletedTweet" do
69
+ it "works if no handler" do
70
+ obj = Twitter::Streaming::DeletedTweet.new(:id => 1)
71
+ bot.handle_streaming_object(obj, handler)
72
+ end
73
+
74
+ it "passes to handler" do
75
+ apply_to_handler { delete { |t| @last_object = t } }
76
+ obj = Twitter::Streaming::DeletedTweet.new(:id => 1)
77
+ bot.handle_streaming_object(obj, handler)
78
+ expect(handler.last_object).to eql(obj)
79
+ end
80
+ end
81
+
82
+ describe "Twitter::DirectMessage" do
83
+ it "works if no handler" do
84
+ obj = Twitter::DirectMessage.new(:id => 1)
85
+ bot.handle_streaming_object(obj, handler)
86
+ end
87
+
88
+ it "passes to handler" do
89
+ apply_to_handler { direct_message { |t| @last_object = t } }
90
+ obj = Twitter::DirectMessage.new(:id => 1)
91
+ bot.handle_streaming_object(obj, handler)
92
+ expect(handler.last_object).to eql(obj)
93
+ end
94
+ end
95
+
96
+ describe "Twitter::Streaming::Event" do
97
+ it "ignores events generated by authenticated user" do
98
+ event = Twitter::Streaming::Event.new(
99
+ :event => :follow,
100
+ :source => {:id => user.id, :name => 'name', :screen_name => 'name'},
101
+ :target => {:id => user.id, :name => 'name', :screen_name => 'name'})
102
+
103
+ apply_to_handler { followed { |t| @last_object = t } }
104
+ bot.handle_streaming_object(event, handler)
105
+ expect(handler.last_object).to be_nil
106
+ end
107
+
108
+ describe "follow" do
109
+ before(:each) do
110
+ @event = Twitter::Streaming::Event.new(
111
+ :event => :follow,
112
+ :source => {:id => 12345, :name => 'name', :screen_name => 'name'},
113
+ :target => {:id => user.id, :name => 'name', :screen_name => 'name'})
114
+
115
+ end
116
+
117
+ it "works if no handler" do
118
+ bot.handle_streaming_object(@event, handler)
119
+ expect(handler.last_object).to be_nil
120
+ end
121
+
122
+ it "passes to handler" do
123
+ apply_to_handler { followed { |t| @last_object = t } }
124
+ bot.handle_streaming_object(@event, handler)
125
+ expect(handler.last_object.class).to be(Twitter::User)
126
+ expect(handler.last_object.id).to be(12345)
127
+ end
128
+ end
129
+
130
+ describe "favorite" do
131
+ before(:each) do
132
+ @event = Twitter::Streaming::Event.new(
133
+ :event => :favorite,
134
+ :source => {:id => 12345, :name => 'name', :screen_name => 'name'},
135
+ :target => {:id => user.id, :name => 'name', :screen_name => 'name'},
136
+ :target_object => {
137
+ :from_user => "chatterbot",
138
+ :id => 56789,
139
+ :text => "I am a tweet!",
140
+ :user => { :id => 1, :screen_name => "chatterbot" }
141
+ })
142
+ end
143
+
144
+ it "works if no handler" do
145
+ bot.handle_streaming_object(@event, handler)
146
+ expect(handler.last_object).to be_nil
147
+ end
148
+
149
+ it "passes to handler" do
150
+ apply_to_handler { favorited { |_, t| @last_object = t } }
151
+ bot.handle_streaming_object(@event, handler)
152
+ expect(handler.last_object.class).to be(Twitter::Tweet)
153
+ expect(handler.last_object.text).to eq("I am a tweet!")
154
+ end
155
+ end
156
+ end
157
+
158
+ describe "Twitter::Streaming::FriendList" do
159
+ it "works if no handler" do
160
+ obj = Twitter::Streaming::FriendList.new
161
+ bot.handle_streaming_object(obj, handler)
162
+ end
163
+
164
+ it "passes to handler" do
165
+ apply_to_handler { friends { |t| @last_object = t } }
166
+ obj = Twitter::Streaming::FriendList.new
167
+ bot.handle_streaming_object(obj, handler)
168
+ expect(handler.last_object).to eql(obj)
169
+ end
170
+ end
171
+ end
172
+ end
@@ -7,32 +7,32 @@ describe "Chatterbot::Tweet" do
7
7
  end
8
8
 
9
9
  it "calls require_login when tweeting" do
10
- @bot.should_receive(:require_login).and_return(false)
10
+ expect(@bot).to receive(:require_login).and_return(false)
11
11
  @bot.tweet "tweet test!"
12
12
  end
13
13
 
14
14
  it "calls client.update with the right values" do
15
15
  bot = test_bot
16
16
 
17
- bot.should_receive(:require_login).and_return(true)
18
- bot.stub(:client).and_return(double(Twitter::Client))
17
+ expect(bot).to receive(:require_login).and_return(true)
18
+ allow(bot).to receive(:client).and_return(double(Twitter::Client))
19
19
 
20
- bot.stub(:debug_mode?).and_return(false)
20
+ allow(bot).to receive(:debug_mode?).and_return(false)
21
21
 
22
22
  test_str = "test!"
23
- bot.client.should_receive(:update).with(test_str, {})
23
+ expect(bot.client).to receive(:update).with(test_str, {})
24
24
  bot.tweet test_str
25
25
  end
26
26
 
27
27
  it "doesn't tweet when debug_mode? is set" do
28
28
  bot = test_bot
29
29
 
30
- bot.should_receive(:require_login).and_return(true)
31
- bot.stub(:client).and_return(double(Twitter::Client))
30
+ expect(bot).to receive(:require_login).and_return(true)
31
+ allow(bot).to receive(:client).and_return(double(Twitter::Client))
32
32
 
33
- bot.stub(:debug_mode?).and_return(true)
33
+ allow(bot).to receive(:debug_mode?).and_return(true)
34
34
 
35
- bot.client.should_not_receive(:update)
35
+ expect(bot.client).not_to receive(:update)
36
36
  bot.tweet "no tweet!"
37
37
  end
38
38
  end
@@ -40,35 +40,35 @@ describe "Chatterbot::Tweet" do
40
40
  describe "#reply" do
41
41
  it "calls require_login when replying" do
42
42
  bot = test_bot
43
- bot.should_receive(:require_login).and_return(false)
43
+ expect(bot).to receive(:require_login).and_return(false)
44
44
  bot.reply "reply test!", {"id" => 100}
45
45
  end
46
46
 
47
47
  it "calls client.update with the right values" do
48
48
  bot = test_bot
49
- bot.should_receive(:require_login).and_return(true)
50
- bot.stub(:client).and_return(double(Twitter::Client))
49
+ expect(bot).to receive(:require_login).and_return(true)
50
+ allow(bot).to receive(:client).and_return(double(Twitter::Client))
51
51
 
52
- bot.stub(:debug_mode?).and_return(false)
52
+ allow(bot).to receive(:debug_mode?).and_return(false)
53
53
 
54
54
  test_str = "test!"
55
55
 
56
56
  s = {
57
57
  :id => 100
58
58
  }
59
- bot.client.should_receive(:update).with(test_str, {:in_reply_to_status_id => 100})
59
+ expect(bot.client).to receive(:update).with(test_str, {:in_reply_to_status_id => 100})
60
60
  bot.reply test_str, s
61
61
  end
62
62
 
63
63
 
64
64
  it "doesn't reply when debug_mode? is set" do
65
65
  bot = test_bot
66
- bot.should_receive(:require_login).and_return(true)
67
- bot.stub(:client).and_return(double(Twitter::Client))
66
+ expect(bot).to receive(:require_login).and_return(true)
67
+ allow(bot).to receive(:client).and_return(double(Twitter::Client))
68
68
 
69
- bot.stub(:debug_mode?).and_return(true)
69
+ allow(bot).to receive(:debug_mode?).and_return(true)
70
70
 
71
- bot.client.should_not_receive(:update)
71
+ expect(bot.client).not_to receive(:update)
72
72
  bot.reply "no reply test!", {:id => 100}
73
73
  end
74
74
  end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Chatterbot::Utils" do
4
+ before(:each) do
5
+ @bot = test_bot
6
+ end
7
+
8
+ describe "id_from_tweet" do
9
+ it "works with numbers" do
10
+ expect(@bot.id_from_tweet(1234)).to eq(1234)
11
+ end
12
+
13
+ it "works with tweets" do
14
+ t = Twitter::Tweet.new(:id => 7890, :text => "did you know that i hate bots?")
15
+ expect(@bot.id_from_tweet(t)).to eq(7890)
16
+ end
17
+ end
18
+
19
+ describe "id_from_user" do
20
+ it "works with numbers" do
21
+ expect(@bot.id_from_user(1234)).to eq(1234)
22
+ end
23
+
24
+ it "works with users" do
25
+ t = Twitter::User.new(:id => 2345, :name => "name")
26
+ expect(@bot.id_from_user(t)).to eq(2345)
27
+ end
28
+ end
29
+ end
@@ -20,7 +20,7 @@ end
20
20
 
21
21
  def run(files_to_run)
22
22
  puts("Running: #{files_to_run}")
23
- system("clear;rspec -cfs #{files_to_run}")
23
+ system("clear;rspec -cfd #{files_to_run}")
24
24
  no_int_for_you
25
25
  end
26
26
 
@@ -45,16 +45,4 @@ def no_int_for_you
45
45
  @sent_an_int = nil
46
46
  end
47
47
 
48
- Signal.trap 'INT' do
49
- if @sent_an_int then
50
- puts " A second INT? Ok, I get the message. Shutting down now."
51
- exit
52
- else
53
- puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again."
54
- @sent_an_int = true
55
- Kernel.sleep 1.5
56
- run_all_specs
57
- end
58
- end
59
-
60
48
  run_all_specs
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chatterbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Mitchell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-18 00:00:00.000000000 Z
11
+ date: 2014-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redcarpet
@@ -30,28 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.4.7
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.4.7
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: twitter
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: 4.8.1
47
+ version: 5.8.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: 4.8.1
54
+ version: 5.8.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: launchy
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -112,30 +112,30 @@ dependencies:
112
112
  name: rspec
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - '>='
115
+ - - ~>
116
116
  - !ruby/object:Gem::Version
117
- version: 2.14.1
117
+ version: 3.0.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>='
122
+ - - ~>
123
123
  - !ruby/object:Gem::Version
124
- version: 2.14.1
124
+ version: 3.0.0
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rspec-mocks
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '>='
129
+ - - ~>
130
130
  - !ruby/object:Gem::Version
131
- version: 2.14.1
131
+ version: 3.0.2
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '>='
136
+ - - ~>
137
137
  - !ruby/object:Gem::Version
138
- version: 2.14.1
138
+ version: 3.0.2
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: rdoc
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -165,7 +165,7 @@ dependencies:
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
167
  - !ruby/object:Gem::Dependency
168
- name: watchr
168
+ name: observr
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
171
  - - '>='
@@ -205,6 +205,7 @@ files:
205
205
  - examples/echoes_bot.rb
206
206
  - examples/loop_bot.rb
207
207
  - examples/search_bot.rb
208
+ - examples/streaming_bot.rb
208
209
  - examples/tweet_logger.rb
209
210
  - lib/chatterbot.rb
210
211
  - lib/chatterbot/blacklist.rb
@@ -213,15 +214,20 @@ files:
213
214
  - lib/chatterbot/config.rb
214
215
  - lib/chatterbot/db.rb
215
216
  - lib/chatterbot/dsl.rb
217
+ - lib/chatterbot/favorite.rb
216
218
  - lib/chatterbot/followers.rb
217
219
  - lib/chatterbot/helpers.rb
218
220
  - lib/chatterbot/logging.rb
221
+ - lib/chatterbot/profile.rb
219
222
  - lib/chatterbot/reply.rb
220
223
  - lib/chatterbot/retweet.rb
221
224
  - lib/chatterbot/search.rb
222
225
  - lib/chatterbot/skeleton.rb
226
+ - lib/chatterbot/streaming.rb
227
+ - lib/chatterbot/streaming_handler.rb
223
228
  - lib/chatterbot/tweet.rb
224
229
  - lib/chatterbot/ui.rb
230
+ - lib/chatterbot/utils.rb
225
231
  - lib/chatterbot/version.rb
226
232
  - spec/blacklist_spec.rb
227
233
  - spec/bot_spec.rb
@@ -229,15 +235,20 @@ files:
229
235
  - spec/config_spec.rb
230
236
  - spec/db_spec.rb
231
237
  - spec/dsl_spec.rb
238
+ - spec/favorite_spec.rb
232
239
  - spec/followers_spec.rb
233
240
  - spec/helpers_spec.rb
234
241
  - spec/logging_spec.rb
242
+ - spec/profile_spec.rb
235
243
  - spec/reply_spec.rb
236
244
  - spec/retweet_spec.rb
237
245
  - spec/search_spec.rb
238
246
  - spec/skeleton_spec.rb
239
247
  - spec/spec_helper.rb
248
+ - spec/streaming_handler_spec.rb
249
+ - spec/streaming_spec.rb
240
250
  - spec/tweet_spec.rb
251
+ - spec/utils_spec.rb
241
252
  - specs.watchr
242
253
  - templates/skeleton.txt
243
254
  homepage: http://github.com/muffinista/chatterbot
@@ -260,7 +271,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
260
271
  version: '0'
261
272
  requirements: []
262
273
  rubyforge_project: chatterbot
263
- rubygems_version: 2.1.11
274
+ rubygems_version: 2.2.2
264
275
  signing_key:
265
276
  specification_version: 4
266
277
  summary: A ruby framework for writing Twitter bots