Contents
JavaScript Sample: Fetching a Listing of ExpertsJavaScript: Fetching an Expert's ReviewsJavaScript: Fetching an Expert's Full ProfileRuby Sample: Fetching a Listing of ExpertsJavaScript Sample: Fetching a Listing of Experts
This sample code uses jQuery to fetch 10 experts from a given category and display them on the page. It is meant as a guide on the API and is not production-ready.
//******************
// Kasamba profile listing API Endpoint example
// Uses API Version 1.1
// No support or warranty is offered or implied for this snippet.
//*******************
//updates the experts displayed to the given category
//categories provided are converted into the categoryid used in the ajax request
function changeExperts(categoryID){
$("#expertgrid").empty(); // I have a div element with id "expertgrid" that I'm using to hold my expert information
//build GET url and fetch the results
var url = '//apis.kassrv.com/experts.svc/1.1/categories/'+categoryID+'/jsonp?sort=9&count=10&callback=api_expertProfiles';
$.ajax({
type:'GET',
url: url,
dataType: 'script' });
});
}
//callback function from retrieving a listing of expert profiles
//obj is returned json data
function api_expertProfiles(obj){
// cycle through experts and create their html
for(var i=0;i< obj.experts.length;i++){
var catID = obj.category.id;
var expertID = obj.experts[i].id;
var expertThumbnail = obj.experts[i].thumbnail;
var expertName = obj.experts[i].name;
var expertAverageRating = Math.round(obj.experts[i].averageRating);
var expertReviews = obj.experts[i].reviews;
var expertReviewsCount = obj.experts[i].reviewsCount;
var expertFee = obj.experts[i].fee;
var expertDetailedDescription = obj.experts[i].description;
var expertContactLive = obj.experts[i].contactLive;
var expertStatus = obj.experts[i].status;
var expertContactOffline = obj.experts[i].contactOffline;
var contactLink = expertContactOffline;
if(expertStatus == 1 || expertStatus == 4) // these statuses are 'available'
{ contactLink = expertContactLive; }
//start the expert profile HTML
var expertString = "< div id='"+GRIDNAME+"_Expert_"+i+"' >"+
"< div class='grid' >"+
"< div class='profile-info' >"+
"< div class='profile-photo' >"+
"< a href=\"javascript:OpenWindow('https://www.liveperson.com/Professional/expert-profile.aspx?BanID="+BANNERID+"&CatID="+catID+"&ExpID="+expertID+"','970','800') \" >< img class='photo' src='"+expertThumbnail+"' title='"+expertName+"' alt='"+expertName+"' / >< /a >"+
"< /div >"+
"< span class='rating' >< img src='//marketing-kasamba.kassrv.com/ac/liveperson-partners/shared/graphics/rate"+expertAverageRating+".png' / >< /span >"+
"< cite class='reviews' >< a href=\"javascript:OpenWindow('https://www.liveperson.com/rating/expert-rating.aspx?BanID="+BANNERID+"&i_MemID="+expertID+"','450','700')\" >"+expertReviewsCount+" reviews< /a >< /cite >"+
"< cite class='perminute' >$ "+expertFee+"/min< /cite >"+
"< /div >"+
"< div class='profile-desc' >"+
"< h2 >< a id='profile_url' href=\"javascript:OpenWindow('https://www.liveperson.com/Professional/expert-profile.aspx?BanID="+BANNERID+"&CatID="+catID+"&ExpID="+expertID+"','970','800')\" class='expname' >"+expertName+"< /a >< /h2 >"+
"< p class='description' >"+expertDetailedDescription+"< /p >"+
"< a href=\"javascript:OpenWindow('"+contactLink+"45123','680','700')\" title='Contact Live' class='contact-link' >"+expertStatus+"< /a >"+
"< /div >"+
"< /div >"+
"< /div >";
//write the expert's profile to the div container
$("#expertgrid").append(expertString);
}
// clean up profile descriptions
// set statuses
// remove html from brief descriptions
// trim desctions
}
JavaScript: Fetching an Expert's Reviews
This sample code uses jQuery to fetch all the reviews for a specific expert It is meant as a guide on the API and is not production-ready.
//******************
// Kasamba expert reviews
// Uses API Version 1.1
// No support or warranty is offered or implied for this snippet.
//*******************
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
//Get and populate an experts reviews
function getExpertReviews(expertid){
//cursory validation
if(expertid == 0) {
profilestring = 'No Expert Found with that ID.';
$(profileDiv).html(profilestring);
return;
}
if(categoryid == 0){
profilestring = 'No Category Found with that ID.';
$(profileDiv).html(profilestring);
return;
}
var url = '//apis.kassrv.com/reviews.svc/1.1/'+expertid+'/jsonp?callback=api_expertProfiles';
$.ajax({
type:'GET',
url: url,
dataType: 'script'
});
}
//callback for expert profile info
function api_expertProfiles(data){
var profilestring = '';
if(data.id != undefined){
profilestring += data.id+'< br / >' +
data.name +'< br / >' +
data.reviewsCount +'< br / >' +
data.averageRating +'< br / >' +
'REVIEWS:'+'< br / >';
$.each(data.reviews, function(i, review){
profilestring += review.clientName+'< br / >'+
review.date+'< br / >'+
review.rating+'< br / >'+
review.comment+'< br / >';
})
$(profileDiv).html(profilestring);
}else{
profilestring = 'Expert not found in that category or there was a mysterious error.';
$(profileDiv).html(profilestring);
}
}
$().ready(function(){
//get expertID from url
URLvars = getUrlVars();
expertid = 0;
categoryid = 0;
profileDiv = '#expertProfile'
if(undefined != URLvars['expertid'])
{ expertid=URLvars['expertid']; }
if(undefined != URLvars['categoryid'])
{ categoryid=URLvars['categoryid']}
alert('fetching expertid : '+expertid+'in categoryid: '+categoryid);
//get expert info via ajax
getExpertReviews(expertid, categoryid);
})
JavaScript: Fetching an Expert's Full Profile
This sample code uses jQuery to fetch 10 experts from a given category and display them on the page. It is meant as a guide on the API and is not production-ready.
//******************
// Kasamba expert full profile
// Uses API Version 1.1
// No support or warranty is offered or implied for this snippet.
//*******************
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function getExpertProfileInfo(expertid){
//cursory validation
if(expertid == 0) {
profilestring = 'No Expert Found with that ID.';
$(profileDiv).html(profilestring);
return;
}
if(categoryid == 0){
profilestring = 'No Category Found with that ID.';
$(profileDiv).html(profilestring);
return;
}
var url = '//apis.kassrv.com/experts.svc/1.1/'+expertid+'/category/'+categoryid+'/jsonp?callback=api_expertProfiles';
$.ajax({
type:'GET',
url: url,
dataType: 'script'
});
}
//callback for expert profile info
function api_expertProfiles(data){
var profilestring = '';
if(data.id != undefined){
profilestring += data.id+'< br / >' +
data.name +'< br / >' +
data.title +'< br / >' +
data.description +'< br / >' +
data.status+'< br / >' +
data.fee+'< br / >' +
data.contactLive+'< br / >' +
data.detailedDescription+'< br / >' +
data.picture+'< br / >';
$(profileDiv).html(profilestring);
}else{
profilestring = 'Expert not found in that category or there was a mysterious error.';
$(profileDiv).html(profilestring);
}
}
$().ready(function(){
//get expertID from url
URLvars = getUrlVars();
expertid = 0;
categoryid = 0;
profileDiv = '#expertProfile'
if(undefined != URLvars['expertid'])
{ expertid=URLvars['expertid']; }
if(undefined != URLvars['categoryid'])
{ categoryid=URLvars['categoryid']}
alert('fetching expertid : '+expertid+'in categoryid: '+categoryid);
//get expert info via ajax
getExpertProfileInfo(expertid, categoryid);
});
Ruby Sample: Fetching a Listing of Experts
This snippet of code fetches 15 experts from the "computers and programming" category using bannerid of 34. Please update the code as necessary when putting into production (e.g. bannerid).
#***************
# Kasamba profiles API Example
# Support API version 1.1 and earlier
# No support or warranty is offered or implied for this snippet.
#***************
require 'json'
require 'net/http'
def getExperts(category, format, args)
unless(format == 'json' || format == 'xml')
raise "Format invalid: must be 'json' or 'xml'"
end
unless(category.is_a?(Fixnum))
raise "Category invalid: must be a number"
end
category = category.to_s #we need it as a string for later
#Setup url and arguments substutitions
getProfilesURLTEMPLATE = "//apis.kassrv.com/experts.svc/APIVERSION/categories/CATEGORYID/FORMAT?"
apiVersionSwap = 'APIVERSION'
categoryIDSwap = 'CATEGORYID'
formatSwap = 'FORMAT'
params = {}
version = "1.1"
# build the request
paramArray = []
args.each { | key , value | paramArray << key+'='+value }
paramString = paramArray.join('&')
getProfileURL = getProfilesURLTEMPLATE.gsub(apiVersionSwap, version).gsub(categoryIDSwap, category).gsub(formatSwap, format)
getProfileURL = getProfileURL + paramString
puts "Requesting: #{getProfileURL} \n" #dump the request url to the screen
# make the request to fetch experts
url = URI.parse(getProfileURL)
response = Net::HTTP.get_response(url)
#check if we got a 200 success message or an error
case response
when Net::HTTPSuccess, Net::HTTPRedirection # OK
return response.body
else
raise response.error! # bubble up errors
end
end
begin
# 70 is the category ID for "computers and programming"
jsonData = getExperts(70, 'json', { "count"=>"15", "banner"=>"34" } ) # fetch 15 experts w/banner 34
rescue
print "There was a problem fetching the experts: ", $!
end
computerExperts = JSON.parse(jsonData)
experts = computerExperts['experts']
puts "Received #{experts.size} experts."
experts.each do |expert| # print out each expert ID and Name
puts expert['id'].to_s + " " + expert['name'].to_s
end