Sha256: 88e7388be7b91096ad9dd12f5a48e3a8cd0e6b7d80fc1106606f3718cfc1b3b2

Contents?: true

Size: 855 Bytes

Versions: 1

Compression:

Stored size: 855 Bytes

Contents

require 'cinch'

module Cinch::Plugins
  class Calculate
    include Cinch::Plugin

    self.help = "Use .math <problem> to do math computations. (e.g. 5 feet / inches, .math sin(2) + 4)"

    match /math (.+)/

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

    def execute(m, problem)
      m.reply math(problem), true
    end

    private

    def math(problem_string)
      return 'Sorry, I can\'t do that' unless units_binary_exists?
      return IO.popen([@units_path, "-t", problem_string]).readline
    rescue EOFError
      debug "Make sure you have GNU Units installed and not the BSD Units that ships with OSX."
    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-calculate-1.0.0 lib/cinch/plugins/calculate/calculate.rb