
Cisco Evolved Programmable Network Manager API
Evolved Programmable Network Manager API Documentation

# Using 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

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

    $base_uri = 'https://my_server/webacs/api/v4'
    $user, $pass = 'user', 'password'
    $clients ='/data/ClientDetails'

    # wrap resource access
    class PagedResource
    	def initialize(url, extraQuery, user, password,ps)
    		@resource = RestClient::Resource.new url, :user => user, :password => password, :verify_ssl => false
    		@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

    		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]))