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}"
33 # Just a little helper for the initialize method...
34 def find_facts_file(name)
35 full_path = File.join Config::datadir, "plugins", name
36 found_files = Dir[full_path]
45 def help(plugin, topic="chuck")
46 "fact|chuck|norris|chucknorris [min_rating] => \"fact\" shows a random Chuck Norris fact (optional minimum rating from 1-10, default=6.0)."
47 #\"fact [person]\" shows a fact about someone in the channel.
52 min = params[:minrating].to_f
53 debug "+ Getting Chuck Norris fact (rating > #{min})..."
55 viable_facts = @@facts.select {|rating, fact| rating >= min}
56 if viable_facts.empty?
57 debug " - no facts found with rating >= #{min}"
58 m.reply "Are you nuts?!? There are no facts better than #{min}!!!"
62 rating, fact = viable_facts[rand(viable_facts.length)]
63 m.reply "#{fact} [score=#{rating}]"
68 plugin = ChuckNorrisPlugin.new
70 plugin.map 'fact :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
71 plugin.map 'chucknorris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
72 plugin.map 'chuck :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
73 plugin.map 'norris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}