bjeanes-twibot 0.1.6 → 0.1.7

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/History.txt CHANGED
@@ -1,3 +1,20 @@
1
+ == 0.1.7 / 2009-04-18
2
+
3
+ * Added :process option that configures how the bot will process incoming tweets and messages
4
+
5
+ == 0.1.6 / 2009-04-13
6
+
7
+ * Fixed configure block not actually working for username and password (Bodaniel Jeanes)
8
+ * Minor updates in tests
9
+
10
+ == 0.1.5 / 2009-04-12
11
+
12
+ * Added support for regular expression routes
13
+ * Make timeline_for option configurable, ie in config: timeline_for: :public
14
+ * Fixed bug: Users where unlawfully rejected when their screen name started with
15
+ a capital letter (Wilco)
16
+ * Fixed bug: Twibot crashed if there were no handlers registered
17
+
1
18
  == 0.1.4 / 2009-03-24
2
19
 
3
20
  * Removed some warnings
data/Readme.rdoc CHANGED
@@ -148,6 +148,7 @@ reaches max_interval, where it will stay until Twibot finds anything.
148
148
  * Ben Vandgrift (Twitter downtime error handling) - http://neovore.com/
149
149
  * Jens Ohlig (warnings)
150
150
  * Wilco van Duinkerken (bug fixes) - http://www.sparkboxx.com/
151
+ * Bodaniel Jeanes (configure block fix) - http://bjeanes.github.com/
151
152
 
152
153
  == License
153
154
 
data/lib/twibot.rb CHANGED
@@ -7,7 +7,7 @@ require File.join(File.dirname(__FILE__), 'hash')
7
7
  module Twibot
8
8
 
9
9
  # :stopdoc:
10
- VERSION = '0.1.6'
10
+ VERSION = '0.1.7'
11
11
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
12
12
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
13
13
  # :startdoc:
data/lib/twibot/bot.rb CHANGED
@@ -24,7 +24,7 @@ module Twibot
24
24
  def prompt?
25
25
  @prompt
26
26
  end
27
-
27
+
28
28
  def processed
29
29
  @processed ||= {
30
30
  :message => nil,
@@ -32,7 +32,7 @@ module Twibot
32
32
  :tweet => nil
33
33
  }
34
34
  end
35
-
35
+
36
36
  def twitter
37
37
  @twitter ||= Twitter::Client.new :login => config[:login], :password => config[:password]
38
38
  end
@@ -47,24 +47,32 @@ module Twibot
47
47
  puts "\nAnd it's a wrap. See ya soon!"
48
48
  exit
49
49
  end
50
+
51
+ case config[:process]
52
+ when :all, nil
53
+ # do nothing so it will fetch ALL
54
+ when :new
55
+ # Make sure we don't process messages and tweets received prior to bot launch
56
+ messages = twitter.messages(:received, { :count => 1 })
57
+ processed[:message] = messages.first.id if messages.length > 0
58
+
59
+ handle_tweets = !handlers.nil? && handlers[:tweet].length + handlers[:reply].length > 0
60
+ tweets = []
50
61
 
51
- # Make sure we don't process messages and tweets received prior to bot launch
52
- messages = twitter.messages(:received, { :count => 1 })
53
- processed[:message] = messages.first.id if messages.length > 0
54
-
55
- handle_tweets = !@handlers.nil? && @handlers[:tweet].length + @handlers[:reply].length > 0
56
- tweets = []
62
+ begin
63
+ tweets = handle_tweets ? twitter.timeline_for(config[:timeline_for], { :count => 1 }) : []
64
+ rescue Twitter::RESTError => e
65
+ log.error("Failed to connect to Twitter. It's likely down for a bit:")
66
+ log.error(e.to_s)
67
+ end
57
68
 
58
- begin
59
- tweets = handle_tweets ? twitter.timeline_for(config[:timeline_for], { :count => 1 }) : []
60
- rescue Twitter::RESTError => e
61
- log.error("Failed to connect to Twitter. It's likely down for a bit:")
62
- log.error(e.to_s)
69
+ processed[:tweet] = tweets.first.id if tweets.length > 0
70
+ processed[:reply] = tweets.first.id if tweets.length > 0
71
+ when Numeric, /\d+/ # a tweet ID to start from
72
+ processed[:tweet] = processed[:reply] = processed[:message] = config[:process]
73
+ else abort "Unknown process option #{config[:process]}, aborting..."
63
74
  end
64
75
 
65
- processed[:tweet] = tweets.first.id if tweets.length > 0
66
- processed[:reply] = tweets.first.id if tweets.length > 0
67
-
68
76
  poll
69
77
  end
70
78
 
