lib/cinch/plugins/convert/convert.rb in cinch-convert-0.0.1 vs lib/cinch/plugins/convert/convert.rb in cinch-convert-1.0.0
- old
+ new
@@ -1,53 +1,49 @@
+require 'cinch'
+
module Cinch::Plugins
class Convert
include Cinch::Plugin
self.help = "Use .convert <thing 1> to <thing 2> to do a unit conversion. (e.g. .convert 5 feet to meters)"
- match /convert (.+)/
+ match /convert (.+) to (.+)/
def initialize(*args)
super
@units_path = config[:units_path] || '/usr/bin/units'
end
- def execute(m, conversion_string)
- m.reply convert(conversion_string), true
+ def execute(m, from, to)
+ m.reply convert(from, to), true
end
private
- def convert(conversion)
- unless File.exist? @units_path
- debug "Cinch can't find the unit conversion binary."
- return "Sorry I can't convert that."
- end
+ def convert(from, to)
+ return "Sorry, there's a configuration issue." unless units_binary_exists?
- match = conversion.match(/([+\d\.-]+)\s*([\/*\s\w]+) to ([\/*\s\w]+)$/)
+ unless from.nil? || to.nil?
+ units_output = IO.popen([@units_path, "-t", from, to])
- unless match.nil?
- num = match[1]
- units_from = match[2]
- units_to = match[3]
-
- units_output = IO.popen([@units_path, "-t", "#{num} #{units_from}", units_to])
-
# we only take one line here because the first line of output is
# either an error message or the part of the conversion output we
# want.
- units_line = units_output.readline
- units_line.chomp!
+ units_line = units_output.readline.chomp!
- if units_line =~ /Unknown unit/
+ if units_line.match(/Unknown unit/)
"Sorry, #{units_line.downcase}."
- elsif units_line =~ /conformability error/
+ elsif units_line.match(/conformability error/)
"Sorry, there was a conformability error when making that conversion."
else
- "#{num} #{units_from} is #{units_line} #{units_to}."
+ "#{from} is #{units_line} #{to}."
end
- else
- "Sorry, I don't understand, please use `.convert X unitname to unitname`."
end
+ end
+
+ def units_binary_exists?
+ return true if File.exist? @units_path
+ debug "Cinch can't find the unit conversion binary."
+ false
end
end
end