bjeanes-twibot 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +24 -0
- data/Rakefile +30 -0
- data/Readme.rdoc +175 -0
- data/lib/hash.rb +8 -0
- data/lib/twibot/bot.rb +231 -0
- data/lib/twibot/config.rb +138 -0
- data/lib/twibot/handlers.rb +111 -0
- data/lib/twibot/macros.rb +66 -0
- data/lib/twibot/tweets.rb +4 -0
- data/lib/twibot.rb +87 -0
- data/test/test_bot.rb +185 -0
- data/test/test_config.rb +89 -0
- data/test/test_handler.rb +191 -0
- data/test/test_hash.rb +34 -0
- data/test/test_helper.rb +44 -0
- data/test/test_twibot.rb +1 -0
- data/twibot.gemspec +38 -0
- metadata +95 -0
@@ -0,0 +1,191 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Twibot)
|
2
|
+
|
3
|
+
class TestHandler < Test::Unit::TestCase
|
4
|
+
context "pattern writer" do
|
5
|
+
should "abort on empty values" do
|
6
|
+
handler = Twibot::Handler.new
|
7
|
+
|
8
|
+
handler.pattern = nil
|
9
|
+
assert_nil handler.instance_eval { @options[:pattern] }
|
10
|
+
assert_nil handler.instance_eval { @options[:tokens] }
|
11
|
+
|
12
|
+
handler.pattern = ""
|
13
|
+
assert_nil handler.instance_eval { @options[:pattern] }
|
14
|
+
assert_nil handler.instance_eval { @options[:tokens] }
|
15
|
+
end
|
16
|
+
|
17
|
+
should "turn regular pattern into regex" do
|
18
|
+
handler = Twibot::Handler.new
|
19
|
+
handler.pattern = "command"
|
20
|
+
|
21
|
+
assert_equal(/command(\s.+)?/, handler.instance_eval { @options[:pattern] })
|
22
|
+
assert_equal 0, handler.instance_eval { @options[:tokens] }.length
|
23
|
+
end
|
24
|
+
|
25
|
+
should "convert single named switch to regex" do
|
26
|
+
handler = Twibot::Handler.new
|
27
|
+
handler.pattern = ":command"
|
28
|
+
|
29
|
+
assert_equal(/([^\s]+)(\s.+)?/, handler.instance_eval { @options[:pattern] })
|
30
|
+
assert_equal 1, handler.instance_eval { @options[:tokens] }.length
|
31
|
+
assert_equal :command, handler.instance_eval { @options[:tokens].first }
|
32
|
+
end
|
33
|
+
|
34
|
+
should "convert several named switches to regexen" do
|
35
|
+
handler = Twibot::Handler.new
|
36
|
+
handler.pattern = ":command fixed_word :subcommand"
|
37
|
+
|
38
|
+
assert_equal(/([^\s]+) fixed_word ([^\s]+)(\s.+)?/, handler.instance_eval { @options[:pattern] })
|
39
|
+
assert_equal 2, handler.instance_eval { @options[:tokens] }.length
|
40
|
+
assert_equal :command, handler.instance_eval { @options[:tokens].first }
|
41
|
+
assert_equal :subcommand, handler.instance_eval { @options[:tokens][1] }
|
42
|
+
end
|
43
|
+
|
44
|
+
should "convert several named switches to regexen specified by options" do
|
45
|
+
handler = Twibot::Handler.new(":time :hour", :hour => /\d\d/)
|
46
|
+
|
47
|
+
assert_equal(/([^\s]+) ((?-mix:\d\d))(\s.+)?/, handler.instance_eval { @options[:pattern] })
|
48
|
+
assert_equal 2, handler.instance_eval { @options[:tokens] }.length
|
49
|
+
assert_equal :time, handler.instance_eval { @options[:tokens].first }
|
50
|
+
assert_equal :hour, handler.instance_eval { @options[:tokens][1] }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
should "recognize empty pattern" do
|
55
|
+
handler = Twibot::Handler.new
|
56
|
+
message = twitter_message "cjno", "A twitter direct message"
|
57
|
+
|
58
|
+
assert handler.recognize?(message)
|
59
|
+
end
|
60
|
+
|
61
|
+
should "recognize empty pattern and allowed user" do
|
62
|
+
handler = Twibot::Handler.new "", :from => "cjno"
|
63
|
+
message = twitter_message "cjno", "A twitter direct message"
|
64
|
+
assert handler.recognize?(message)
|
65
|
+
|
66
|
+
handler = Twibot::Handler.new "", :from => ["cjno", "irbno"]
|
67
|
+
assert handler.recognize?(message)
|
68
|
+
end
|
69
|
+
|
70
|
+
should "not recognize empty pattern and disallowed user" do
|
71
|
+
handler = Twibot::Handler.new "", :from => "irbno"
|
72
|
+
message = twitter_message "cjno", "A twitter direct message"
|
73
|
+
assert !handler.recognize?(message)
|
74
|
+
|
75
|
+
handler = Twibot::Handler.new "", :from => ["irbno", "satan"]
|
76
|
+
assert !handler.recognize?(message)
|
77
|
+
end
|
78
|
+
|
79
|
+
should "recognize fixed pattern and no user" do
|
80
|
+
handler = Twibot::Handler.new "time"
|
81
|
+
message = twitter_message "cjno", "time oslo norway"
|
82
|
+
assert handler.recognize?(message)
|
83
|
+
end
|
84
|
+
|
85
|
+
should "recognize dynamic pattern and no user" do
|
86
|
+
handler = Twibot::Handler.new "time :city :country"
|
87
|
+
message = twitter_message "cjno", "time oslo norway"
|
88
|
+
assert handler.recognize?(message)
|
89
|
+
end
|
90
|
+
|
91
|
+
should "not recognize dynamic pattern and no user" do
|
92
|
+
handler = Twibot::Handler.new "time :city :country"
|
93
|
+
message = twitter_message "cjno", "oslo norway what is the time?"
|
94
|
+
assert !handler.recognize?(message)
|
95
|
+
end
|
96
|
+
|
97
|
+
should "recognize fixed pattern and user" do
|
98
|
+
handler = Twibot::Handler.new "time", :from => ["cjno", "irbno"]
|
99
|
+
message = twitter_message "cjno", "time oslo norway"
|
100
|
+
assert handler.recognize?(message)
|
101
|
+
end
|
102
|
+
|
103
|
+
should "recognize dynamic pattern and user" do
|
104
|
+
handler = Twibot::Handler.new "time :city :country", :from => ["cjno", "irbno"]
|
105
|
+
message = twitter_message "cjno", "time oslo norway"
|
106
|
+
assert handler.recognize?(message)
|
107
|
+
end
|
108
|
+
|
109
|
+
should "not recognize dynamic pattern and user" do
|
110
|
+
handler = Twibot::Handler.new "time :city :country", :from => ["cjno", "irbno"]
|
111
|
+
message = twitter_message "dude", "time oslo norway"
|
112
|
+
assert !handler.recognize?(message)
|
113
|
+
end
|
114
|
+
|
115
|
+
should "recognize symbol users" do
|
116
|
+
handler = Twibot::Handler.new "time :city :country", :from => [:cjno, :irbno]
|
117
|
+
message = twitter_message "dude", "time oslo norway"
|
118
|
+
assert !handler.recognize?(message)
|
119
|
+
|
120
|
+
message = twitter_message("cjno", "time oslo norway")
|
121
|
+
assert handler.recognize?(message)
|
122
|
+
end
|
123
|
+
|
124
|
+
should "recognize tweets from allowed users" do
|
125
|
+
handler = Twibot::Handler.new :from => [:cjno, :irbno]
|
126
|
+
message = tweet "cjno", "time oslo norway"
|
127
|
+
assert handler.recognize?(message)
|
128
|
+
end
|
129
|
+
|
130
|
+
should "recognize tweets from allowed users with capital screen names" do
|
131
|
+
handler = Twibot::Handler.new :from => [:cjno, :irbno]
|
132
|
+
message = tweet "Cjno", "time oslo norway"
|
133
|
+
assert handler.recognize?(message)
|
134
|
+
end
|
135
|
+
|
136
|
+
should "accept options as only argument" do
|
137
|
+
handler = Twibot::Handler.new :from => :cjno
|
138
|
+
assert_equal(:cjno, handler.instance_eval { @options[:from] })
|
139
|
+
assert_nil handler.instance_eval { @options[:pattern] }
|
140
|
+
end
|
141
|
+
|
142
|
+
should "provide parameters in params hash" do
|
143
|
+
handler = Twibot::Handler.new("time :city :country", :from => ["cjno", "irbno"]) do |message, params|
|
144
|
+
assert_equal "oslo", params[:city]
|
145
|
+
assert_equal "norway", params[:country]
|
146
|
+
end
|
147
|
+
|
148
|
+
message = twitter_message "cjno", "time oslo norway"
|
149
|
+
assert handler.recognize?(message)
|
150
|
+
handler.dispatch(message)
|
151
|
+
end
|
152
|
+
|
153
|
+
should "call constructor block from handle" do
|
154
|
+
handler = Twibot::Handler.new("time :city :country", :from => ["cjno", "irbno"]) do |message, params|
|
155
|
+
raise "Boom!"
|
156
|
+
end
|
157
|
+
|
158
|
+
assert_raise(RuntimeError) do
|
159
|
+
handler.handle(nil, nil)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
should "recognize regular expressions" do
|
164
|
+
handler = Twibot::Handler.new /(?:what|where) is (.*)/i
|
165
|
+
message = twitter_message "dude", "Where is this shit?"
|
166
|
+
assert handler.recognize?(message)
|
167
|
+
|
168
|
+
message = twitter_message "dude", "How is this shit?"
|
169
|
+
assert !handler.recognize?(message)
|
170
|
+
end
|
171
|
+
|
172
|
+
should "recognize regular expressions from specific users" do
|
173
|
+
handler = Twibot::Handler.new /(?:what|where) is (.*)/i, :from => "cjno"
|
174
|
+
message = twitter_message "dude", "Where is this shit?"
|
175
|
+
assert !handler.recognize?(message)
|
176
|
+
|
177
|
+
message = twitter_message "cjno", "Where is this shit?"
|
178
|
+
assert handler.recognize?(message)
|
179
|
+
end
|
180
|
+
|
181
|
+
should "provide parameters as arrays when matching regular expressions" do
|
182
|
+
handler = Twibot::Handler.new(/time ([^\s]*) ([^\s]*)/) do |message, params|
|
183
|
+
assert_equal "oslo", params[0]
|
184
|
+
assert_equal "norway", params[1]
|
185
|
+
end
|
186
|
+
|
187
|
+
message = twitter_message "cjno", "time oslo norway"
|
188
|
+
assert handler.recognize?(message)
|
189
|
+
handler.dispatch(message)
|
190
|
+
end
|
191
|
+
end
|
data/test/test_hash.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Twibot)
|
2
|
+
|
3
|
+
class TestHash < Test::Unit::TestCase
|
4
|
+
should "convert string keys to symbols" do
|
5
|
+
hash = { "one" => 1, "two" => 2 }
|
6
|
+
hash.symbolize_keys!
|
7
|
+
|
8
|
+
assert_equal 1, hash[:one]
|
9
|
+
assert_equal 2, hash[:two]
|
10
|
+
assert_nil hash["one"]
|
11
|
+
assert_nil hash["two"]
|
12
|
+
end
|
13
|
+
|
14
|
+
should "convert string keys and preserve symbol keys" do
|
15
|
+
hash = { "one" => 1, :two => 2 }
|
16
|
+
hash.symbolize_keys!
|
17
|
+
|
18
|
+
assert_equal 1, hash[:one]
|
19
|
+
assert_equal 2, hash[:two]
|
20
|
+
assert_nil hash["one"]
|
21
|
+
assert_nil hash["two"]
|
22
|
+
end
|
23
|
+
|
24
|
+
should "convert hashes recursively" do
|
25
|
+
hash = { "one" => 1, :two => { "three" => 3, "four" => 4 } }
|
26
|
+
hash.symbolize_keys!
|
27
|
+
|
28
|
+
assert_equal 1, hash[:one]
|
29
|
+
assert_equal 3, hash[:two][:three]
|
30
|
+
assert_equal 4, hash[:two][:four]
|
31
|
+
assert_nil hash["one"]
|
32
|
+
assert_nil hash["two"]
|
33
|
+
end
|
34
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'twibot')
|
6
|
+
|
7
|
+
module Test::Unit::Assertions
|
8
|
+
def assert_hashes_equal(expected, actual, message = nil)
|
9
|
+
full_message = build_message(message, <<EOT, expected.inspect, actual.inspect)
|
10
|
+
<?> expected but was
|
11
|
+
<?>.
|
12
|
+
EOT
|
13
|
+
assert_block(full_message) do
|
14
|
+
break false if expected.keys.length != actual.keys.length
|
15
|
+
expected.keys.all? { |k| expected[k] == actual[k] }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def assert_hashes_not_equal(expected, actual, message = nil)
|
20
|
+
full_message = build_message(message, <<EOT, expected.inspect, actual.inspect)
|
21
|
+
<?> expected but was
|
22
|
+
<?>.
|
23
|
+
EOT
|
24
|
+
assert_block(full_message) do
|
25
|
+
break false if expected.keys.length != actual.keys.length
|
26
|
+
expected.keys.any? { |k| expected[k] != actual[k] }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def twitter_message(from, text)
|
32
|
+
Twitter::Message.new(:id => 1,
|
33
|
+
:sender => Twitter::User.new(:screen_name => from),
|
34
|
+
:text => text,
|
35
|
+
:recipient => "twibot",
|
36
|
+
:created_at => Time.now)
|
37
|
+
end
|
38
|
+
|
39
|
+
def tweet(from, text)
|
40
|
+
Twitter::Status.new(:id => 1,
|
41
|
+
:text => text,
|
42
|
+
:user => Twitter::User.new(:screen_name => from),
|
43
|
+
:created_at => Time.now)
|
44
|
+
end
|
data/test/test_twibot.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Twibot)
|
data/twibot.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{twibot}
|
5
|
+
s.version = "0.1.6"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Christian Johansen"]
|
9
|
+
s.date = %q{2009-04-13}
|
10
|
+
s.description = %q{Twibot (pronounced like "Abbot"), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra.}
|
11
|
+
s.email = %q{christian@cjohansen.no}
|
12
|
+
s.extra_rdoc_files = ["History.txt", "Readme.rdoc"]
|
13
|
+
s.files = ["History.txt", "Rakefile", "Readme.rdoc", "lib/hash.rb", "lib/twibot.rb", "lib/twibot/bot.rb", "lib/twibot/config.rb", "lib/twibot/handlers.rb", "lib/twibot/macros.rb", "lib/twibot/tweets.rb", "test/test_bot.rb", "test/test_config.rb", "test/test_handler.rb", "test/test_hash.rb", "test/test_helper.rb", "test/test_twibot.rb", "twibot.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/bjeanes/twibot/}
|
16
|
+
s.rdoc_options = ["--main", "Readme.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{twibot}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Twibot (pronounced like "Abbot"), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra}
|
21
|
+
s.test_files = ["test/test_bot.rb", "test/test_config.rb", "test/test_handler.rb", "test/test_hash.rb", "test/test_helper.rb", "test/test_twibot.rb"]
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 2
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
s.add_runtime_dependency(%q<mbbx6spp-twitter4r>, [">= 0.3.1"])
|
29
|
+
s.add_development_dependency(%q<bones>, [">= 2.5.0"])
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<mbbx6spp-twitter4r>, [">= 0.3.1"])
|
32
|
+
s.add_dependency(%q<bones>, [">= 2.5.0"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<mbbx6spp-twitter4r>, [">= 0.3.1"])
|
36
|
+
s.add_dependency(%q<bones>, [">= 2.5.0"])
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bjeanes-twibot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christian Johansen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-13 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mbbx6spp-twitter4r
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.3.1
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bones
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.5.0
|
34
|
+
version:
|
35
|
+
description: Twibot (pronounced like "Abbot"), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra.
|
36
|
+
email: christian@cjohansen.no
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- History.txt
|
43
|
+
- Readme.rdoc
|
44
|
+
files:
|
45
|
+
- History.txt
|
46
|
+
- Rakefile
|
47
|
+
- Readme.rdoc
|
48
|
+
- lib/hash.rb
|
49
|
+
- lib/twibot.rb
|
50
|
+
- lib/twibot/bot.rb
|
51
|
+
- lib/twibot/config.rb
|
52
|
+
- lib/twibot/handlers.rb
|
53
|
+
- lib/twibot/macros.rb
|
54
|
+
- lib/twibot/tweets.rb
|
55
|
+
- test/test_bot.rb
|
56
|
+
- test/test_config.rb
|
57
|
+
- test/test_handler.rb
|
58
|
+
- test/test_hash.rb
|
59
|
+
- test/test_helper.rb
|
60
|
+
- test/test_twibot.rb
|
61
|
+
- twibot.gemspec
|
62
|
+
has_rdoc: true
|
63
|
+
homepage: http://github.com/bjeanes/twibot/
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --main
|
67
|
+
- Readme.rdoc
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project: twibot
|
85
|
+
rubygems_version: 1.2.0
|
86
|
+
signing_key:
|
87
|
+
specification_version: 2
|
88
|
+
summary: Twibot (pronounced like "Abbot"), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra
|
89
|
+
test_files:
|
90
|
+
- test/test_bot.rb
|
91
|
+
- test/test_config.rb
|
92
|
+
- test/test_handler.rb
|
93
|
+
- test/test_hash.rb
|
94
|
+
- test/test_helper.rb
|
95
|
+
- test/test_twibot.rb
|