Sha256: 7f4cb70e3f7a958ce4c42cb2cc4cdf16d5d035353057b7da3843ab8a454d61ee

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module Surveyor
  class Common
    RAND_CHARS = [('a'..'z'), ('A'..'Z'), (0..9)].map{|r| r.to_a}.flatten.to_s
    OPERATORS = %w(== != < > <= >= =~)
    
    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.to_s.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

3 entries across 3 versions & 1 rubygems

Version Path
surveyor-0.16.1 lib/surveyor/common.rb
surveyor-0.16.0 lib/surveyor/common.rb
surveyor-0.15.0 lib/surveyor/common.rb