diff --git a/lib/options.rb b/lib/options.rb new file mode 100644 index 0000000000000000000000000000000000000000..2185f8507bda0c7ec588c2ad8f16a3f77de6903e --- /dev/null +++ b/lib/options.rb @@ -0,0 +1,56 @@ +require 'optparse' +require 'yaml' + +# Allow us to get options, set by user or default ones. +class Options + def initialize(file = "#{ENV['HOME']}/.playbot") + @options = {} + @file = file + end + + # Read the options from commande line and configuration file. Command line overwrite configuration file. + def read_all + # Firt we read options from command line. + OptionParser.new do |opts| + opts.banner = "Usage: ./run.rb [OPTIONS]" + + opts.on('-h', '--help', 'show this help') do + puts opts + exit + end + + opts.on('-s', '--silent', 'set log to FATAL') do + @options[:silent] = true + end + + opts.on('-a', '--admin', 'admin nick') do + @options[:admin] = arg + end + + opts.on('-n', '--network', 'server address') do + @options[:address] = arg + end + + opts.on('-p', '--port', 'server port') do + @options[:port] = arg + end + end.parse! + + # Next we look to a configuration file. + read_file + end + + # Read the options from the configuration file. + def read_file + if File.exists?(@file) + YAML.load_file(@file).each do |k, v| + @options[k.to_sym] = v unless @options.has_key?(k) + end + end + + @options[:silent] ||= false + @options[:nicknames] ||= ['PlayBot', 'Play_Bot', 'Play__Bot', 'Play___Bot'] + @options[:channels] ||= ['#hormone'] + @options + end +end diff --git a/lib/playbot.rb b/lib/playbot.rb index 33a6ea66ab23db48850eb3f960b37b2ac9ca1337..d49ea3a94684e610bcd797dea7213e7c0d8a8740 100644 --- a/lib/playbot.rb +++ b/lib/playbot.rb @@ -78,8 +78,7 @@ class PlayBot < IRCBot handler = SitePlugin.for_site(url) return if handler.nil? - handler = handler.new(@options) - content = handler.get(url) + content = handler.new(@options).get(url) msg(event.channel, "#{content[:title]} | #{content[:author]}") end diff --git a/run.rb b/run.rb index 17adda0e386029d70ebe78198caeaa049707b038..0a0b221e4f7d9ca1ea2169a897970bf25ed906d5 100755 --- a/run.rb +++ b/run.rb @@ -1,56 +1,14 @@ #!/usr/bin/ruby require 'logger' -require 'optparse' -require 'yaml' require_relative 'lib/playbot' +require_relative 'lib/options' # This code start the PlayBot with somes options. -options = {} +options = Options.new.read_all +puts options -# First we read options from command line. -OptionParser.new do |opts| - opts.banner = "Usage: ./run.rb [OPTIONS]" - - opts.on('-h', '--help', 'show this help') do - puts opts - exit - end - - opts.on('-s', '--silent', 'set log to FATAL') do - options[:silent] = true - end - - opts.on('-a', '--admin', 'admin nick') do - options[:admin] = arg - end - - opts.on('-n', '--network', 'server address') do - options[:address] = arg - end - - opts.on('-p', '--port', 'server port') do - options[:port] = arg - end -end.parse! - -# Next we look to an configuration file. -if File.exists?("#{ENV['HOME']}/.playbot") - YAML.load_file("#{ENV['HOME']}/.playbot").each do |k, v| - options[k.to_sym] = v unless options.has_key?(k) - end -end - -options[:silent] ||= false - -bot = PlayBot.new( - :address => options[:address], - :port => options[:port], - :nicknames => ['PlayBot', 'Play_Bot', 'Play__Bot', 'Play___Bot'], - :channels => ['#hormone'], - :admin => options[:admin], - :silent => options[:silent] -) +bot = PlayBot.new(options) bot.irc_loop diff --git a/spec/site_plugin_spec.rb b/spec/site_plugin_spec.rb index 833ea20b3d0f993526b669d908def6d58daa6e0d..867e9fb3472b91d90441d2719b22c7685161a782 100644 --- a/spec/site_plugin_spec.rb +++ b/spec/site_plugin_spec.rb @@ -31,4 +31,11 @@ describe SitePlugin do lambda { @siteplugin.can_handle?('test') }.should raise_error end end + + describe '#get' do + it 'raise an exception' do + @siteplugin = SitePlugin.new + lambda { @siteplugin.get('test') }.should raise_error + end + end end diff --git a/spec/youtube_plugin_spec.rb b/spec/youtube_plugin_spec.rb index 8a6c06e11f24681c4a2fac0f16eb40dc999a7cef..621a4a34d7ca2b9f4627da4d8787227eda31af42 100644 --- a/spec/youtube_plugin_spec.rb +++ b/spec/youtube_plugin_spec.rb @@ -17,8 +17,11 @@ describe YoutubePlugin do describe '#get' do it "return video's informations" do - YoutubePlugin.new.get('http://youtube.com/watch?v=Pb8VPYMgHlg')[:title].should == 'DJ Showtek - FTS (Fuck the system)' - YoutubePlugin.new.get('http://youtube.com/watch?v=Pb8VPYMgHlg')[:author].should == 'bf2julian' + options = Options.new.read_file + video = YoutubePlugin.new(options).get('http://youtube.com/watch?v=Pb8VPYMgHlg') + + video[:title].should == 'DJ Showtek - FTS (Fuck the system)' + video[:author].should == 'bf2julian' end end end