ircbot 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ircbot (0.2.0)
4
+ ircbot (0.2.1)
5
5
  chawan
6
6
  data_objects
7
7
  dm-core
@@ -50,7 +50,7 @@ GEM
50
50
  optionize (>= 0.1.0)
51
51
  extlib (0.9.15)
52
52
  net-irc (0.0.9)
53
- night-time (1.0.1)
53
+ night-time (1.0.2)
54
54
  optionize (0.1.1)
55
55
  pg (0.14.0)
56
56
  rspec (2.10.0)
@@ -13,7 +13,9 @@ plugins:
13
13
  -
14
14
  name: reminder
15
15
  db: "postgres://localhost/ircbot"
16
- - summary
16
+ -
17
+ name: summary
18
+ engines: http, https, ch2, twitter
17
19
  reminder
18
20
  multiline: true
19
21
  timeout: 500
@@ -20,8 +20,11 @@ module Ircbot
20
20
  attr_accessor :plugin_name
21
21
 
22
22
  def self.command?(name)
23
- @commands ||= (public_instance_methods - Plugin.public_instance_methods).inject({"setup"=>1, "help"=>1}) {|h,k| h[k.to_s] = 1; h }
24
- !! @commands[name.to_s]
23
+ !! commands[name.to_s]
24
+ end
25
+
26
+ def self.commands
27
+ @commands ||= (public_instance_methods - Plugin.public_instance_methods + %w(help) - %w(reply)).inject({}) {|h,k| h[k.to_s] = 1; h }
25
28
  end
26
29
 
27
30
  def initialize(plugins = nil)
@@ -68,7 +71,7 @@ module Ircbot
68
71
  end
69
72
 
70
73
  def help
71
- raise "no helps for #{plugin_name}"
74
+ self[:help] || "commands: %s" % self.class.commands.keys.sort.join(",")
72
75
  end
73
76
 
74
77
  ######################################################################
@@ -1,4 +1,4 @@
1
1
  module Ircbot
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.2'
3
3
  HOMEPAGE = "http://github.com/maiha/ircbot"
4
4
  end
@@ -34,7 +34,7 @@ commands: load, start, stop, delete
34
34
 
35
35
  when "help"
36
36
  if arg.empty?
37
- done [bot_help, plugins_help].join("\n")
37
+ done [config_help, plugins_help, bot_help].compact.join("\n")
38
38
  else
39
39
  done plugins[arg].help
40
40
  end
@@ -49,8 +49,11 @@ commands: load, start, stop, delete
49
49
  end
50
50
 
51
51
  def bot_help
52
- config.help ||
53
- "%s bot [%s (ver:%s)]" % [config.nick, Ircbot::HOMEPAGE, Ircbot::VERSION]
52
+ "%s bot [%s (ver:%s)]" % [config.nick, Ircbot::HOMEPAGE, Ircbot::VERSION]
53
+ end
54
+
55
+ def config_help
56
+ config.help
54
57
  end
55
58
  end
56
59
 
@@ -126,11 +126,6 @@ class ReminderPlugin < Ircbot::Plugin
126
126
  end
127
127
  end
128
128
 
129
- def help
130
- ["list -> show future reminders",
131
- "YYYY-mm-dd or YYYY-mm-dd HH:MM text -> register the text"].join("\n")
132
- end
133
-
134
129
  def setup
135
130
  return if @watcher
136
131
  bot = self.bot
@@ -2,7 +2,9 @@ require 'dsl_accessor'
2
2
  require 'extlib'
3
3
 
4
4
  module Engines
5
- Mapping = []
5
+ Mapping = []
6
+ Loaded = {}
7
+ NotFound = Class.new(RuntimeError)
6
8
 
7
9
  class NotImplementedError < NotImplementedError; end
8
10
  class Nop < NotImplementedError; end
@@ -14,17 +16,48 @@ module Engines
14
16
  raise NotImplementedError, "[BUG] Not supported URL: %s" % url
15
17
  end
16
18
 
