Skip to content
Extraits de code Groupes Projets
Valider e5fdc65b rédigé par Alexandre Morignot's avatar Alexandre Morignot
Parcourir les fichiers

Class Options

parent d1bb1251
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
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
...@@ -78,8 +78,7 @@ class PlayBot < IRCBot ...@@ -78,8 +78,7 @@ class PlayBot < IRCBot
handler = SitePlugin.for_site(url) handler = SitePlugin.for_site(url)
return if handler.nil? return if handler.nil?
handler = handler.new(@options) content = handler.new(@options).get(url)
content = handler.get(url)
msg(event.channel, "#{content[:title]} | #{content[:author]}") msg(event.channel, "#{content[:title]} | #{content[:author]}")
end end
......
#!/usr/bin/ruby #!/usr/bin/ruby
require 'logger' require 'logger'
require 'optparse'
require 'yaml'
require_relative 'lib/playbot' require_relative 'lib/playbot'
require_relative 'lib/options'
# This code start the PlayBot with somes options. # This code start the PlayBot with somes options.
options = {} options = Options.new.read_all
puts options
# First we read options from command line. bot = PlayBot.new(options)
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.irc_loop bot.irc_loop
...@@ -31,4 +31,11 @@ describe SitePlugin do ...@@ -31,4 +31,11 @@ describe SitePlugin do
lambda { @siteplugin.can_handle?('test') }.should raise_error lambda { @siteplugin.can_handle?('test') }.should raise_error
end end
end end
describe '#get' do
it 'raise an exception' do
@siteplugin = SitePlugin.new
lambda { @siteplugin.get('test') }.should raise_error
end
end
end end
...@@ -17,8 +17,11 @@ describe YoutubePlugin do ...@@ -17,8 +17,11 @@ describe YoutubePlugin do
describe '#get' do describe '#get' do
it "return video's informations" do it "return video's informations" do
YoutubePlugin.new.get('http://youtube.com/watch?v=Pb8VPYMgHlg')[:title].should == 'DJ Showtek - FTS (Fuck the system)' options = Options.new.read_file
YoutubePlugin.new.get('http://youtube.com/watch?v=Pb8VPYMgHlg')[:author].should == 'bf2julian' 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 end
end end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter