Sha256: bf5f455040f2a14293b0ece585d279da0aa3174b0759272a3b5f8011e00669c2

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'securerandom'
require 'uuidtools'

module Surveyor
  class Common
    OPERATORS = %w(== != < > <= >= =~)

    class << self
      if SecureRandom.respond_to?(:urlsafe_base64)
        def make_tiny_code
          # 7 random bytes is increased to ~10 characters (sans padding) by
          # base64 encoding
          SecureRandom.urlsafe_base64(7)
        end
      else
        def make_tiny_code
          s = [SecureRandom.random_bytes(7)].pack("m*")
          s.delete!("\n")
          s.tr!("+/", "-_")
          s.delete!("=")
        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/su, ' ') # Remove html tags
        col_text.downcase!                            # Remove capitalization
        col_text.gsub!(/\"|\'/u, '')                   # Remove potential problem characters
        col_text.gsub!(/\(.*?\)/u,'')                  # Remove text inside parens
        col_text.gsub!(/\W/u, ' ')                     # 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

      def generate_api_id
        UUIDTools::UUID.random_create.to_s
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
surveyor-1.1.0 lib/surveyor/common.rb
surveyor-1.0.1 lib/surveyor/common.rb
surveyor-1.0.0 lib/surveyor/common.rb