Sha256: a675bddbe1c6839313853fab401b5940c3cbf8352109b085b05e001a3fbe5155

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Lita
  module Handlers
    class OnewheelWordJumble < Handler

      route /^words\s+([a-zA-Z]+)\s*(\d*)$/i, :words, command: true

      def words(response)
        Lita.logger.debug('Loading words...')

        # Build the dict of words
        words = {}
        File.open(File.expand_path('../../words', File.dirname(__FILE__))).each_line do |l|
          l.chomp!
          words[l.downcase] = 1
        end

        letters = response.matches[0][0]
        len = response.matches[0][1].to_i

        if len == 0
          min_len = 3
          len = letters.length
        else
          min_len = len
        end

        Lita.logger.debug "Searching for alternatives to #{letters} with a length between #{min_len} and #{len}"

        combos = []

        (min_len..len).each do |n|
          Lita.logger.debug "Checking for #{n} length words..."
          arr = []
          letters.split(//).permutation(n).to_a.map(&:join).each do |combo|
            arr.push combo if words[combo] == 1
          end
          combos.push arr.sort.uniq
        end

        combo_str = combos.join ", "

        Lita.logger.info "Returning: #{combo_str}"

        response.reply(combo_str)
      end

      Lita.register_handler(self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-onewheel-word-jumble-0.1.1 lib/lita/handlers/onewheel_word_jumble.rb