bitbckt-botbckt 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -15,6 +15,12 @@ begin
15
15
  g.homepage = 'http://github.com/bitbckt/botbckt'
16
16
  g.description = 'Boredom strikes on Sunday mornings.'
17
17
  g.authors = ['Brandon Mitchell']
18
+ g.executables << 'botbckt'
19
+ g.add_dependency('eventmachine', '>= 0.12.0')
20
+ g.add_dependency('activesupport')
21
+ g.add_dependency('json', '>= 1.1.2')
22
+ g.add_dependency('hpricot', '>= 0.6')
23
+ g.add_dependency('em-redis', '>= 0.1.1')
18
24
  end
19
25
  rescue LoadError
20
26
  puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 0
2
+ :patch: 2
3
3
  :major: 0
4
4
  :minor: 4
data/lib/botbckt/bot.rb CHANGED
@@ -11,6 +11,7 @@ module Botbckt #:nodoc:
11
11
 
12
12
  attr_accessor :logger
13
13
  attr_accessor :store
14
+ attr_accessor :connection
14
15
 
15
16
  # ==== Parameters
16
17
  # options<Hash{Symbol => String,Integer}>
@@ -29,32 +30,21 @@ module Botbckt #:nodoc:
29
30
  # :backend_port<Integer>:: The port used by the Redis store. Optional.
30
31
  #
31
32
  def self.start(options)
32
-
33
- self.instance.logger = ActiveSupport::BufferedLogger.new options.delete(:log) || 'botbckt.log',
34
- options.delete(:log_level) || INFO
35
-
36
- host, port = options.delete(:backend_host), options.delete(:backend_port)
37
33
  daemonize = options.delete(:daemonize)
38
34
  pid = options.delete(:pid) || 'botbckt.pid'
39
35
 
40
36
  if daemonize || daemonize.nil?
41
37
  EventMachine::fork_reactor do
42
- Botbckt::IRC.connect(options)
43
-
44
- if host && port
45
- self.instance.store = Store.new(host, port)
46
- end
38
+ start!
47
39
 
48
40
  if pid
49
- File.open(pid, 'w'){ |f| f.write("#{Process.pid}") }
41
+ File.open(pid, 'w') { |file| file.write("#{Process.pid}") }
50
42
  at_exit { File.delete(pid) if File.exist?(pid) }
51
43
  end
52
44
 
53
45
  end
54
46
  else
55
- EventMachine::run do
56
- Botbckt::IRC.connect(options)
57
- end
47
+ EventMachine::run { start! }
58
48
  end
59
49
  end
60
50
 
@@ -75,6 +65,14 @@ module Botbckt #:nodoc:
75
65
  @commands
76
66
  end
77
67
 
68
+ # Sets the key to the given value, creating the key if necessary.
69
+ #
70
+ # ==== Parameters
71
+ # key<String>:: The identifier for this value. Required.
72
+ # value<Object>:: The value to store at the key. Required.
73
+ # &block:: A callback to execute after the value is stored. The block should
74
+ # take a single parameter: the value stored. Optional.
75
+ #
78
76
  #--
79
77
  # TODO: Forwardable?
80
78
  #++
@@ -82,6 +80,13 @@ module Botbckt #:nodoc:
82
80
  self.store && self.store.set(key, value, &block)
83
81
  end
84
82
 
83
+ # Retrieves the value stored at key. Returns nil if the key does not exist.
84
+ #
85
+ # ==== Parameters
86
+ # key<String>:: The identifier to retrieve. Required.
87
+ # &block:: A callback to execute after the value is retrieved. The block should
88
+ # take a single parameter: the value retrieved. Required.
89
+ #
85
90
  #--
86
91
  # TODO: Forwardable?
87
92
  #++
@@ -89,6 +94,14 @@ module Botbckt #:nodoc:
89
94
  self.store && self.store.get(key, &block)
90
95
  end
91
96
 
97
+ # Increments the value stored at key by 1, creating the key and initializing
98
+ # it to 0 if necessary.
99
+ #
100
+ # ==== Parameters
101
+ # key<String>:: The identifier whose value should be incremented. Required.
102
+ # &block:: A callback to execute after the value is stored. The block should
103
+ # take a single parameter: the value stored. Optional.
104
+ #
92
105
  #--
93
106
  # TODO: Forwardable?
94
107
  #++
@@ -106,7 +119,7 @@ module Botbckt #:nodoc:
106
119
  # TODO: Before/after callbacks?
107
120
  #++
108
121
  def run(command, sender, channel, *args)
109
- callable = commands[command.to_sym]
122
+ callable = commands[command.to_sym] || raise("Unregistered command called. (#{command})")
110
123
 
111
124
  if callable.is_a?(Class)
112
125
  # Callables are Singletons; we use #create! as a convention to give
@@ -114,9 +127,14 @@ module Botbckt #:nodoc:
114
127
  callable = callable.create!(sender, channel, *args)
115
128
  end
116
129
 
117
- callable.respond_to?(:call) ? callable.call(sender, channel, *args) : say(Bot.befuddled, channel)
118
- # TODO: Log me.
119
- rescue StandardError => e
130
+ if callable.respond_to?(:call)
131
+ callable.call(sender, channel, *args)
132
+ else
133
+ raise("Non-callable used as command. (#{command})")
134
+ end
135
+
136
+ rescue StandardError => error
137
+ log "ERROR:\n#{error.inspect}\n#{error.backtrace}", DEBUG
120
138
  say Bot.befuddled, channel
121
139
  end
122
140
 
@@ -140,17 +158,36 @@ module Botbckt #:nodoc:
140
158
  end
141
159
 
142
160
  # ==== Parameters
143
- # msg<String>:: A message to send to the channel
161
+ # msg<String>:: A message to send to the channel. Required.
144
162
  # channel<String>:: The channel to send the message. Required.
145
163
  #
146
164
  def say(msg, channel)
147
- Botbckt::IRC.connection.say msg, channel
165
+ self.connection.say msg, channel
148
166
  end
149
167
 
150
- def log(msg, level = INFO) #:nodoc:
168
+ # ==== Parameters
169
+ # msg<String>:: A message to log. Required.
170
+ # level<Integer>:: The minimum log level at which to log this message. Defaults to INFO.
171
+ #
172
+ def log(msg, level = INFO)
151
173
  self.logger.add(level, msg)
152
174
  end
153
175
 
176
+ private
177
+
178
+ def self.start!(options) #:nodoc:
179
+ self.instance.logger = ActiveSupport::BufferedLogger.new options.delete(:log) || 'botbckt.log',
180
+ options.delete(:log_level) || INFO
181
+
182
+ host, port = options.delete(:backend_host), options.delete(:backend_port)
183
+
184
+ if host && port
185
+ self.instance.store = Store.new(host, port)
186
+ end
187
+
188
+ self.instance.connection = Botbckt::IRC.connect(options.merge(:bot => self.instance))
189
+ end
190
+
154
191
  end
155
192
 
156
193
  end
data/lib/botbckt/cmd.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Botbckt #:nodoc:
2
2
 
3
- class Cmd
3
+ class Cmd #:nodoc:
4
4
 
5
5
  SEVERITY = %w{0 1 2 3 4 5}
6
6
  SEVERITY_ALIASES = { "DEBUG" => 0, "INFO" => 1, "WARN" => 2, "ERROR" => 3, "FATAL" => 4, "UNKNOWN" => 5}
@@ -49,7 +49,7 @@ module Botbckt #:nodoc:
49
49
  # channel<String>:: The channel to send the message. Required.
50
50
  #
51
51
  def self.say(msg, channel)
52
- Botbckt::Bot.instance.say(msg, channel) if msg
52
+ Botbckt::Bot.instance.say(msg, channel)
53
53
  end
54
54
 
55
55
  # Proxy for Command.say
@@ -58,14 +58,37 @@ module Botbckt #:nodoc:
58
58
  self.class.say(msg, channel)
59
59
  end
60
60
 
61
+ # Sets the key to the given value, creating the key if necessary.
62
+ #
63
+ # ==== Parameters
64
+ # key<String>:: The identifier for this value. Required.
65
+ # value<Object>:: The value to store at the key. Required.
66
+ # &block:: A callback to execute after the value is stored. The block should
67
+ # take a single parameter: the value stored. Optional.
68
+ #
61
69
  def set(key, value, &block)
62
70
  Botbckt::Bot.instance.set(key, value, &block)
63
71
  end
64
72
 
73
+ # Retrieves the value stored at key. Returns nil if the key does not exist.
74
+ #
75
+ # ==== Parameters
76
+ # key<String>:: The identifier to retrieve. Required.
77
+ # &block:: A callback to execute after the value is retrieved. The block should
78
+ # take a single parameter: the value retrieved. Required.
79
+ #
65
80
  def get(key, &block)
66
81
  Botbckt::Bot.instance.get(key, &block)
67
82
  end
68
83
 
