Sha256: cc89f58bc1aebfd2968c3f371a81b4efbc79b136f174c7cd1bd47dfc697802dd

Contents?: true

Size: 974 Bytes

Versions: 1

Compression:

Stored size: 974 Bytes

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...')

        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
          len = letters.length
        end

        puts "Searching for alternatives to #{letters} with a length of #{len}"

        combos = []

        letters.split(//).permutation(len).to_a.map(&:join).each do |combo|
          combos.push combo  if words[combo] == 1
        end

        combo_str = combos.sort.uniq.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.0.0 lib/lita/handlers/onewheel_word_jumble.rb