8 class ChuckNorrisPlugin < Plugin
12 if path = find_facts_file('chucknorris.yml.gz')
13 fyml = Zlib::GzipReader.open(path)
14 elsif path = find_facts_File('chucknorris.yml')
17 raise "Error: Couldn't find chucknorris.yml[.gz]"
20 debug "+ [chucknorris] Loading #{path}..."
22 @@facts = YAML.load(fyml).map{|fact,(score,votes)| votes >= MIN_VOTES ? [score,fact] : nil}.compact
23 debug "+ [chucknorris] #{@@facts.length} Chuck Norris facts loaded..."
24 debug " Random fact: #{@@facts[rand(@@facts.size)].inspect}"
29 # Just a little helper for the initialize method...
30 def find_facts_file(name)
31 full_path = File.join Config::datadir, "plugins", name
32 found_files = Dir[full_path]
41 def help(plugin, topic="chuck")
42 "fact|chuck|norris|chucknorris [min_rating] => \"fact\" shows a random Chuck Norris fact (optional minimum rating from 1-10, default=6.0)."
43 #\"fact [person]\" shows a fact about someone in the channel.
48 min = params[:minrating].to_f
49 debug "+ Getting Chuck Norris fact (rating > #{min})..."
51 viable_facts = @@facts.select {|rating, fact| rating >= min}
52 if viable_facts.empty?
53 debug " - no facts found with rating >= #{min}"
54 m.reply "Are you nuts?!? There are no facts better than #{min}!!!"
58 rating, fact = viable_facts[rand(viable_facts.length)]
59 m.reply "#{fact} [score=#{rating}]"
64 plugin = ChuckNorrisPlugin.new
66 plugin.map 'fact :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
67 plugin.map 'chucknorris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
68 plugin.map 'chuck :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
69 plugin.map 'norris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}