17
- # load ruby library and register its url
18
- def self.register(name)
19
- load File.dirname(__FILE__) + "/engines/#{name}.rb"
20
- klass = instance_eval(Extlib::Inflection.camelize(name))
21
- Mapping.unshift [klass.url, klass] unless klass == Base
19
+ def self.[](name)
20
+ unless loaded?(name)
21
+ raise NotFound, name.to_s
22
+ end
23
+ instance_eval(Extlib::Inflection.camelize(name))
24
+ end
25
+
26
+ def self.setup(*names)
27
+ Mapping.clear
28
+ names = (%w( none ) + names).flatten.uniq
29
+ debug "[Summary] Engines.setup(%s)" % names.inspect
30
+ names.each do |name|
31
+ klass = self[name]
32
+ Mapping.unshift [klass.url, klass]
33
+ end
34
+ end
35
+
36
+ # load ruby library
37
+ def self.load(name)
38
+ name = name.to_s
39
+ return false if loaded?(name)
40
+
41
+ file = File.dirname(__FILE__) + "/engines/#{name}.rb"
42
+ Kernel.load(file)
43
+
44
+ Loaded[name] = Time.now
45
+ end
46
+ def self.loaded?(name) Loaded[name]; end
47
+
48
+ def self.load_engines
49
+ load :base
50
+ load :none
51
+ Dir.glob(File.dirname(__FILE__) + "/engines/*.rb").sort.each do |file|
52
+ name = File.basename(file, ".*")
53
+ load(name)
54
+ end
22
55
  end
23
56
 
24
- register("base")
25
- register("none")
26
- register("https")
27
- register("ch2")
28
- register("twitter")
57
+ # logger
58
+ def self.debug(msg)
59
+ puts msg.to_s
60
+ end
29
61
  end
30
62
 
63
+ Engines.load_engines
@@ -0,0 +1,6 @@
1
+ module Engines
2
+ class Http < Base
3
+ url %r{^http://}
4
+ end
5
+ end
6
+
@@ -1,6 +1,24 @@
1
1
  require File.join(File.dirname(__FILE__), "spec_helper")
2
2
 
3
- describe Engines do
3
+ describe Engines, "Engines with default settings" do
4
+ before { Engines.setup }
5
+
6
+ summary "http://www.asahi.com" do
7
+ its(:class) {should == Engines::None}
8
+ end
9
+ end
10
+
11
+ describe Engines, "Engines(http)" do
12
+ before { Engines.setup("http") }
13
+
14
+ summary "http://www.asahi.com" do
15
+ its(:class) {should == Engines::Http}
16
+ end
17
+ end
18
+
19
+ describe Engines, "Engines(https,twitter,ch2)" do
20
+ before { Engines.setup(["https", "twitter", "ch2"]) }
21
+
4
22
  summary "https://example.com" do
5
23
  its(:class) {should == Engines::Https}
6
24
  end
@@ -13,14 +13,26 @@ require 'engines'
13
13
  # [TEST]
14
14
  # cd plugins/summary
15
15
  # rspec -c spec
16
+ #
17
+ # [CONFIG]
18
+ # engines: *NAME_OF_ENGINES
19
+ # (see. config/samples/postgres.yml)
16
20
 
17
21
  class SummaryPlugin < Ircbot::Plugin
18
22
  Quote = ">> "
23
+ DEFAULT_ENGINES = %w( http https ch2 twitter )
19
24
 
20
25
  def help
21
26
  "[Summary] summarize web page (responds to only 2ch or https)"
22
27
  end
23
28
 
29
+ def setup
30
+ engines = self[:engines]
31
+ engines = engines.to_s.split(/\s*[,\s]\s*/).compact unless engines.is_a?(Array)
32
+ engines = DEFAULT_ENGINES if engines.blank?
33
+ Engines.setup(engines)
34
+ end
35
+
24
36
  def reply(text)
25
37
  scan_urls(text).each do |url|
26
38
  summary = once(url) {
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ircbot
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-03 00:00:00 Z
18
+ date: 2012-07-31 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: dsl_accessor
@@ -289,6 +289,7 @@ files:
289
289
  - plugins/summary/engines.rb
290
290
  - plugins/summary/engines/base.rb
291
291
  - plugins/summary/engines/ch2.rb
292
+ - plugins/summary/engines/http.rb
292
293
  - plugins/summary/engines/https.rb
293
294
  - plugins/summary/engines/none.rb
294
295
  - plugins/summary/engines/twitter.rb