84
+ # Increments the value stored at key by 1, creating the key and initializing
85
+ # it to 0 if necessary.
86
+ #
87
+ # ==== Parameters
88
+ # key<String>:: The identifier whose value should be incremented. Required.
89
+ # &block:: A callback to execute after the value is stored. The block should
90
+ # take a single parameter: the value stored. Optional.
91
+ #
69
92
  def increment!(key, &block)
70
93
  Botbckt::Bot.instance.increment!(key, &block)
71
94
  end
data/lib/botbckt/irc.rb CHANGED
@@ -60,7 +60,7 @@ module Botbckt #:nodoc:
60
60
  args << $5.squish if $5
61
61
 
62
62
  # run args: command, sender, channel, optional args
63
- Botbckt::Bot.instance.run($4, *args)
63
+ self.config.bot.run($4, *args)
64
64
  else
65
65
  log line
66
66
  end
@@ -76,7 +76,7 @@ module Botbckt #:nodoc:
76
76
  private
77
77
 
78
78
  def log(msg, level = Botbckt::Bot::INFO) #:nodoc:
79
- Botbckt::Bot.instance.log msg, level
79
+ self.config.bot.log msg, level
80
80
  end
81
81
 
82
82
  def command(*cmd) #:nodoc:
data/lib/botbckt/store.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'em-redis'
2
-
3
1
  module Botbckt #:nodoc:
4
2
 
5
3
  # Implements a basic key/value store API for cross-session state storage.
@@ -11,18 +9,46 @@ module Botbckt #:nodoc:
11
9
 
12
10
  attr_accessor :backend
13
11
 
12
+ # ==== Parameters
13
+ # host<String>:: IP address or hostname of the Redis server. Required.
14
+ # port<Integer>:: Port the Redis server is listening on. Required.
15
+ #
14
16
  def initialize(host, port)
15
17
  self.backend = EventMachine::Protocols::Redis.connect(host, port)
16
18
  end
17
19
 
20
+ # Sets the key to the given value, creating the key if necessary.
21
+ #
22
+ # ==== Parameters
23
+ # key<String>:: The identifier for this value. Required.
24
+ # value<Object>:: The value to store at the key. Required.
25
+ # &block:: A callback to execute after the value is stored. The block should
26
+ # take a single parameter: the value stored. Optional.
27
+ #
18
28
  def set(key, value, &block)
19
29
  backend.set(key, value, &block)
20
30
  end
21
-
31
+
32
+ # Retrieves the value stored at key. Returns nil if the key does not exist.
33
+ #
34
+ # ==== Parameters
35
+ # key<String>:: The identifier to retrieve. Required.
36
+ # &block:: A callback to execute after the value is retrieved. The block should
37
+ # take a single parameter: the value retrieved. Required.
38
+ #
22
39
  def get(key, &block)
40
+ return unless block_given?
23
41
  backend.get(key, &block)
24
42
  end
25
43
 
44
+ # Increments the value stored at key by 1, creating the key and initializing
45
+ # it to 0 if necessary.
46
+ #
47
+ # ==== Parameters
48
+ # key<String>:: The identifier whose value should be incremented. Required.
49
+ # &block:: A callback to execute after the value is stored. The block should
50
+ # take a single parameter: the value stored. Optional.
51
+ #
26
52
  def increment!(key, &block)
27
53
  backend.incr(key, &block)
28
54
  end
data/lib/botbckt.rb CHANGED
@@ -1,3 +1,3 @@
1
- %w{ rubygems eventmachine activesupport ostruct json open-uri cgi hpricot singleton optparse }.each { |lib| require lib }
1
+ %w{ rubygems eventmachine activesupport ostruct json open-uri cgi hpricot singleton optparse em-redis }.each { |lib| require lib }
2
2
  %w{ irc store bot utilities command cmd }.each { |lib| require File.dirname(__FILE__) + "/botbckt/#{ lib }" }
3
3
  Dir[File.dirname(__FILE__) + '/botbckt/commands/*'].each { |lib| require lib }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitbckt-botbckt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mitchell
@@ -9,14 +9,64 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-23 00:00:00 -07:00
13
- default_executable: botbckt
14
- dependencies: []
15
-
12
+ date: 2009-06-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: eventmachine
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.12.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.1.2
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: hpricot
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0.6"
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: em-redis
57
+ type: :runtime
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 0.1.1
64
+ version:
16
65
  description: Boredom strikes on Sunday mornings.
17
66
  email: brandon@systemisdown.net
18
67
  executables:
19
68
  - botbckt
69
+ - botbckt
20
70
  extensions: []
21
71
 
22
72
  extra_rdoc_files: