mattmueller-twibot 0.1.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Twibot)
2
+ require 'stringio'
3
+
4
+ class TestConfig < Test::Unit::TestCase
5
+ should "default configuration be a hash" do
6
+ assert_not_nil Twibot::Config::DEFAULT
7
+ assert Twibot::Config::DEFAULT.is_a?(Hash)
8
+ end
9
+
10
+ should "initialize with no options" do
11
+ assert_hashes_equal({}, Twibot::Config.new.settings)
12
+ end
13
+
14
+ should "return config from add" do
15
+ config = Twibot::Config.new
16
+ assert_equal config, config.add(Twibot::Config.new)
17
+ end
18
+
19
+ should "alias add to <<" do
20
+ config = Twibot::Config.new
21
+ assert config.respond_to?(:<<)
22
+ assert config << Twibot::Config.new
23
+ end
24
+
25
+ should "mirror method_missing as config getters" do
26
+ config = Twibot::Config.default << Twibot::Config.new
27
+ assert_equal Twibot::Config::DEFAULT[:min_interval], config.min_interval
28
+ assert_equal Twibot::Config::DEFAULT[:login], config.login
29
+ end
30
+
31
+ should "mirror missing methods as config setters" do
32
+ config = Twibot::Config.default << Twibot::Config.new
33
+ assert_equal Twibot::Config::DEFAULT[:min_interval], config.min_interval
34
+
35
+ val = config.min_interval
36
+ config.min_interval = val + 5
37
+ assert_not_equal Twibot::Config::DEFAULT[:min_interval], config.min_interval
38
+ assert_equal val + 5, config.min_interval
39
+ end
40
+
41
+ should "not override default hash" do
42
+ config = Twibot::Config.default
43
+ hash = Twibot::Config::DEFAULT
44
+
45
+ config.min_interval = 0
46
+ config.max_interval = 0
47
+
48
+ assert_hashes_not_equal Twibot::Config::DEFAULT, config.to_hash
49
+ assert_hashes_equal hash, Twibot::Config::DEFAULT
50
+ end
51
+
52
+ should "return merged configuration from to_hash" do
53
+ config = Twibot::Config.new
54
+ config.min_interval = 10
55
+ config.max_interval = 10
56
+
57
+ config2 = Twibot::Config.new({})
58
+ config2.min_interval = 1
59
+ config << config2
60
+ options = config.to_hash
61
+
62
+ assert_equal 10, options[:max_interval]
63
+ assert_equal 1, options[:min_interval]
64
+ end
65
+ end
66
+
67
+ class TestCliConfig < Test::Unit::TestCase
68
+ should "configure from options" do
69
+ config = Twibot::CliConfig.new %w{--min-interval 10 --max-interval 15}
70
+ assert_equal 10, config.min_interval
71
+ assert_equal 15, config.max_interval
72
+ end
73
+ end
74
+
75
+ class TestFileConfig < Test::Unit::TestCase
76
+ should "subclass config for file config" do
77
+ assert Twibot::FileConfig.new(StringIO.new).is_a?(Twibot::Config)
78
+ end
79
+
80
+ should "read settings from stream" do
81
+ config = Twibot::FileConfig.new(StringIO.new <<-YAML)
82
+ min_interval: 10
83
+ max_interval: 20
84
+ YAML
85
+
86
+ assert_equal 10, config.min_interval
87
+ assert_equal 20, config.max_interval
88
+ end
89
+ end
@@ -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
@@ -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
@@ -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
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Twibot)
@@ -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.7.1"
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/mattmueller/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_helper.rb", "test/test_config.rb", "test/test_hash.rb", "test/test_twibot.rb", "test/test_bot.rb", "test/test_handler.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<twitter4r>, [">= 0.3.1"])
29
+ s.add_development_dependency(%q<bones>, [">= 2.4.0"])
30
+ else
31
+ s.add_dependency(%q<twitter4r>, [">= 0.3.1"])
32
+ s.add_dependency(%q<bones>, [">= 2.4.0"])
33
+ end
34
+ else
35
+ s.add_dependency(%q<twitter4r>, [">= 0.3.1"])
36
+ s.add_dependency(%q<bones>, [">= 2.4.0"])
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mattmueller-twibot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7.1
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: 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.4.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/mattmueller/twibot/
64
+ licenses:
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --main
68
+ - Readme.rdoc
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project: twibot
86
+ rubygems_version: 1.3.5
87
+ signing_key:
88
+ specification_version: 2
89
+ summary: Twibot (pronounced like "Abbot"), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra
90
+ test_files:
91
+ - test/test_helper.rb
92
+ - test/test_config.rb
93
+ - test/test_hash.rb
94
+ - test/test_twibot.rb
95
+ - test/test_bot.rb
96
+ - test/test_handler.rb