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
@@ -11,10 +11,10 @@ describe "Chatterbot::DB" do
11
11
  context "prerequisites" do
12
12
  describe "get_connection" do
13
13
  it "should make sure sequel is actually installed" do
14
- Chatterbot::Bot.any_instance.stub(:has_sequel?) { false }
14
+ allow_any_instance_of(Chatterbot::Bot).to receive(:has_sequel?) { false }
15
15
  @bot = Chatterbot::Bot.new
16
16
  @bot.config[:db_uri] = @db_uri
17
- @bot.should_receive(:display_db_config_notice)
17
+ expect(@bot).to receive(:display_db_config_notice)
18
18
  @bot.db
19
19
  end
20
20
  end
@@ -23,7 +23,7 @@ describe "Chatterbot::DB" do
23
23
  context "db interactions" do
24
24
  before(:each) do
25
25
  @bot = Chatterbot::Bot.new
26
- @bot.stub(:update_config_at_exit)
26
+ allow(@bot).to receive(:update_config_at_exit)
27
27
  @bot.config[:db_uri] = @db_uri
28
28
  end
29
29
 
@@ -35,7 +35,7 @@ describe "Chatterbot::DB" do
35
35
  [:blacklist, :tweets, :config].each do |table|
36
36
  it "should create table #{table}" do
37
37
  @tmp_conn = @bot.db
38
- @tmp_conn.tables.include?(table).should == true
38
+ expect(@tmp_conn.tables.include?(table)).to eq(true)
39
39
  end
40
40
  end
41
41
  end
@@ -46,7 +46,7 @@ describe "Chatterbot::DB" do
46
46
  @bot.config[:db_uri] = @db_uri
47
47
 
48
48
  @bot.db
49
- @bot.store_database_config.should == true
49
+ expect(@bot.store_database_config).to eq(true)
50
50
  end
51
51
  end
52
52
  end
@@ -6,28 +6,28 @@ describe "Chatterbot::DSL" do
6
6
  @bot = double(Chatterbot::Bot, :config => {})
7
7
  @bot.send :require, 'chatterbot/dsl'
8
8
 
9
- Chatterbot::DSL.stub(:bot).and_return(@bot)
9
+ allow(Chatterbot::DSL).to receive(:bot).and_return(@bot)
10
10
  end
11
11
 
12
12
  describe "client" do
13
13
  it "returns the bot object" do
14
- client.should eql(@bot.client)
14
+ expect(client).to eql(@bot.client)
15
15
  end
16
16
  end
17
17
 
18
18
  describe "blacklist" do
19
19
  it "#blacklist passes along to bot object" do
20
- @bot.should_receive(:blacklist=).with(["foo"])
20
+ expect(@bot).to receive(:blacklist=).with(["foo"])
21
21
  blacklist ["foo"]
22
22
  end
23
23
 
24
24
  it "#blacklist turns single-string arg into an array" do
25
- @bot.should_receive(:blacklist=).with(["foo"])
25
+ expect(@bot).to receive(:blacklist=).with(["foo"])
26
26
  blacklist "foo"
27
27
  end
28
28
 
29
29
  it "#blacklist turns comma-delimited string arg into an array" do
30
- @bot.should_receive(:blacklist=).with(["foo", "bar"])
30
+ expect(@bot).to receive(:blacklist=).with(["foo", "bar"])
31
31
  blacklist "foo, bar"
32
32
  end
33
33
  end
@@ -35,117 +35,164 @@ describe "Chatterbot::DSL" do
35
35
  [:no_update, :debug_mode, :verbose].each do |method|
36
36
  describe method.to_s do
37
37
  it "#{method.to_s} with nil passes along true to bot object" do
38
- @bot.should_receive("#{method.to_s}=").with(true)
38
+ expect(@bot).to receive("#{method.to_s}=").with(true)
39
39
  send method
40
40
  end
41
41
 
42
42
  it "#debug_mode with false is passed" do
43
- @bot.should_receive("#{method.to_s}=").with(false)
43
+ expect(@bot).to receive("#{method.to_s}=").with(false)
44
44
  send method, false
45
45
  end
46
46
 
47
47
  it "#debug_mode with true is passed" do
48
- @bot.should_receive("#{method.to_s}=").with(true)
48
+ expect(@bot).to receive("#{method.to_s}=").with(true)
49
49
  send method, true
50
50
  end
51
51
  end
52
52
  end
53
53
 
54
+ it "#badwords returns an array" do
55
+ expect(bad_words).to be_a(Array)
56
+ end
57
+
54
58
  describe "exclude" do
55
59
  it "#exclude passes along to bot object" do
56
- @bot.should_receive(:exclude=).with(["foo"])
60
+ expect(@bot).to receive(:exclude=).with(["foo"])
57
61
  exclude ["foo"]
58
62
  end
59
63
 
60
64
  it "#exclude turns single-string arg into an array" do
61
- @bot.should_receive(:exclude=).with(["foo"])
65
+ expect(@bot).to receive(:exclude=).with(["foo"])
62
66
  exclude "foo"
63
67
  end
64
68
 
65
69
  it "#exclude turns comma-delimited string arg into an array" do
66
- @bot.should_receive(:exclude=).with(["foo", "bar"])
70
+ expect(@bot).to receive(:exclude=).with(["foo", "bar"])
67
71
  exclude "foo, bar"
68
72
  end
69
73
  end
70
74
 
71
75
  describe "search" do
72
76
  it "passes along to bot object" do
73
- @bot.should_receive(:search).with("foo", { })
77
+ expect(@bot).to receive(:search).with("foo", { })
74
78
  search("foo")
75
79
  end
76
80
 
77
81
  it "passes multiple queries along to bot object" do
78
- @bot.should_receive(:search).with(["foo","bar"], { })
82
+ expect(@bot).to receive(:search).with(["foo","bar"], { })
79
83
  search(["foo","bar"])
80
84
  end
81
85
  end
82
86
 
87
+ describe "streaming" do
88
+ it "passes along to bot object" do
89
+ expect(@bot).to receive(:do_streaming)
90
+ streaming {}
91
+ end
92
+ end
93
+
94
+
83
95
  it "#retweet passes along to bot object" do
84
- @bot.should_receive(:retweet).with(1234)
96
+ expect(@bot).to receive(:retweet).with(1234)
85
97
  retweet(1234)
86
98
  end
87
99
 
100
+ it "#favorite passes along to bot object" do
101
+ expect(@bot).to receive(:favorite).with(1234)
102
+ favorite(1234)
103
+ end
104
+
88
105
  it "#replies passes along to bot object" do
89
- @bot.should_receive(:replies)
106
+ expect(@bot).to receive(:replies)
90
107
  replies
91
108
  end
92
109
 
110
+ it "#streaming_tweets passes along to bot object" do
111
+ expect(@bot).to receive(:streaming_tweets)
112
+ streaming_tweets
113
+ end
114
+
93
115
  it "#followers passes along to bot object" do
94
- @bot.should_receive(:followers)
116
+ expect(@bot).to receive(:followers)
95
117
  followers
96
118
  end
97
119
 
120
+ it "#follow passes along to bot object" do
121
+ expect(@bot).to receive(:follow).with(1234)
122
+ follow(1234)
123
+ end
124
+
98
125
  it "#tweet passes along to bot object" do
99
- @bot.should_receive(:tweet).with("hello sailor!", {:foo => "bar" }, nil)
126
+ expect(@bot).to receive(:tweet).with("hello sailor!", {:foo => "bar" }, nil)
100
127
  tweet "hello sailor!", {:foo => "bar"}
101
128
  end
102
129
 
103
130
  it "#reply passes along to bot object" do
104
- @bot.should_receive(:reply).with("hello sailor!", { :source => "source "})
131
+ expect(@bot).to receive(:reply).with("hello sailor!", { :source => "source "})
105
132
  reply "hello sailor!", { :source => "source "}
106
133
  end
107
134
 
135
+ it "#profile_text setter passes along to bot object" do
136
+ expect(@bot).to receive(:profile_text).with("hello sailor!")
137
+ profile_text "hello sailor!"
138
+ end
139
+
140
+ it "#profile_text getter passes along to bot object" do
141
+ expect(@bot).to receive(:profile_text)
142
+ profile_text
143
+ end
144
+
145
+ it "#profile_website passes along to bot object" do
146
+ expect(@bot).to receive(:profile_website).with("http://hellosailor.com")
147
+ profile_website "http://hellosailor.com"
148
+ end
149
+
150
+ it "#profile_website getter passes along to bot object" do
151
+ expect(@bot).to receive(:profile_website)
152
+ profile_website
153
+ end
154
+
108
155
  context "setters" do
109
156
  [:consumer_secret, :consumer_key, :token, :secret].each do |k|
110
157
  it "should be able to set #{k}" do
111
158
  send(k, "foo")
112
- @bot.config[k].should == "foo"
159
+ expect(@bot.config[k]).to eq("foo")
113
160
  end
114
161
  end
115
162
  end
116
163
 
117
164
  describe "update_config" do
118
165
  it "should pass to bot object" do
119
- @bot.should_receive(:update_config)
166
+ expect(@bot).to receive(:update_config)
120
167
  update_config
121
168
  end
122
169
  end
123
170
 
124
171
  describe "since_id" do
125
172
  it "should pass to bot object" do
126
- @bot.should_receive(:config).and_return({:since_id => 1234})
127
- since_id.should == 1234
173
+ expect(@bot).to receive(:config).and_return({:since_id => 1234})
174
+ expect(since_id).to eq(1234)
128
175
  end
129
176
 
130
177
  it "can be set" do
131
178
  since_id(1234)
132
- @bot.config[:since_id].should == 1234
179
+ expect(@bot.config[:since_id]).to eq(1234)
133
180
  end
134
181
  end
135
182
 
136
183
  describe "since_id_reply" do
137
184
  it "should pass to bot object" do
138
- @bot.should_receive(:config).and_return({:since_id_reply => 1234})
139
- since_id_reply.should == 1234
185
+ expect(@bot).to receive(:config).and_return({:since_id_reply => 1234})
186
+ expect(since_id_reply).to eq(1234)
140
187
  end
141
188
  end
142
189
 
143
190
  describe "db" do
144
191
  it "should pass to bot object" do
145
192
  bot_db = double(Object)
146
- @bot.should_receive(:db).and_return(bot_db)
193
+ expect(@bot).to receive(:db).and_return(bot_db)
147
194
 
148
- db.should eql(bot_db)
195
+ expect(db).to eql(bot_db)
149
196
  end
150
197
  end
151
198
 
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Chatterbot::Favorite" do
4
+ describe "#favorite" do
5
+ before(:each) do
6
+ @bot = test_bot
7
+ @t = Twitter::Tweet.new(:id => 7890, :text => "something awesome that you should fave")
8
+ end
9
+
10
+ it "calls require_login" do
11
+ expect(@bot).to receive(:require_login).and_return(false)
12
+ @bot.favorite @t
13
+ end
14
+
15
+ context "data" do
16
+ before(:each) do
17
+ expect(@bot).to receive(:require_login).and_return(true)
18
+ allow(@bot).to receive(:client).and_return(double(Twitter::Client))
19
+
20
+ allow(@bot).to receive(:debug_mode?).and_return(false)
21
+ end
22
+
23
+ it "calls client.favorite with an id" do
24
+ expect(@bot.client).to receive(:favorite).with(7890)
25
+ @bot.favorite(@t.id)
26
+ end
27
+
28
+ it "calls client.retweet with a tweet" do
29
+ expect(@bot.client).to receive(:favorite).with(7890)
30
+ @bot.favorite(@t)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,19 +1,40 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Chatterbot::Followers" do
4
- it "calls require_login" do
5
- bot = test_bot
6
- bot.should_receive(:require_login).and_return(false)
7
- bot.followers
4
+ before(:each) do
5
+ @bot = test_bot
8
6
  end
9
7
 
10
- it "returns followers" do
11
- bot = test_bot
12
- bot.should_receive(:require_login).and_return(true)
13
- bot.stub(:client).and_return(fake_followers(3))
8
+ describe "followers" do
9
+ it "calls require_login" do
10
+ expect(@bot).to receive(:require_login).and_return(false)
11
+ @bot.followers
12
+ end
14
13
 
15
- result = bot.followers
16
- result.size.should == 3
17
- result[0].name.should == "Follower 1"
14
+ it "returns followers" do
15
+ expect(@bot).to receive(:require_login).and_return(true)
16
+ allow(@bot).to receive(:client).and_return(fake_followers(3))
17
+
18
+ result = @bot.followers
19
+ expect(result.size).to eq(3)
20
+ expect(result[0].name).to eq("Follower 1")
21
+ end
18
22
  end
23
+
24
+ describe "follow" do
25
+ it "calls require_login" do
26
+ expect(@bot).to receive(:require_login).and_return(false)
27
+ @bot.follow(1234)
28
+ end
29
+
30
+ it "works" do
31
+ expect(@bot).to receive(:require_login).and_return(true)
32
+ allow(@bot).to receive(:client).and_return(double(Twitter::Client))
33
+ expect(@bot.client).to receive(:follow).with(1234)
34
+
35
+ @bot.follow(1234)
36
+ end
37
+ end
38
+
39
+
19
40
  end
@@ -3,20 +3,20 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe "Chatterbot::Helpers" do
4
4
  it "#tweet_user works with [:from_user]" do
5
5
  bot = Chatterbot::Bot.new
6
- bot.tweet_user({:from_user => "foo"}).should == "@foo"
7
- bot.tweet_user({:from_user => "@foo"}).should == "@foo"
6
+ expect(bot.tweet_user({:from_user => "foo"})).to eq("@foo")
7
+ expect(bot.tweet_user({:from_user => "@foo"})).to eq("@foo")
8
8
  end
9
9
 
10
10
  it "#tweet_user works with [:user][:screen_name]" do
11
11
  bot = Chatterbot::Bot.new
12
- bot.tweet_user({:user => {:screen_name => "foo"}}).should == "@foo"
13
- bot.tweet_user({:user => {:screen_name => "@foo"}}).should == "@foo"
12
+ expect(bot.tweet_user({:user => {:screen_name => "foo"}})).to eq("@foo")
13
+ expect(bot.tweet_user({:user => {:screen_name => "@foo"}})).to eq("@foo")
14
14
  end
15
15
 
16
16
  it "#tweet_user works with string" do
17
17
  bot = Chatterbot::Bot.new
18
- bot.tweet_user("foo").should == "@foo"
19
- bot.tweet_user("@foo").should == "@foo"
18
+ expect(bot.tweet_user("foo")).to eq("@foo")
19
+ expect(bot.tweet_user("@foo")).to eq("@foo")
20
20
  end
21
21
 
22
22
  describe "#from_user" do
@@ -25,15 +25,21 @@ describe "Chatterbot::Helpers" do
25
25
  end
26
26
 
27
27
  it "should accept strings" do
28
- @bot.from_user("x").should == "x"
28
+ expect(@bot.from_user("x")).to eq("x")
29
29
  end
30
30
 
31
- it "should accept :from_user tweet" do
32
- @bot.from_user(Twitter::Tweet.new(:id => 123, :from_user => "x")).should == "x"
31
+ it "should accept tweet" do
32
+ t = Twitter::Tweet.new(:id => 123, :text => 'Tweet text.', :user => {:id => 123, :screen_name => "x"})
33
+ expect(@bot.from_user(t)).to eq("x")
33
34
  end
34
35
 
36
+ it "should accept user" do
37
+ u = Twitter::User.new(:id => 123, :name => "x")
38
+ expect(@bot.from_user(fake_user("x"))).to eq("x")
39
+ end
40
+
35
41
  it "should accept :screen_name" do
36
- @bot.from_user(:user => {:screen_name => "x"}).should == "x"
42
+ expect(@bot.from_user(:user => {:screen_name => "x"})).to eq("x")
37
43
  end
38
44
  end
39
45
 
@@ -41,28 +47,27 @@ describe "Chatterbot::Helpers" do
41
47
  before(:each) do
42
48
  class TestBot < Chatterbot::Bot; end
43
49
  @bot = Chatterbot::Bot.new
44
- #@bot.client = mock(Object)
45
50
  end
46
51
 
47
52
  it "can set botname" do
48
53
  @bot = TestBot.new
49
54
  @bot.botname = "foo"
50
- @bot.botname.should == "foo"
55
+ expect(@bot.botname).to eq("foo")
51
56
  end
52
57
 
53
58
  it "calls botname smartly for inherited classes" do
54
59
  @bot2 = TestBot.new
55
- @bot2.botname.should == "testbot"
60
+ expect(@bot2.botname).to eq("testbot")
56
61
  end
57
62
 
58
63
  it "calls botname for non-inherited bots" do
59
- File.should_receive(:basename).and_return("bot")
60
- @bot.botname.should == "bot"
64
+ expect(File).to receive(:basename).and_return("bot")
65
+ expect(@bot.botname).to eq("bot")
61
66
  end
62
67
 
63
68
  it "uses specified name" do
64
69
  @bot = Chatterbot::Bot.new(:name => 'xyzzy')
65
- @bot.botname.should == "xyzzy"
70
+ expect(@bot.botname).to eq("xyzzy")
66
71
  end
67
72
  end
68
73
 
@@ -73,16 +78,16 @@ describe "Chatterbot::Helpers" do
73
78
  end
74
79
 
75
80
  it "should replace username by calling tweet_user" do
76
- @bot.should_receive(:tweet_user).and_return("@foobar")
77
- @bot.replace_variables("#USER#", @tweet).should == "@foobar"
81
+ expect(@bot).to receive(:tweet_user).and_return("@foobar")
82
+ expect(@bot.replace_variables("#USER#", @tweet)).to eq("@foobar")
78
83
  end
79
84
 
80
85
  it "should be fine with not replacing anything" do
81
- @bot.replace_variables("foobar", @tweet).should == "foobar"
86
+ expect(@bot.replace_variables("foobar", @tweet)).to eq("foobar")
82
87
  end
83
88
 
84
89
  it "should be fine without a tweet" do
85
- @bot.replace_variables("foobar").should == "foobar"
90
+ expect(@bot.replace_variables("foobar")).to eq("foobar")
86
91
  end
87
92
  end
88
93
  end