From e5fdc65bfb6f3c55aaa21aab14a36bb1d3b4467d Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@gmail.com>
Date: Wed, 2 Jan 2013 21:13:27 +0100
Subject: [PATCH] Class Options

---
 lib/options.rb              | 56 +++++++++++++++++++++++++++++++++++++
 lib/playbot.rb              |  3 +-
 run.rb                      | 50 +++------------------------------
 spec/site_plugin_spec.rb    |  7 +++++
 spec/youtube_plugin_spec.rb |  7 +++--
 5 files changed, 73 insertions(+), 50 deletions(-)
 create mode 100644 lib/options.rb

diff --git a/lib/options.rb b/lib/options.rb
new file mode 100644
index 0000000..2185f85
--- /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 33a6ea6..d49ea3a 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 17adda0..0a0b221 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 833ea20..867e9fb 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 8a6c06e..621a4a3 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
-- 
GitLab