Cisco Prime Infrastructure API
Prime Infrastructure API Documentation

# Scripting (Ruby)

When using Ruby, there are many options for what client to use. The one demonstrated below uses the rest-client gem.

    #!/usr/bin/env ruby
    #*************************************************
    # Copyright (c) 2012 Cisco Systems, Inc.
    # All rights reserved.
    #**************************************************

    require "rubygems"
    require 'rest_client'
    require 'json'

    $base_uri = 'https://ncs-xmp-build-2/webacs/api/v1/'
    $clients ='data/ClientDetails'
    $user, $pass = 'root', 'Public123'

    # wrap resource access
    class PagedResource
    	def initialize(url, extraQuery, user, password,ps)
    	   # query additions eg.. .sort=xx&ssdsd .. don't need encoding...
    	    @resource = RestClient::Resource.new url, :user => user, :password => password
    		@options =  {:accept => 'application/json' };
    		@extra = URI.encode(extraQuery);
    		@pagesize=ps;
    	end

    	def getPage(start)
    	     query = URI.encode('?.full=true&.firstResult='+ start.to_s+ '&.maxResults=' +@pagesize.to_s) + '&'+@extra;
    	     puts "Query: " + query;
    		 res = @resource[query].get @options
    		 if (res.code != 200)
    		 	raise "Bad response received:",res.to_str
    		 	exit
    		 end
    		last_res = JSON.parse(res)
    		return last_res;
    	end

    	def getComplete()
    		resList =[];
    		start = Time.new
    		partial = getPage(0);
    		count = partial['queryResponse']['@count'].to_i;
    		last = partial['queryResponse']['@last'].to_i;
    		if (count == 0)
    		  puts ("Read 0 records")
    		  return resList;
    		end
    		rumble resList, partial
    #		puts(JSON.pretty_generate(partial))

    		while last<count-1 do
    			partial = getPage(last+1);
    			last = partial['queryResponse']['@last'].to_i;
    			rumble resList, partial
    		end
    		finish = Time.new
    		puts ("Read "+resList.length.to_s+" records")
    		puts ('Took: '+(finish-start).to_i.round().to_s+' seconds')
    		return resList;
    	end

    	private

    	def rumble(resA,partial)
    			partial['queryResponse']['entity'].each do | ent|
    			   dto = ent['@dtoType']
    			   resA.push(ent[dto])
    			end
    	end
    end

    clientRes = PagedResource.new $base_uri + $clients,"",$user, $pass, 1000;

    clients = clientRes.getComplete();

    puts(JSON.pretty_generate(clients[0]))

For full documentation on the rest-client gem, please visit http://rubydoc.info/gems/rest-client/1.6.1