JSON API » Sample clients » Ruby

#!/usr/bin/env ruby

# This is a sample client for the Enswitch JSON API.
# It is released into the public domain.
# It comes with absolutely no warranty, and is not supported.

require 'json'
require 'open-uri'

base = 'http://enswitch.example.com/api/json/'
function = 'access/list'
username = 'testuser'
password = 'testpass'

reply = open( base + function + '/?auth_username=' + username + ';auth_password=' + password )
output = JSON.parse( reply.read )
fatal = 0
output[ 'responses' ].each { |r|
	if r[ 'code' ].to_i >= 400
		$stderr.puts r[ 'message' ]
		fatal = 1
	end
}
if fatal == 1
	exit( 1 )
end

output[ 'data' ].each { |d|
	puts 'Remote access account id=' + d[ 'id' ] + ', name=' + d[ 'name' ]
}