Skip to content

Instantly share code, notes, and snippets.

@lwe
Created August 11, 2009 11:26
Show Gist options
  • Save lwe/165751 to your computer and use it in GitHub Desktop.
Save lwe/165751 to your computer and use it in GitHub Desktop.
Swiss-ify random data generated by faker
# make faker a bit more swiss-friendly :)
module Faker
class Company
# Add swiss suffixes like AG, GmbH etc.
def self.suffix
%w(AG GmbH und\ Söhne und\ Partner &\ Co. Gruppe LLC Inc.).rand
end
end
class Internet
# Add extensions like ch, de, li and at
def self.domain_suffix
%w(com net biz ch de li at ch ch).rand
end
end
class Address
# Swiss ZIP codes are like 5000 or 3000 etc...
def self.ch_zipcode
"#{rand(9)+1}#{Faker.numerify('###')}"
end
# just some country codes...
def self.country_code
%w(CH CH CH DE AT US LI US HK VN).rand
end
end
class PhoneNumber
COMPANY_FORMATS = ['0800 ### ###', '0800 ## ## ##', '0## ### ## ##', '0## ### ## ##', '+41 ## ### ## ##', '0900 ### ###']
SIMPLE_FORMATS = ['0## ### ## ##', '+41## ### ## ##']
MOBILE_FORMATS = ['07M ### ## ##', '+417M ### ## ##', '0041 7M ### ## ##']
# Allows creation of different phone numbers
def self.ch_phone_number(kind = :simple)
Faker.numerify const_get("#{kind.to_s.upcase}_FORMATS").rand.gsub('M', "#{rand(4)+6}")
end
end
end
@felixrabe
Copy link

In what context does this code work? In Ruby 1.9.3, I get:

NoMethodError: private method `rand' called for ["0## ### ## ##", "+41## ### ## ##"]:Array

So [].rand does not seem to work.

@lwe
Copy link
Author

lwe commented May 14, 2012

In 1.9.3 there's Array#sample, though this code is pretty old, so no idea if it still works as expected, good luck :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment