lib/roast/main.rb in kdaigle-roast-0.0.5 vs lib/roast/main.rb in kdaigle-roast-0.0.6
- old
+ new
@@ -1,63 +1,80 @@
module Roast
class Main
-
include Tinder
- def self.run(first_msg=nil, room=nil)
-
- config_file = YAML.load_file(ENV['HOME'] + '/roast.yml')
-
- options = Hash.new
- options[:room] = (room ? room.to_s : nil) || config_file["default"]
-
+ attr_accessor :message, :room, :config, :domain, :ssl, :username, :password, :verbose
+
+ def initialize(options)
+ @message = options[:message]
+ @config = YAML.load_file(ENV['HOME'] + '/roast.yml')
+ room_config = @config[(options[:room] ? options[:room].to_s : @config["default"])]
+ @room = room_config['room']
+ @domain = room_config['domain']
+ @ssl = room_config['ssl']
+ @username = room_config['username']
+ @password = room_config['password']
+ @verbose = options[:verbose]
+ self
+ end
+
+ def self.run(options={})
+
opts = OptionParser.new do |opt|
opt.banner = "Usage: roast [options] message"
opt.separator ""
opt.separator "Specific options:"
opt.on("-r", "--room [room config]",
"Room name from config") do |room|
options[:room] = room
end
+ opt.on("-v", "--verbose",
+ "Be verbose during the posting of the message") do |verbose|
+ options[:verbose] = true
+ end
end
opts.parse!(ARGV)
- puts options.inspect
+ roast = Roast::Main.new(options)
- conf = config_file[options[:room]]
+ roast.vocalize "Using #{roast.room} configuration ..."
- msg = first_msg || ARGV[0] || STDIN.read
+ campfire = Campfire.new(roast.domain, :ssl => roast.ssl)
- puts "Using #{conf['room']} configuration ..."
+ roast.vocalize "Logging in..."
+ campfire.login(roast.username, roast.password)
+ roast.vocalize "Logged in!"
- campfire = Campfire.new(conf['domain'], :ssl => conf['ssl'])
+ room = campfire.find_room_by_name(roast.room)
- puts "Logging in..."
- campfire.login conf['username'], conf['password']
- puts "Logged in!"
+ roast.vocalize "Joining #{room.name}..."
+ roast.vocalize "Entered room #{room.name}"
- room = campfire.find_room_by_name conf["room"]
+ roast.vocalize "Saying message..."
- puts "Joining #{room.name}..."
- # room.join(true)
- puts "Entered room #{room.name}"
-
- puts "Saying message..."
-
- if msg.split("\n").size > 1
- room.paste(msg)
+ if message_long?(options[:message])
+ room.paste(options[:message])
else
- room.speak(msg)
+ room.speak(options[:message])
end
- puts "Leaving room..."
+ roast.vocalize "Leaving room..."
- # room.leave
+ roast.vocalize "Done."
+ end
+
+ def vocalize(message)
+ STDOUT.puts message if self.verbose
+ end
+
+ private
- puts "Done."
+ def self.message_long?(message)
+ (message.split("\n").size > 1) || (message.size >= 255)
end
+
end
end
\ No newline at end of file