raibo 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  Botfile
6
+ /tags
data/bin/raibo CHANGED
@@ -57,5 +57,5 @@ b = Raibo::Bot.new(server,
57
57
  :nick => options.nick,
58
58
  :channel => options.channel,
59
59
  :verbose => options.verbose)
60
- b.instance_eval(File.read(options.botfile))
60
+ b.load_config_file(options.botfile)
61
61
  b.run
@@ -7,3 +7,4 @@ require 'raibo/version'
7
7
  require 'raibo/connection'
8
8
  require 'raibo/bot'
9
9
  require 'raibo/message'
10
+ require 'raibo/dsl'
@@ -1,27 +1,34 @@
1
1
  module Raibo
2
2
  class Bot
3
- def initialize(*args)
4
- @handlers = []
3
+ attr_accessor :docs
5
4
 
5
+ def initialize(*args)
6
6
  if args.first.is_a?(Raibo::Connection)
7
7
  @connection = args.shift
8
8
  else
9
9
  @connection = Raibo::Connection.new(*args)
10
10
  end
11
- end
12
11
 
13
- def use(handler=nil, &block)
14
- @handlers.push(handler || block)
12
+ reset
15
13
  end
16
14
 
17
- def match(regexp, &block)
15
+ def reset
16
+ @handlers = []
17
+ @docs = []
18
+ @dsl = Raibo::DSL.new(self, @connection)
19
+
18
20
  use do |msg|
19
- if msg.body =~ regexp
20
- exec_with_var_arity($~, msg, &block)
21
+ if msg.body == '!help'
22
+ @bot.help
21
23
  end
22
24
  end
23
25
  end
24
26
 
27
+ def use(handler=nil, &block)
28
+ @handlers.push(handler || block)
29
+ @docs.concat(handler.docs) if handler.respond_to?(:docs)
30
+ end
31
+
25
32
  def run(async=false)
26
33
  if async
27
34
  @thread = Thread.new { run_sync }
@@ -40,6 +47,16 @@ module Raibo
40
47
  @thread.alive? if @thread
41
48
  end
42
49
 
50
+ def load_config_file(filename)
51
+ @dsl.instance_eval(IO.read(filename), filename)
52
+ end
53
+
54
+ def help
55
+ width = [15, @docs.map(&:first).map(&:length).max].max
56
+
57
+ @connection.say(*@docs.map { |cmd, desc| "%-#{width}s %s" % [cmd, desc] })
58
+ end
59
+
43
60
  private
44
61
  def run_sync
45
62
  @connection.open
@@ -54,10 +71,14 @@ module Raibo
54
71
  end
55
72
  end
56
73
  @handlers.each do |handler|
57
- if handler.is_a?(Proc)
58
- break if @connection.instance_exec(message, &handler)
59
- else
60
- break if handler.call(@connection, message)
74
+ begin
75
+ if handler.is_a?(Proc)
76
+ break if @dsl.instance_exec(message, &handler)
77
+ else
78
+ break if handler.call(@connection, message)
79
+ end
80
+ rescue Exception => e
81
+ @connection.say e.inspect
61
82
  end
62
83
  end
63
84
  end
@@ -57,8 +57,8 @@ module Raibo
57
57
  send "PART #{channel}"
58
58
  end
59
59
 
60
- def say(msg)
61
- msg(@channel, msg)
60
+ def say(*msgs)
61
+ msgs.each { |msg| msg(@channel, msg) }
62
62
  end
63
63
 
64
64
  def msg(dest, msg)
@@ -69,14 +69,5 @@ module Raibo
69
69
  puts "<-- #{str}" if @verbose
70
70
  @connection.puts(str)
71
71
  end
72
-
73
- def exec_with_var_arity(*args, &block)
74
- arity = block.arity
75
- if arity <= 0
76
- instance_eval(&block)
77
- else
78
- instance_exec(*args.take(block.arity), &block)
79
- end
80
- end
81
72
  end
82
73
  end
@@ -0,0 +1,38 @@
1
+ module Raibo
2
+ class DSL
3
+ def initialize(bot, connection)
4
+ @bot = bot
5
+ @connection = connection
6
+ end
7
+
8
+ def use(handler=nil, &block)
9
+ @bot.use(handler, &block)
10
+ end
11
+
12
+ def match(regexp, &block)
13
+ use do |msg|
14
+ if msg.body =~ regexp
15
+ exec_with_var_arity($~, msg, &block)
16
+ end
17
+ end
18
+ end
19
+
20
+ def doc(cmd, desc='')
21
+ @bot.docs << [cmd, desc]
22
+ end
23
+
24
+ def method_missing(meth, *args, &block)
25
+ @connection.__send__(meth, *args, &block)
26
+ end
27
+
28
+ private
29
+ def exec_with_var_arity(*args, &block)
30
+ arity = block.arity
31
+ if arity <= 0
32
+ instance_eval(&block)
33
+ else
34
+ instance_exec(*args.take(block.arity), &block)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Raibo
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,29 +1,24 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: raibo
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
4
5
  prerelease:
5
- version: 0.0.3
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Ryan Fitzgerald
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-07-17 00:00:00 -07:00
14
- default_executable:
12
+ date: 2011-08-30 00:00:00.000000000Z
15
13
  dependencies: []
16
-
17
- description: ""
18
- email:
14
+ description: ''
15
+ email:
19
16
  - rwfitzge@gmail.com
20
- executables:
17
+ executables:
21
18
  - raibo
22
19
  extensions: []
23
-
24
20
  extra_rdoc_files: []
25
-
26
- files:
21
+ files:
27
22
  - .gitignore
28
23
  - Gemfile
29
24
  - Rakefile
@@ -32,39 +27,36 @@ files:
32
27
  - lib/raibo.rb
33
28
  - lib/raibo/bot.rb
34
29
  - lib/raibo/connection.rb
30
+ - lib/raibo/dsl.rb
35
31
  - lib/raibo/message.rb
36
32
  - lib/raibo/version.rb
37
33
  - raibo.gemspec
38
34
  - spec/message_spec.rb
39
35
  - spec/spec_helper.rb
40
- has_rdoc: true
41
- homepage: ""
36
+ homepage: ''
42
37
  licenses: []
43
-
44
38
  post_install_message:
45
39
  rdoc_options: []
46
-
47
- require_paths:
40
+ require_paths:
48
41
  - lib
49
- required_ruby_version: !ruby/object:Gem::Requirement
42
+ required_ruby_version: !ruby/object:Gem::Requirement
50
43
  none: false
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: "0"
55
- required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
49
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: "0"
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
61
54
  requirements: []
62
-
63
55
  rubyforge_project: raibo
64
- rubygems_version: 1.5.2
56
+ rubygems_version: 1.8.6
65
57
  signing_key:
66
58
  specification_version: 3
67
59
  summary: A simple IRC library
68
- test_files:
60
+ test_files:
69
61
  - spec/message_spec.rb
70
62
  - spec/spec_helper.rb