site logo

Example: User creation (Ruby)


Example: user creation (Ruby)  

#!/usr/bin/ruby
########################################
# $Id: create_users.rb,v 1.3 2008/01/13 20:54:44 d Exp $
#
# This is an example how to use the Mojo Helpdesk API
# Language: ruby
#
# This script shows how to:
#  - create users
#
# You will need to adjust the URL and MY_KEY values:
#
# URL - Base URL to your helpdesk.
#
# MY_KEY - Your access key, as displayed on your "profile" page
#
########################################
# Feel free to use this code and modify to fit your needs.
# More information visit www.mojohelpdesk.com
########################################
require 'soap/wsdlDriver'
require 'open-uri'

########################################
# Your helpdesk URL
# URL = "http://supportxyz.mojohelpdeskcom
# MY_KEY = "8b1459cfcb367d72419e2642e56fa3b72bc5c2a0"
#
# Replace the info below with your own:
#
# URL = "http://yourhd.mojohelpdesk.com"
# MY_KEY = "find-your-key-in-your-user-profile"
URL = "http://myhelpdesk.mojohelpdesk.com"
MY_KEY = "asdf87asd9f71asdf98sda7f9ad8sf7"
#
#########################################

begin
  rpc = SOAP::WSDLDriverFactory.new("#{URL}/backend/service.wsdl").create_rpc_driver
rescue
  printf "Fatal error: Could not connect to server.wsdl or an error occured (%s). Check your URL and try again.\n", $!
  exit(-1)
end

#
# User params
#
params = {
  'company_id' => "newco",
  'email' => "jean@mail#{rand(99999999)}.com",
  # using a random email address allows to run the script several time w/o running into the error of 'email should be unique'.
  'first_name' => 'Jean',
  'last_name' => 'Vacance',
  'display_name' => 'Big John',
  'work_phone' => '555 666 5454',
  'cell_phone' => '111 666 5454',
  'home_phone' => '222 666 5454',
  'password' => '123456789',
  'user_notes' => 'some notes here',
  'send_welcome_email' => 'false'
}

#
# Send the request and get the result
#
result = rpc.CreateUser("#{MY_KEY}", params)

if result['successful'] != true
  printf ">>> Error: %s\n", result['error_message']
  exit(-1)
end


u = result.user
printf ">>> User created, id: %d, email %s\n", u['id'], u['email']
exit(0)