Created
March 31, 2009 17:22
-
-
Save metavida/88282 to your computer and use it in GitHub Desktop.
How to update a User record via the Haiku LMS API, using Ruby.
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 'rubygems' | |
require 'oauth' | |
require 'xmlsimple' | |
# Create an OAuth Consumer with the appropriate API key and secret. | |
@consumer = OAuth::Consumer.new("api_key_EUNzNgVkrpFUFg", "api_secret_KBq6hTvjjvqAWOPz6c4WnQHN6jvkU8", { :site=>"https://my-domain.haikulearning.com" }) | |
# Create an OAuth Access Token using your personal key and secret. | |
@access_token = OAuth::AccessToken.new(@consumer, "token_959b3659b65ddbb6f8552c3401", "secret_40ec1d3617e7f58811c2f1e4416dcf2f7") | |
# Generate the corrent URL based on this hash | |
@params = { | |
"import_id"=>"0023992", | |
"first_name"=>"Susan", | |
"middle_name"=>"Newman", | |
"last_name"=>"Weisman", | |
"nickname"=>"Suzie", | |
"login"=>"susanw", | |
"password"=>"Suzie's Password", | |
"email"=>"[email protected]", | |
"user_type"=>"T", | |
"enabled"=>"true", | |
"display_id"=>"S239" | |
} | |
# In order to update a User resource we need to send a PUT request | |
@response = @access_token.put("/do/services/user/1", @params) | |
# Parse the XML response | |
@response = XmlSimple.xml_in(@response.body) | |
if @response['status'] == 'ok' | |
# If the response from the Haiku LMS API indicates success | |
# extract the first user record (only one should have been returned in this case) | |
# and display it. | |
@user = @response['users'].first['user'] | |
puts @user.inspect | |
# [{ | |
# "id"=>"1", | |
# "import_id"=>"0023992", | |
# "first_name"=>"Susan", | |
# "middle_name"=>"Newman", | |
# "last_name"=>"Weisman", | |
# "nickname"=>"Suzie", | |
# "login"=>"susanw", | |
# "email"=>"[email protected]", | |
# "user_type"=>"T", | |
# "organization_id"=>"1", | |
# "enabled"=>"true", | |
# "display_id"=>"S239" | |
# }] | |
else | |
# If there was an error, display the error message | |
@error = @response['error'].first | |
message = "API Error #{ @error['code'] }: #{ @error['description'] }" | |
message += "\n#{ @error['details'] }" if @error['details'] | |
raise message | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment