Ruby Sample Code Users
This demonstrates how to create, read, update and delete users.
You will need an authorization token. To get a token, follow the instructions in the API overview.
# =====================================================================
#
# Sample code to Create and Delete users using the Bridge API
#
# =====================================================================
require 'rest_client'
require 'json'
# Global path to Bridge URL. Your URL will be different.
$bridge_url = "http://bridge.local.bridgeops.sh:3000"
$auth_token = "your authorization token"
# As an admin, create some users
# Note the bridge API only supports creating one user at a time.
# It returns an array of hashes. One hash in the array for each
# user successfully created.
def create_users(token)
puts "Create 3 users"
url = "#{$bridge_url}/api/admin/users"
users = []
users << {
"uid" => "george.washington@usa.gov", # unique id
"first_name" => "George",
"last_name" => "Washington",
"email" => "george.washington@usa.gov"
}
users << {
"uid" => "john.adams@usa.gov", # unique id
"first_name" => "John",
"last_name" => "Adams",
"email" => "john.adams@usa.gov"
}
users << {
"uid" => "thomas.jefferson@usa.gov", # unique id
"first_name" => "Thomas",
"last_name" => "Jefferson",
"email" => "thomas.jefferson@usa.gov"
}
user_ids = [] # for the created users
users.each do |user|
payload = { 'users' => [user] }
headers = {AUTHORIZATION: "Basic #{token}", 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
response = RestClient.post(url, payload.to_json, headers)
json_response = JSON.parse(response)
json_response["users"].each do |user|
user_ids << { id: user["id"], name: user["name"] }
puts " created: #{user['name']} with id #{user['id']}"
end
end
user_ids
end
# Delete users given an array of user data
# each item in the array should be a hash with two items, like this:
# {id: <id>, name: <name> }
def delete_users(token, ids)
puts "Deleting users"
headers = {AUTHORIZATION: "Basic #{token}" }
ids.each do |pair|
url = "#{$bridge_url}/api/admin/users/#{pair[:id]}"
response = RestClient.delete(url, headers)
if response.code == 204
puts " deleted: #{pair[:id]}: #{pair[:name]}"
end
end
end
# As an author list all users.
# Note that this uses an API from authors, not admins.
def list_all_users(token, print: true)
puts "List all users"
url = "#{$bridge_url}/api/author/users"
headers = {AUTHORIZATION: "Basic #{token}" }
response = RestClient.get(url, headers)
json_response = JSON.parse(response)
puts " Found #{json_response["users"].count} users" if print
json_response["users"].each do |user|
puts " #{user['name']} - roles: #{user['roles']}"
end
end
# Get's information about each user given a user ID
def list_each_user(token, user_ids)
puts "Listing each user:"
user_ids.each do |pair|
url = "#{$bridge_url}/api/author/users/#{pair[:id]}"
headers = {AUTHORIZATION: "Basic #{token}" }
response = RestClient.get(url, headers)
json_response = JSON.parse(response)
json_response["users"].each do |user|
puts " #{user['name']} - roles: #{user['roles']}, email: #{user['email']}"
end
end
end
def update_user(token, id)
puts "Modifying a user"
url = "#{$bridge_url}/api/author/users/#{id}"
payload = { 'user' => {
"email" => "james.madison@usa.gov",
"first_name" => "James",
"last_name" => "Madison"
}
}
headers = {AUTHORIZATION: "Basic #{token}" }
response = RestClient.put(url, payload, headers)
puts response.description
end
# Start execution
user_ids = create_users($auth_token)
list_each_user($auth_token, user_ids)
update_user($auth_token, user_ids.first[:id])
list_each_user($auth_token, user_ids)
delete_users($auth_token, user_ids)
list_all_users($auth_token)