robut 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +17 -0
  3. data/README.rdoc +124 -0
  4. data/Rakefile +27 -0
  5. data/bin/robut +9 -0
  6. data/examples/Chatfile +22 -0
  7. data/lib/robut.rb +5 -0
  8. data/lib/robut/connection.rb +167 -0
  9. data/lib/robut/plugin.rb +16 -0
  10. data/lib/robut/plugin/base.rb +70 -0
  11. data/lib/robut/plugin/calc.rb +20 -0
  12. data/lib/robut/plugin/echo.rb +14 -0
  13. data/lib/robut/plugin/later.rb +73 -0
  14. data/lib/robut/plugin/lunch.rb +57 -0
  15. data/lib/robut/plugin/meme.rb +30 -0
  16. data/lib/robut/plugin/ping.rb +11 -0
  17. data/lib/robut/plugin/rdio.rb +70 -0
  18. data/lib/robut/plugin/rdio/public/css/rdio.css +141 -0
  19. data/lib/robut/plugin/rdio/public/css/style.css +79 -0
  20. data/lib/robut/plugin/rdio/public/css/style_after.css +42 -0
  21. data/lib/robut/plugin/rdio/public/images/background.png +0 -0
  22. data/lib/robut/plugin/rdio/public/images/no-album.png +0 -0
  23. data/lib/robut/plugin/rdio/public/index.html +42 -0
  24. data/lib/robut/plugin/rdio/public/js/libs/dd_belatedpng.js +13 -0
  25. data/lib/robut/plugin/rdio/public/js/libs/jquery-1.5.1.min.js +16 -0
  26. data/lib/robut/plugin/rdio/public/js/libs/modernizr-1.7.min.js +2 -0
  27. data/lib/robut/plugin/rdio/public/js/rdio.js +129 -0
  28. data/lib/robut/plugin/rdio/public/js/script.js +3 -0
  29. data/lib/robut/plugin/rdio/server.rb +34 -0
  30. data/lib/robut/plugin/say.rb +20 -0
  31. data/lib/robut/plugin/sayings.rb +31 -0
  32. data/lib/robut/plugin/twss.rb +13 -0
  33. data/lib/robut/storage.rb +3 -0
  34. data/lib/robut/storage/base.rb +21 -0
  35. data/lib/robut/storage/hash_store.rb +26 -0
  36. data/lib/robut/storage/yaml_store.rb +51 -0
  37. data/lib/robut/version.rb +4 -0
  38. data/robut.gemspec +23 -0
  39. data/test/mocks/connection_mock.rb +26 -0
  40. data/test/simplecov_helper.rb +2 -0
  41. data/test/test_helper.rb +7 -0
  42. data/test/unit/connection_test.rb +123 -0
  43. data/test/unit/plugin/base_test.rb +16 -0
  44. data/test/unit/plugin/echo_test.rb +26 -0
  45. data/test/unit/plugin/later_test.rb +39 -0
  46. data/test/unit/plugin/lunch_test.rb +35 -0
  47. data/test/unit/plugin/ping_test.rb +21 -0
  48. data/test/unit/plugin/say_test.rb +28 -0
  49. data/test/unit/storage/hash_store_test.rb +15 -0
  50. data/test/unit/storage/yaml_store_test.rb +42 -0
  51. data/test/unit/storage/yaml_test.yml +1 -0
  52. metadata +135 -0
@@ -0,0 +1,4 @@
1
+ module Robut # :nodoc:
2
+ # Robut's version number.
3
+ VERSION = "0.2.0"
4
+ end
data/robut.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "robut/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "robut"
7
+ s.version = Robut::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Justin Weiss"]
10
+ s.email = ["justin@uberweiss.org"]
11
+ s.homepage = "http://rdoc.info/github/justinweiss/robut/master/frames"
12
+ s.summary = %q{A simple plugin-enabled HipChat bot}
13
+ s.description = %q{A simple plugin-enabled HipChat bot}
14
+
15
+ s.rubyforge_project = "robut"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "xmpp4r", "~> 0.5.0"
23
+ end
@@ -0,0 +1,26 @@
1
+ require 'robut/storage/hash_store'
2
+
3
+ class Robut::ConnectionMock < Robut::Connection
4
+
5
+ def initialize(config = nil)
6
+ self.config = config || self.class.config
7
+ self.store = Robut::Storage::HashStore
8
+ end
9
+
10
+ def replies
11
+ @replies ||= []
12
+ end
13
+
14
+ def reply(msg, to = nil)
15
+ replies << msg
16
+ end
17
+
18
+ def handle_message(plugins, time, nick, message)
19
+ messages << [time, nick, message]
20
+ end
21
+
22
+ def messages
23
+ @messages ||= []
24
+ end
25
+
26
+ end
@@ -0,0 +1,2 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
@@ -0,0 +1,7 @@
1
+ require 'robut'
2
+ require 'test/unit'
3
+ require 'mocks/connection_mock'
4
+
5
+ Robut::ConnectionMock.configure do |config|
6
+ config.nick = "Robut t. Robot"
7
+ end
@@ -0,0 +1,123 @@
1
+ require 'test_helper'
2
+
3
+ class SimplePlugin
4
+ attr_accessor :run
5
+
6
+ def initialize(*args)
7
+ super(*args)
8
+ @run = false
9
+ end
10
+
11
+ def handle(*args)
12
+ self.run = true
13
+ end
14
+ end
15
+
16
+ class ReplyToUserPlugin < Robut::Plugin::Base
17
+ def initialize(*args)
18
+ super(*args)
19
+ end
20
+
21
+ def handle(time, nick, message)
22
+ reply("Reply", nick)
23
+ end
24
+ end
25
+
26
+ class ReplyToRoomPlugin < Robut::Plugin::Base
27
+ def initialize(*args)
28
+ super(*args)
29
+ end
30
+
31
+ def handle(time, nick, message)
32
+ reply("Reply", :room)
33
+ end
34
+ end
35
+
36
+ class ReplyMock
37
+ attr_accessor :messages
38
+
39
+ def initialize
40
+ @messages = []
41
+ end
42
+
43
+ def send(message)
44
+ @messages << message
45
+ end
46
+
47
+ def room
48
+ "my_room@test.com"
49
+ end
50
+ end
51
+
52
+ class ConnectionTest < Test::Unit::TestCase
53
+
54
+ def setup
55
+ Robut::Plugin.plugins = [SimplePlugin]
56
+ @connection = Robut::Connection.new({
57
+ :jid => 'abc@def.com',
58
+ :nick => "Test Robut"
59
+ })
60
+ end
61
+
62
+ def test_end_to_end_message
63
+ Robut::Plugin.plugins = [Robut::Plugin::Echo]
64
+ @connection.muc = ReplyMock.new
65
+ @connection.handle_message(plugins(@connection), Time.now, 'Justin', '@test echo Test Message')
66
+ message = @connection.muc.messages.first
67
+ assert_equal(@connection.muc.room, message.to.to_s)
68
+ assert_equal("Test Message", message.body)
69
+ end
70
+
71
+ def test_handle_message_delegates_to_plugin
72
+ plugins = plugins(@connection)
73
+ assert !plugins.first.run, "The plugin was not set up correctly."
74
+ @connection.handle_message(plugins, Time.now, 'Justin', 'Test Message')
75
+ assert plugins.first.run, "The plugin's handle_message method should have been run"
76
+ end
77
+
78
+ def test_handle_message_from_person
79
+ Robut::Plugin.plugins = [Robut::Plugin::Echo]
80
+ sender = Jabber::JID.new('justin@example.com')
81
+ @connection.client = ReplyMock.new
82
+ @connection.handle_message(plugins(@connection, sender), Time.now, 'Justin', '@test echo Test Message')
83
+ message = @connection.client.messages.first
84
+ assert_equal(sender, message.to)
85
+ assert_equal("Test Message", message.body)
86
+ end
87
+
88
+ def test_reply_directly_to_user
89
+ Robut::Plugin.plugins = [ReplyToUserPlugin]
90
+ @connection.client = ReplyMock.new
91
+ justin = Jabber::JID.new('justin@example.com')
92
+ @connection.roster = OpenStruct.new({
93
+ :items => {
94
+ justin => OpenStruct.new({
95
+ :iname => "Justin Weiss"
96
+ })
97
+ }
98
+ })
99
+
100
+ @connection.handle_message(plugins(@connection), Time.now, 'justin WEISS', 'Test Message')
101
+ message = @connection.client.messages.first
102
+ assert_equal(justin, message.to.to_s)
103
+ assert_equal(:chat, message.type)
104
+ assert_equal("Reply", message.body)
105
+ end
106
+
107
+ def test_reply_directly_to_room
108
+ Robut::Plugin.plugins = [ReplyToRoomPlugin]
109
+ sender = Jabber::JID.new('justin@example.com')
110
+ @connection.muc = ReplyMock.new
111
+ @connection.handle_message(plugins(@connection, sender), Time.now, 'Justin', '@test echo Test Message')
112
+ message = @connection.muc.messages.first
113
+ assert_equal(@connection.muc.room, message.to.to_s)
114
+ assert_equal("Reply", message.body)
115
+ end
116
+
117
+ private
118
+
119
+ def plugins(connection, sender = nil)
120
+ Robut::Plugin.plugins.map { |p| p.new(connection, sender) }
121
+ end
122
+
123
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class Robut::Plugin::BaseTest < Test::Unit::TestCase
4
+
5
+ def test_sent_to_me?
6
+ plugin = Robut::Plugin::Base.new(Robut::ConnectionMock.new)
7
+
8
+ assert plugin.sent_to_me?("@Robut hello there")
9
+ assert !plugin.sent_to_me?("@Robuto hello there")
10
+ assert !plugin.sent_to_me?("@David hello there")
11
+ assert plugin.sent_to_me?("this is a @Robut message")
12
+ assert plugin.sent_to_me?("this is a message to @robut")
13
+ assert !plugin.sent_to_me?("this is a message to@robut")
14
+ end
15
+
16
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+ require 'robut/plugin/echo'
3
+
4
+ class Robut::Plugin::EchoTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @connection = Robut::ConnectionMock.new
8
+ @plugin = Robut::Plugin::Echo.new(@connection)
9
+ end
10
+
11
+ def test_replies_with_this
12
+ @plugin.handle(Time.now, "@john", "@robut echo this")
13
+ assert_equal ["this"], @plugin.connection.replies
14
+ end
15
+
16
+ def test_replies_with_nicks
17
+ @plugin.handle(Time.now, "@john", "@robut echo @justin look over here!")
18
+ assert_equal ["@justin look over here!"], @plugin.connection.replies
19
+ end
20
+
21
+ def test_doesnt_reply_with_empty
22
+ @plugin.handle(Time.now, "@john", "@robut echo")
23
+ assert_equal [], @plugin.connection.replies
24
+ end
25
+
26
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+ require 'robut/plugin/later'
3
+
4
+ class Robut::Plugin::LaterTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @connection = Robut::ConnectionMock.new
8
+ @plugin = Robut::Plugin::Later.new(@connection)
9
+ @plugin.instance_eval do
10
+ def threader; yield; end # no threads
11
+ def sleep(*prms); end # just skip the whole sleeping part
12
+ end
13
+ end
14
+
15
+ def test_replies_with_minutes
16
+ @plugin.handle(Time.now, "@john", "@robut in 0 minutes msg me")
17
+ assert_equal ["Ok, see you in 0 minutes"], @plugin.connection.replies
18
+ message = @plugin.connection.messages.first
19
+ assert message
20
+ assert_equal message[1], "@john"
21
+ assert_equal message[2], "@robut msg me"
22
+ end
23
+
24
+ def test_replies_with_sec
25
+ @plugin.handle(Time.now, "@john", "@robut in 1 sec msg me")
26
+ assert_equal ["Ok, see you in 1 sec"], @plugin.connection.replies
27
+ end
28
+
29
+ def test_replies_with_hr
30
+ @plugin.handle(Time.now, "@john", "@robut in 1 hr msg me")
31
+ assert_equal ["Ok, see you in 1 hr"], @plugin.connection.replies
32
+ end
33
+
34
+ def test_replies_with_hrs
35
+ @plugin.handle(Time.now, "@john", "@robut in 2 hrs msg me")
36
+ assert_equal ["Ok, see you in 2 hrs"], @plugin.connection.replies
37
+ end
38
+
39
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+ require 'robut/plugin/lunch'
3
+
4
+ class Robut::Plugin::LunchTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @connection = Robut::ConnectionMock.new
8
+ @plugin = Robut::Plugin::Lunch.new(@connection)
9
+ @plugin.places = ["Pho"]
10
+ end
11
+
12
+ def test_handle_returns_pho_for_lunch
13
+ @plugin.handle(Time.now, "John", "lunch?")
14
+ assert_equal ["Pho!"], @plugin.connection.replies
15
+ end
16
+
17
+ def test_handle_returns_all_places_for_lunch_places
18
+ @plugin.new_place("Teriyaki")
19
+ @plugin.handle(Time.now, "John", "@robut lunch places")
20
+ assert_equal ["Pho, Teriyaki"], @plugin.connection.replies
21
+ end
22
+
23
+ def test_handle_new_lunch_place
24
+ @plugin.handle(Time.now, "John", "@robut new lunch place Green Leaf")
25
+ assert_equal ["Ok, I'll add \"Green Leaf\" to the the list of lunch places"], @plugin.connection.replies
26
+ assert @plugin.places.include?("Green Leaf")
27
+ end
28
+
29
+ def test_handle_remove_lunch_place
30
+ @plugin.handle(Time.now, "John", "@robut remove lunch place Pho")
31
+ assert_equal ["I removed \"Pho\" from the list of lunch places"], @plugin.connection.replies
32
+ assert @plugin.places.empty?
33
+ end
34
+
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+ require 'robut/plugin/ping'
3
+
4
+ class Robut::Plugin::PingTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @connection = Robut::ConnectionMock.new
8
+ @plugin = Robut::Plugin::Ping.new(@connection)
9
+ end
10
+
11
+ def test_replies_wih_pong
12
+ @plugin.handle(Time.now, "@john", "@robut ping")
13
+ assert_equal ["pong"], @plugin.connection.replies
14
+ end
15
+
16
+ def test_replies_wih_sec
17
+ @plugin.handle(Time.now, "@john", "@robut ping pong")
18
+ assert_equal [], @plugin.connection.replies
19
+ end
20
+
21
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+ require 'robut/plugin/say'
3
+
4
+ class Robut::Plugin::SayTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @connection = Robut::ConnectionMock.new
8
+ @plugin = Robut::Plugin::Say.new(@connection)
9
+ @plugin.instance_eval do
10
+ # Stub out system
11
+ def system(c); system_calls << c; end
12
+ def system_calls; @system_calls ||= []; end;
13
+ end
14
+ end
15
+
16
+ def test_says_stuff
17
+ @plugin.handle(Time.now, "@john", "@robut say stuff")
18
+ assert_equal [], @plugin.connection.replies # shouldn't reply to the chat room
19
+ assert_equal ["say stuff"], @plugin.system_calls
20
+ end
21
+
22
+ def test_doesnt_say_stuff
23
+ @plugin.handle(Time.now, "@john", "@robut ok don't say stuff")
24
+ assert_equal [], @plugin.connection.replies
25
+ assert_equal [], @plugin.system_calls
26
+ end
27
+
28
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+ require 'robut/storage/hash_store'
3
+
4
+ class Robut::Storage::HashStoreTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @store = Robut::Storage::HashStore
8
+ end
9
+
10
+ def test_can_write_and_read
11
+ assert_equal 'in the trunk', (@store['junk'] = 'in the trunk')
12
+ assert_equal 'in the trunk', @store['junk']
13
+ end
14
+
15
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+ require 'robut/storage/yaml_store'
3
+
4
+ class Robut::Storage::YamlStoreTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @store = Robut::Storage::YamlStore
8
+ @store.file = new_yaml_file
9
+ end
10
+
11
+ def teardown
12
+ File.delete new_yaml_file if File.exists?(new_yaml_file)
13
+ end
14
+
15
+ def test_can_write_and_read
16
+ assert_equal 'in the trunk', (@store['junk'] = 'in the trunk')
17
+ assert_equal 'in the trunk', @store['junk']
18
+ end
19
+
20
+ def test_read_from_file
21
+ @store.file = test_yaml_file
22
+ assert_equal 'bar', @store['foo']
23
+ end
24
+
25
+ def test_persists_to_file
26
+ @store['pot'] = 'roast'
27
+ assert File.exists?(new_yaml_file)
28
+ yaml = YAML.load_file(new_yaml_file)
29
+ assert_equal 'roast', yaml['pot']
30
+ end
31
+
32
+ private
33
+
34
+ def test_yaml_file
35
+ File.join(File.dirname(__FILE__), 'yaml_test.yml')
36
+ end
37
+
38
+ def new_yaml_file
39
+ File.join(File.dirname(__FILE__), 'new_yaml_test.yml')
40
+ end
41
+
42
+ end
@@ -0,0 +1 @@
1
+ foo: bar
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robut
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: ruby
7
+ authors:
8
+ - Justin Weiss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-18 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: xmpp4r
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.5.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ description: A simple plugin-enabled HipChat bot
28
+ email:
29
+ - justin@uberweiss.org
30
+ executables:
31
+ - robut
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - README.rdoc
40
+ - Rakefile
41
+ - bin/robut
42
+ - examples/Chatfile
43
+ - lib/robut.rb
44
+ - lib/robut/connection.rb
45
+ - lib/robut/plugin.rb
46
+ - lib/robut/plugin/base.rb
47
+ - lib/robut/plugin/calc.rb
48
+ - lib/robut/plugin/echo.rb
49
+ - lib/robut/plugin/later.rb
50
+ - lib/robut/plugin/lunch.rb
51
+ - lib/robut/plugin/meme.rb
52
+ - lib/robut/plugin/ping.rb
53
+ - lib/robut/plugin/rdio.rb
54
+ - lib/robut/plugin/rdio/public/css/rdio.css
55
+ - lib/robut/plugin/rdio/public/css/style.css
56
+ - lib/robut/plugin/rdio/public/css/style_after.css
57
+ - lib/robut/plugin/rdio/public/images/background.png
58
+ - lib/robut/plugin/rdio/public/images/no-album.png
59
+ - lib/robut/plugin/rdio/public/index.html
60
+ - lib/robut/plugin/rdio/public/js/libs/dd_belatedpng.js
61
+ - lib/robut/plugin/rdio/public/js/libs/jquery-1.5.1.min.js
62
+ - lib/robut/plugin/rdio/public/js/libs/modernizr-1.7.min.js
63
+ - lib/robut/plugin/rdio/public/js/rdio.js
64
+ - lib/robut/plugin/rdio/public/js/script.js
65
+ - lib/robut/plugin/rdio/server.rb
66
+ - lib/robut/plugin/say.rb
67
+ - lib/robut/plugin/sayings.rb
68
+ - lib/robut/plugin/twss.rb
69
+ - lib/robut/storage.rb
70
+ - lib/robut/storage/base.rb
71
+ - lib/robut/storage/hash_store.rb
72
+ - lib/robut/storage/yaml_store.rb
73
+ - lib/robut/version.rb
74
+ - robut.gemspec
75
+ - test/mocks/connection_mock.rb
76
+ - test/simplecov_helper.rb
77
+ - test/test_helper.rb
78
+ - test/unit/connection_test.rb
79
+ - test/unit/plugin/base_test.rb
80
+ - test/unit/plugin/echo_test.rb
81
+ - test/unit/plugin/later_test.rb
82
+ - test/unit/plugin/lunch_test.rb
83
+ - test/unit/plugin/ping_test.rb
84
+ - test/unit/plugin/say_test.rb
85
+ - test/unit/storage/hash_store_test.rb
86
+ - test/unit/storage/yaml_store_test.rb
87
+ - test/unit/storage/yaml_test.yml
88
+ has_rdoc: true
89
+ homepage: http://rdoc.info/github/justinweiss/robut/master/frames
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: -555971066717655202
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: -555971066717655202
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project: robut
118
+ rubygems_version: 1.5.2
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: A simple plugin-enabled HipChat bot
122
+ test_files:
123
+ - test/mocks/connection_mock.rb
124
+ - test/simplecov_helper.rb
125
+ - test/test_helper.rb
126
+ - test/unit/connection_test.rb
127
+ - test/unit/plugin/base_test.rb
128
+ - test/unit/plugin/echo_test.rb
129
+ - test/unit/plugin/later_test.rb
130
+ - test/unit/plugin/lunch_test.rb
131
+ - test/unit/plugin/ping_test.rb
132
+ - test/unit/plugin/say_test.rb
133
+ - test/unit/storage/hash_store_test.rb
134
+ - test/unit/storage/yaml_store_test.rb
135
+ - test/unit/storage/yaml_test.yml