Created
December 1, 2016 04:55
-
-
Save beaglebot/cda7d2bb1dfe64b55578b049e23af7f1 to your computer and use it in GitHub Desktop.
Generates users and orgs for the senior infrastructure engineer coding challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'faraday' | |
gem 'ffaker' | |
gem 'byebug' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GEM | |
remote: https://rubygems.org/ | |
specs: | |
byebug (9.0.6) | |
faraday (0.10.0) | |
multipart-post (>= 1.2, < 3) | |
ffaker (2.1.0) | |
multipart-post (2.0.0) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
byebug | |
faraday | |
ffaker | |
BUNDLED WITH | |
1.13.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faraday' | |
require 'json' | |
require_relative './generator' | |
DOMAIN = 'z3n-tianhao-coding-test' | |
USERNAME = 'bgalvin@zendesk.com' | |
API_TOKEN = 'IJ3vYs6FSGRckDs3IrbVgyabX3hbkypG0HXMDYmZ' | |
NUM_USERS = 500 | |
NUM_ORGS = 100 | |
generator = SimZen::Generator.new | |
organizations = [] | |
def build_user(generator, organizations) | |
{ | |
name: generator.name, | |
email: generator.email, | |
timezone: generator.time_zone, | |
tags: generator.user_tags, | |
phone: generator.phone, | |
organization_id: rand(2) == 1 ? organizations.sample : nil, | |
role: "end-user", | |
verified: true | |
} | |
end | |
def build_organization(generator) | |
{ | |
name: generator.company_name, | |
tags: generator.organization_tags | |
} | |
end | |
conn = Faraday.new(url: "https://#{DOMAIN}.zendesk.com/") | |
conn.basic_auth("#{USERNAME}/token", API_TOKEN) | |
NUM_ORGS.times do | |
begin | |
response = conn.post('/api/v2/organizations.json') do |req| | |
req.headers['Content-Type'] = 'application/json' | |
req.body = JSON.dump({ | |
organization: build_organization(generator) | |
}) | |
end | |
if response.status == 201 | |
puts response.body | |
organizations << JSON.parse(response.body)['organization']['id'] | |
else | |
puts "FAILURE: " + response.body | |
end | |
end | |
end | |
puts organizations | |
NUM_USERS.times do | |
begin | |
response = conn.post('/api/v2/users.json') do |req| | |
req.headers['Content-Type'] = 'application/json' | |
req.body = JSON.dump({ | |
user: build_user(generator, organizations) | |
}) | |
end | |
if response.status == 201 | |
puts response.body | |
else | |
puts "FAILURE: " + response.body | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffaker' | |
module SimZen | |
class Generator | |
def name | |
FFaker::Name.name | |
end | |
def company_name | |
FFaker::Company.name | |
end | |
def org_name | |
FFaker::Lorem.words(3).join(' ').capitalize | |
end | |
def domain_name | |
FFaker::Internet.domain_name | |
end | |
def email | |
FFaker::Internet.safe_email | |
end | |
def phone | |
FFaker::PhoneNumber.short_phone_number | |
end | |
def sentence | |
tags.join(' ').capitalize | |
end | |
def phrase | |
FFaker::Lorem.phrase | |
end | |
def tags(amt = 4) | |
FFaker::Lorem.words(amt) | |
end | |
def word(gen = :lorem) | |
"FFaker::#{gen.to_s.camelize}".constantize.word | |
end | |
USER_TAGS = %w(vip cto cio) | |
def user_tags | |
USER_TAGS.sample(rand(2)) | |
end | |
ORG_TAGS = %w(trial enterprise churned) | |
def organization_tags | |
ORG_TAGS.sample(rand(2)) | |
end | |
def description | |
FFaker::Lorem.paragraphs(4).join("\n\n") | |
end | |
alias_method :body, :description | |
def time_zone | |
nil | |
end | |
def date(options = {}) | |
starting_at = options[:starting_at] || simulator.epoch | |
ending_at = options[:ending_at] || Time.now | |
starting_at + rand(ending_at - starting_at) | |
end | |
def chance(prob = 0.5) | |
rand < prob | |
end | |
def weighted(weights) | |
target = rand(weights.values.sum) | |
weights.reduce(0) do |count, (option, weight)| | |
count += weight | |
if target < count | |
return option == :none ? nil : option | |
end | |
count | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment