Sha256: 48c2f3367bca8312972a3e6478bef2bf9b82266696131f7bf0c3c6b07bb43fcf

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

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 (.+) to (.+)/

    def initialize(*args)
      super
      @units_path = config[:units_path] || '/usr/bin/units'
    end

    def execute(m, from, to)
      m.reply convert(from, to), true
    end

    private

    def convert(from, to)
      return "Sorry, there's a configuration issue." unless units_binary_exists?

      unless from.nil? || to.nil?
        units_output = IO.popen([@units_path, "-t", from, 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.chomp!

        if units_line.match(/Unknown unit/)
          "Sorry, #{units_line.downcase}."
        elsif units_line.match(/conformability error/)
          "Sorry, there was a conformability error when making that conversion."
        else
          "#{from} is #{units_line} #{to}."
        end
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cinch-convert-1.0.0 lib/cinch/plugins/convert/convert.rb