@@ -115,9 +123,7 @@ module Twibot
115
123
  options = {}
116
124
  options[:since_id] = processed[type] if processed[type]
117
125
  begin
118
- dispatch_messages(type,
119
- twitter.timeline_for(config[:include_friends] ? :friends : :me,
120
- options), %w{tweet tweets})
126
+ dispatch_messages(type, twitter.timeline_for(config.to_hash[:timeline_for] || :public, options), %w{tweet tweets})
121
127
  rescue Twitter::RESTError => e
122
128
  log.error("Failed to connect to Twitter. It's likely down for a bit:")
123
129
  log.error(e.to_s)
data/lib/twibot/config.rb CHANGED
@@ -27,6 +27,7 @@ module Twibot
27
27
  :log_file => nil,
28
28
  :login => nil,
29
29
  :password => nil,
30
+ :process => :new,
30
31
  :prompt => false,
31
32
  :daemonize => false,
32
33
  :include_friends => false,
data/test/test_bot.rb CHANGED
@@ -55,10 +55,68 @@ class TestBot < Test::Unit::TestCase
55
55
  assert !bot.receive_tweets
56
56
  end
57
57
 
58
+ context "with the process option specified" do
59
+ setup do
60
+ @bot = Twibot::Bot.new(@config = Twibot::Config.default)
61
+ @bot.stubs(:prompt?).returns(false)
62
+ @bot.stubs(:twitter).returns(stub)
63
+ @bot.stubs(:processed).returns(stub)
64
+
65
+ # stop Bot actually starting during tests
66
+ @bot.stubs(:poll)
67
+ end
68
+
69
+ should "not process tweets prior to bot launch if :process option is set to :new" do
70
+ @bot.stubs(:handlers).returns({:tweet => [stub], :reply => []})
71
+
72
+ # Should fetch the latest ID for both messages and tweets
73
+ @bot.twitter.expects(:messages).with(:received, { :count => 1 }).
74
+ returns([stub(:id => (message_id = stub))]).once
75
+ @bot.twitter.expects(:timeline_for).with(:public, { :count => 1 }).
76
+ returns([stub(:id => (tweet_id = stub))]).once
77
+
78
+ # And set them to the since_id value to be used for future polling
79
+ @bot.processed.expects(:[]=).with(:message, message_id)
80
+ @bot.processed.expects(:[]=).with(:tweet, tweet_id)
81
+ @bot.processed.expects(:[]=).with(:reply, tweet_id)
82
+
83
+ @bot.configure { |c| c.process = :new }
84
+ @bot.run!
85
+ end
86
+
87
+ [:all, nil].each do |value|
88
+ should "process all tweets if :process option is set to #{value.inspect}" do
89
+ @bot.twitter.expects(:messages).never
90
+ @bot.twitter.expects(:timeline_for).never
91
+
92
+ # Shout not set the any value for the since_id tweets
93
+ @bot.processed.expects(:[]=).never
94
+
95
+ @bot.configure { |c| c.process = value }
96
+ @bot.run!
97
+ end
98
+ end
99
+
100
+ should "process all tweets after the ID specified in the :process option" do
101
+ tweet_id = 12345
102
+
103
+ @bot.processed.expects(:[]=).with(anything, 12345).times(3)
104
+
105
+ @bot.configure { |c| c.process = tweet_id }
106
+ @bot.run!
107
+ end
108
+
109
+ should "raise exit when the :process option is not recognized" do
110
+ @bot.configure { |c| c.process = "something random" }
111
+ assert_raise(SystemExit) { @bot.run! }
112
+ end
113
+
114
+ end
115
+
58
116
  should "receive message" do
59
117
  bot = Twibot::Bot.new(Twibot::Config.new(:log_level => "error"))
60
118
  bot.add_handler(:message, Twibot::Handler.new)
61
- Twitter::Client.any_instance.expects(:messages).with(:received, {}).returns([twitter_message("cjno", "Hei der!")])
119
+ bot.twitter.expects(:messages).with(:received, {}).returns([twitter_message("cjno", "Hei der!")])
62
120
 
63
121
  assert bot.receive_messages
64
122
  end
@@ -66,25 +124,25 @@ class TestBot < Test::Unit::TestCase
66
124
  should "remember last received message" do
67
125
  bot = Twibot::Bot.new(Twibot::Config.new(:log_level => "error"))
68
126
  bot.add_handler(:message, Twibot::Handler.new)
69
- Twitter::Client.any_instance.expects(:messages).with(:received, {}).returns([twitter_message("cjno", "Hei der!")])
127
+ bot.twitter.expects(:messages).with(:received, {}).returns([twitter_message("cjno", "Hei der!")])
70
128
  assert_equal 1, bot.receive_messages
71
129
 
72
- Twitter::Client.any_instance.expects(:messages).with(:received, { :since_id => 1 }).returns([])
130
+ bot.twitter.expects(:messages).with(:received, { :since_id => 1 }).returns([])
73
131
  assert_equal 0, bot.receive_messages
74
132
  end
75
133
 
76
134
  should "receive tweet" do
77
135
  bot = Twibot::Bot.new(Twibot::Config.new(:log_level => "error"))
78
136
  bot.add_handler(:tweet, Twibot::Handler.new)
79
- Twitter::Client.any_instance.expects(:timeline_for).with(:me, {}).returns([tweet("cjno", "Hei der!")])
137
+ bot.twitter.expects(:timeline_for).with(:public, {}).returns([tweet("cjno", "Hei der!")])
80
138
 
81
139
  assert_equal 1, bot.receive_tweets
82
140
  end
83
141
 
84
142
  should "receive friend tweets if configured" do
85
- bot = Twibot::Bot.new(Twibot::Config.new({:log_level => "error", :include_friends => true}))
143
+ bot = Twibot::Bot.new(Twibot::Config.new({:log_level => "error", :timeline_for => :friends}))
86
144
  bot.add_handler(:tweet, Twibot::Handler.new)
87
- Twitter::Client.any_instance.expects(:timeline_for).with(:friends, {}).returns([tweet("cjno", "Hei der!")])
145
+ bot.twitter.expects(:timeline_for).with(:friends, {}).returns([tweet("cjno", "Hei der!")])
88
146
 
89
147
  assert_equal 1, bot.receive_tweets
90
148
  end
@@ -92,17 +150,17 @@ class TestBot < Test::Unit::TestCase
92
150
  should "remember received tweets" do
93
151
  bot = Twibot::Bot.new(Twibot::Config.new(:log_level => "error"))
94
152
  bot.add_handler(:tweet, Twibot::Handler.new)
95
- Twitter::Client.any_instance.expects(:timeline_for).with(:me, {}).returns([tweet("cjno", "Hei der!")])
153
+ bot.twitter.expects(:timeline_for).with(:public, {}).returns([tweet("cjno", "Hei der!")])
96
154
  assert_equal 1, bot.receive_tweets
97
155
 
98
- Twitter::Client.any_instance.expects(:timeline_for).with(:me, { :since_id => 1 }).returns([])
156
+ bot.twitter.expects(:timeline_for).with(:public, { :since_id => 1 }).returns([])
99
157
  assert_equal 0, bot.receive_tweets
100
158
  end
101
159
 
102
160
  should "receive reply when tweet starts with login" do
103
161
  bot = Twibot::Bot.new(Twibot::Config.new(:log_level => "error", :login => "irbno"))
104
162
  bot.add_handler(:reply, Twibot::Handler.new)
105
- Twitter::Client.any_instance.expects(:status).with(:replies, {}).returns([tweet("cjno", "@irbno Hei der!")])
163
+ bot.twitter.expects(:status).with(:replies, {}).returns([tweet("cjno", "@irbno Hei der!")])
106
164
 
107
165
  assert_equal 1, bot.receive_replies
108
166
  end
@@ -110,10 +168,10 @@ class TestBot < Test::Unit::TestCase
110
168
  should "remember received replies" do
111
169
  bot = Twibot::Bot.new(Twibot::Config.new(:log_level => "error", :login => "irbno"))
112
170
  bot.add_handler(:reply, Twibot::Handler.new)
113
- Twitter::Client.any_instance.expects(:status).with(:replies, {}).returns([tweet("cjno", "@irbno Hei der!")])
171
+ bot.twitter.expects(:status).with(:replies, {}).returns([tweet("cjno", "@irbno Hei der!")])
114
172
  assert_equal 1, bot.receive_replies
115
173
 
116
- Twitter::Client.any_instance.expects(:status).with(:replies, { :since_id => 1 }).returns([])
174
+ bot.twitter.expects(:status).with(:replies, { :since_id => 1 }).returns([])
117
175
  assert_equal 0, bot.receive_replies
118
176
  end
119
177
 
data/twibot.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{twibot}
5
- s.version = "0.1.6"
5
+ s.version = "0.1.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Christian Johansen"]
9
- s.date = %q{2009-04-13}
9
+ s.date = %q{2009-04-18}
10
10
  s.description = %q{Twibot (pronounced like "Abbot"), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra.}
11
11
  s.email = %q{christian@cjohansen.no}
12
12
  s.extra_rdoc_files = ["History.txt", "Readme.rdoc"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bjeanes-twibot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Johansen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-13 00:00:00 -07:00
12
+ date: 2009-04-18 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency