Created
September 1, 2010 00:08
-
-
Save metavida/560007 to your computer and use it in GitHub Desktop.
How to "Ping" the Haiku LMS API, using Ruby.
This file contains hidden or 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 'net/http' | |
require 'net/https' | |
require 'xmlsimple' | |
# To Ping the server, we'll send a GET request. | |
@uri = URI.parse("https://my-domain.myhaikuclass.com") | |
@uri.path = "/do/services/test/ping" | |
@uri.query = "hello=world" | |
if @uri.is_a?(URI::HTTPS) | |
http = Net::HTTP.new(@uri.host, @uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
@response = http.request( | |
Net::HTTP::Get.new(@uri.request_uri) | |
) | |
else | |
@response = Net::HTTP.get_response(@uri) | |
end | |
# 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 pong and display it. | |
@pong = @response['pong'] | |
puts @pong.inspect | |
# [{ | |
# "hello"=>"world" | |
# }] | |
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