Sha256: 6bbdf52c5c2f66e106b8559b532a8d718272c0237ea45831af983a39780f2684

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Surveyor
  class Common
    RAND_CHARS = [('a'..'z'), ('A'..'Z'), (0..9)].map{|r| r.to_a}.flatten.to_s
    
    class << self
      def make_tiny_code(len = 10)
        if RUBY_VERSION < "1.8.7"
          (1..len).to_a.map{|i| RAND_CHARS[rand(RAND_CHARS.size), 1] }.to_s
        else
          len.times.map{|i| RAND_CHARS[rand(RAND_CHARS.size), 1] }.to_s
        end
      end

      def to_normalized_string(text)
        words_to_omit = %w(a be but has have in is it of on or the to when)
        col_text = text.gsub(/(<[^>]*>)|\n|\t/s, ' ') # Remove html tags
        col_text.downcase!                            # Remove capitalization
        col_text.gsub!(/\"|\'/, '')                   # Remove potential problem characters
        col_text.gsub!(/\(.*?\)/,'')                  # Remove text inside parens
        col_text.gsub!(/\W/, ' ')                     # Remove all other non-word characters      
        cols = (col_text.split(' ') - words_to_omit)
        (cols.size > 5 ? cols[-5..-1] : cols).join("_")
      end
      
      alias :normalize :to_normalized_string
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
surveyor-0.14.4 lib/surveyor/common.rb