Skip to content

Instantly share code, notes, and snippets.

@nono
Created September 11, 2017 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nono/ba82fb3cac0a05dfa7e7a67ac8a69d11 to your computer and use it in GitHub Desktop.
Save nono/ba82fb3cac0a05dfa7e7a67ac8a69d11 to your computer and use it in GitHub Desktop.
Create fake contacts in CouchDB for Cozy v3
#!/usr/bin/env ruby
# gem install faker rest-client
# ./fake-contacts.rb 100
require 'faker'
require 'json'
require 'rest-client'
Faker::Config.locale = :fr
n = 1
n = ARGV.first.to_i if ARGV.any?
url = "http://localhost:5984/cozy-tools-8080%2Fio-cozy-contacts"
def gen_contact
contact = {}
# Name
first = Faker::Name.first_name
last = Faker::Name.last_name
full = "#{first} #{last}"
contact[:name] = {
firstName: first,
familyName: last
}
contact[:fullname] = full
# Emails
email = Faker::Internet.email [first, last, full].sample
contact[:email] = [{ address: email }]
if rand(2) == 0
perso = Faker::Internet.free_email [first, last, full].sample
contact[:email] << { address: perso, label: 'home' }
end
# Address
addr = {}
addr[:street] = Faker::Address.street_name if rand(2) == 0
addr[:city] = Faker::Address.city if rand(3) == 0
addr[:post_code] = Faker::Address.postcode if rand(4) == 0
addr[:country] = Faker::Address.country if rand(10) == 0
contact[:address] = [addr] unless addr.empty?
# Phone
if rand(3) == 0
phone = Faker::PhoneNumber.cell_phone
contact[:phone] = [{ number: phone }]
end
# Cozy
if rand(4) == 0
cozy = Faker::Internet.domain_name
elsif rand(3) == 0
cozy = "#{first.downcase}.mycozy.cloud"
else
return contact
end
contact[:cozy] = [{ url: cozy }]
contact
end
n.times do
contact = gen_contact
puts contact[:fullname]
RestClient.post url, contact.to_json, {content_type: :json, accept: :json}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment