node-jivesbs-rest
Version:
REST API and Feed Wrapper for Jive SBS 4.5.
353 lines (239 loc) • 8.55 kB
text/coffeescript
class JiveContainer
# Requre Username and Password
constructor: ()->
if !.username || !.password
throw new Error("HubContainer must have username and password in config object")
.url = .proto + .url
= require 'request'
= require 'xml2js'
getGroups: (cb)->
groupsUrl = "/rpc/rest/socialGroupService/socialGroups"
groupsFn = (err, response, body)=>
if !
return
if err
cb(err, null)
return
parseString = .parseString
parseString body, (err, result)->
if err
cb(err, null)
return
if !result['ns2:getSocialGroupsResponse']
cb("no valid response", null)
return
cb(err, result['ns2:getSocialGroupsResponse']['return'] || null)
return
.get(.url + groupsUrl, groupsFn).auth(.username, .password, true)
return @
getProjects: (cb)->
projectsUrl = "/rpc/rest/projectService/projects"
projectsFn = (err, response, body)=>
if !
return
if err
cb(err, null)
return
parseString = .parseString
parseString body, (err, result)->
if err
cb(err, null)
return
if !result['ns2:getProjectsResponse']
cb("no valid response", null)
return
cb(err, result['ns2:getProjectsResponse']['return'] || null)
return
.get(.url + projectsUrl, projectsFn).auth(.username, .password, true)
return @
getPersonalDocuments: (person, cb)->
if !person || !cb
console.log "getPersonalDocuments needs (person, cb)"
return
rssUrl = "/groups/feeds/documents?targetUser=#{person}&numItems=100000"
groupDocumentsFn = (err, response, body)=>
if err
cb(err, null)
return
if !
return
parseString = .parseString
parseString body, (err, result)->
if err
cb(err, null)
return
if !result.rss
cb("no valid response", null)
return
if !result.rss.channel[0].item || result.rss.channel[0].item.length <= 0
cb(null, [])
return
cb(null, result.rss.channel[0].item)
return
.get(.url + rssUrl, groupDocumentsFn).auth(.username, .password, true)
return @
getGroupDocuments: (groupId, cb)->
if !groupId || ! cb
cb("No group Id or callback provided for getting documents", null)
return @
rssUrl = "/community/feeds/documents?containerType=700&container=#{groupId}&numItems=10000"
groupDocumentsFn = (err, response, body)=>
if err
cb(err, null)
return
if !
return
parseString = .parseString
parseString body, (err, result)->
if err
cb(err, null)
return
if !result.rss
cb("no valid response", null)
return
if !result.rss.channel[0].item || result.rss.channel[0].item.length <= 0
cb(null, [])
return
cb(null, result.rss.channel[0].item)
return
.get(.url + rssUrl, groupDocumentsFn).auth(.username, .password, true)
return @
getProjectDocuments: (projectId, cb)->
if !projectId || ! cb
cb("No project Id or callback provided for getting documents", null)
return @
rssUrl = "/community/feeds/documents?containerType=600&container=#{projectId}&numItems=10000"
groupDocumentsFn = (err, response, body)=>
if err
cb(err, null)
return
if !
return
parseString = .parseString
parseString body, (err, result)->
if err
cb(err, null)
return
if !result.rss
cb("no valid response", null)
return
if !result.rss.channel[0].item || result.rss.channel[0].item.length <= 0
cb(null, [])
return
cb(null, result.rss.channel[0].item)
return
.get(.url + rssUrl, groupDocumentsFn).auth(.username, .password, true)
return @
getCommunities: (cb)->
communitiesUrl = "/rpc/rest/communityService/communities"
communitiesFn = (err, response, body)=>
if err
cb(err, null)
return
if !
return
parseString = .parseString
parseString body, (err, result)->
if err
cb(err, null)
return
if !result['ns2:getRecursiveCommunitiesResponse']
cb("no valid response", null)
return
cb(null, result['ns2:getRecursiveCommunitiesResponse']['return'])
return
.get(.url + communitiesUrl, communitiesFn).auth(.username, .password, true)
return @
getCommunityDocuments: (communityId, cb)->
if !communityId || ! cb
cb("No community ID or callback provided for getting documents", null)
return @
documentsUrl = "/rpc/rest/documentService/documentsByCommunity/" + communityId
self = @
documentsFn = (err, response, body)=>
if err
cb(err, null)
return
if !
return
if body.indexOf("javax.xml.bind.MarshalException") is -1
parser = new self.xml2js.Parser()
parser.parseString body, (err, result)->
if err
cb(err, null)
return
if !result['ns2:getDocumentsByCommunityResponse']
cb("no valid response", null)
return
if result['ns2:getDocumentsByCommunityResponse']['return'].length <= 0
cb(null, [])
return
cb(null, result['ns2:getDocumentsByCommunityResponse']['return'] || [])
return
else
cb(null, [])
return
.get(.url + documentsUrl, documentsFn).auth(.username, .password, true)
return @
getAttachments: (documentId, cb)->
if !documentId || !cb
cb("No documentId or callback provided", null)
return @
attachmentsUrl = "/rpc/rest/documentService/attachments/DOC-" + documentId
self = @
attachmentsFn = (err, response, body)=>
if err
cb(err, null)
return
if !
return
parser = new self.xml2js.Parser()
parser.parseString body, (err, result)->
if err
cb(err, null)
return
if !result['ns2:getAttachmentsByDocumentIDResponse']
cb("no valid response", null)
return
if !result['ns2:getAttachmentsByDocumentIDResponse']['return'] || result['ns2:getAttachmentsByDocumentIDResponse']['return'].length <= 0
cb(null, [])
return
cb(null, result['ns2:getAttachmentsByDocumentIDResponse']['return'])
return
.get(.url + attachmentsUrl, attachmentsFn).auth(.username, .password, true)
return @
getBinaryDocument: (documentId, cb)->
if !documentId || !cb
cb("No document ID or cb provided", null)
return
binaryUrl = "/rpc/rest/documentService/binaryDocument/DOC-" + documentId
self = @
binaryFn = (err, response, body)=>
if err
cb(err, null)
return
if !
return
parser = new self.xml2js.Parser()
parser.parseString body, (err, result)->
if err
cb(err, null)
return
if !result || !result['ns2:getBinaryDocumentContentResponse']
cb("no valid response", null)
return
cb(null, result['ns2:getBinaryDocumentContentResponse']['return'][0] || [])
return
try
.get(.url + binaryUrl, binaryFn).auth(.username, .password, true)
catch e
console.log "Request timed out!\n " + e
#try again
.get(.url + binaryUrl, binaryFn).auth(.username, .password, true)
return @
__checkResponseBody: (body, cb)->
if body.indexOf("Bad credentials") >= 0
cb(body, null)
return false
return true
module.exports = JiveContainer