Strip contact data from strings

marcus • May 26th, 2007

we had to find a way to prevent users from publishing their contact information for our documenta accommodation project. here are a few regexp's that make you life easier (it's not 100% waterproof, but prevents most forms of cheating)
patterns = [
  /([\w\._%-]+[\s]*)(@|at|\[at\]|\(at\))([\s]*[\w\.-]+[\s]*\.[\s]*[a-zA-Z]{2,4})/i,
  /([http\:\/\/]*)([\s]*)([www\.]*)([\s]*)([\w\.-]+)([\s]*)\.([\s]*)(de|at|ch|com|org|net)/i,
  /[\d]{2,}/
]

result = orig
for p in patterns
  result = result.gsub(p, '')
end
the full script, including test:
#!/usr/bin/env ruby -w

#
# INPUT
#
orig = 
"""test@web.de
test2 @ web.de
test3 @ web . de
test4 AT web.de
test5(aT)betrug.de
test6 (at) betrug.de
test7[At]betrug.de
test8 [at] betrug.de
http://www.betrug.de
www.betrug.de
betrug.de
www.betrug.com
http://www.betrug.com
www . betrug . com
www. betrug.de
01702138958
01602138958 
0171 2138958
0561 1234567
+49 171 2138958
00491712138958
171 213895
"""

#
# REGEXP PATTERNS 
#
patterns = [
  /([\w\._%-]+[\s]*)(@|at|\[at\]|\(at\))([\s]*[\w\.-]+[\s]*\.[\s]*[a-zA-Z]{2,4})/i,
  /([http\:\/\/]*)([\s]*)([www\.]*)([\s]*)([\w\.-]+)([\s]*)\.([\s]*)(de|at|ch|com|org|net)/i,
  /[\d]{2,}/
]

result = orig
for p in patterns
  result = result.gsub(p, '')
end


#
# TEST
#
spacer = ""; 50.times do  spacer << "-" end
  
puts spacer
puts "ORIGINAL"
puts spacer

i=0
for line in orig
  puts "#{i}: #{line}"
  i=i+1
end  

puts spacer

i=0
for line in result
  puts "#{i}: #{line}"
  i=i+1
end

Easy templating in ruby

marcus • May 26th, 2007

ruby templating the easy way:
#!/usr/bin/ruby -w

require 'erb'

module MW
  module Utils
    module Template
      
      ENDL = "\n"
      
      #
      # compiles the given template
      #
      def self.compile(template, locals={})
        # create code for easyaccess locals
        local_code = "<% " << ENDL
        for key in locals.keys
          local_code << "#{key} = locals[:#{key}]" << ENDL
        end
        local_code << "%>" << ENDL
        template = local_code << template
  
  puts template
  
        # compile template
        erb = ERB.new(template, 0, "%<>")
        compiled = erb.result(binding)
        compiled.gsub(/\n$/,'') # chomp the trailing newline
      end
    end
  end
end


#
# test 1
#
t = "hi <%= firstname %> <%= lastname %>"
puts MW::Utils::Template.compile(t, :firstname=>"peter", :lastname=>"meier")
puts MW::Utils::Template.compile(t, :firstname=>"marcus", :lastname=>"wendt")

#
# test 2
#
class TestUser
  attr_accessor :first, :last
end

u = TestUser.new
u.first = "peter"
u.last = "meier"

t = "hallo <%= user.first %> <%= user.last %>"
puts MW::Utils::Template.compile(t, :user=>u)

random pronounceable password generator

marcus • May 26th, 2007

a quick & simple random pronounceable password generator for ruby.

def random_pronouncable_password(size = 6)
  consonants = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp
st th tr 0 1 2 3 4 5 6 7 8 9 0)
  vocals = %w(a e i o u y)
  
  alternate=true
  password=''
  
  (size * 2).times do
    # get a random vocal or consonant
    chunk = (alternate ? consonants[rand * consonants.size] : vocals[rand * vocals.size])
    alternate = !alternate
    # randomly swap its case & add to password
    chunk = chunk.swapcase if rand > 0.5
    password << chunk
  end
  
  password
end
3d 4-space abstract aesthetic system aesthetics algorithm alien ambient ambisonics animation architecture art artificial audio audio research black&white book caskets classic clicks & cuts code color computer-vision conceptual art consoles cpp culture ddr design devices digtial fabrication documenta documentation drawing dynamics electricity electromagnetism electronics environment event exhibition experimental exploration fashion festival film flocking folk food fractal furniture gamedev generative genetic geometry glitch graphic hacks haptics hardware history hyperspace ideas illustration images inspiration installation instrument intelligence interactive interieur japan java knowledge management landscape library life light liquid live london math micro minimal modernism monochrome motion motion graphics multiples music naming nature nervous ink networked networking opensource osx painting paper particles performance personal photography physics playful politics press print processing processing.org programming quotes recipes research retro romance ruby scripts sculpture SENDUNG.net shopping snippets social software sound space space exploration craft space exploration craft orbiter supercollider swiss systems technology theory theremin toys transformed travel tricks typography universe video visual vj water web2.0 xcode