Permalink
Browse files

Enabled and documented the REST api for the service.

  • Loading branch information...
1 parent 37081a6 commit c2cda9a3572f61bbf274f740351b0fe06f83b788 @tpryan committed Mar 31, 2011
Showing with 244 additions and 0 deletions.
  1. +8 −0 Application.cfc
  2. +115 −0 api/formatter.cfc
  3. +90 −0 api/index.cfm
  4. +31 −0 docs/api.html
View
@@ -23,4 +23,12 @@ component{
application.contentService = new Evangelists.services.contentService();
return true;
}
+
+ public boolean function onMissingTemplate(){
+ writeDump(url);
+ writeDump(request);
+ writeDump(cgi);
+
+ return true;
+ }
}
View
@@ -0,0 +1,115 @@
+component{
+
+ public function init(){
+ variables.lineBreak = createObject("java", "java.lang.System").getProperty("line.separator");
+ return This;
+ }
+
+ public string function formatEntriesAsXML (array entries){
+
+ var results = '<?xml version="1.0" encoding="iso-8859-1"?>' & linebreak;
+
+
+ results = results & "<entries>" & linebreak;
+
+ for (i=1;i <= ArrayLen(arguments.entries); i++){
+ var entry = entries[i];
+ results = results & " <entry>" & linebreak;
+ results = results & " <id>" & entry.getId() & "</id>" & linebreak;
+ results = results & " <guid>" & entry.getGuid() & "</guid>" & linebreak;
+ results = results & " <link>" & entry.getLink() & "</link>" & linebreak;
+ results = results & " <author>" & entry.getAuthor() & "</author>" & linebreak;
+ results = results & " <published>" & entry.getpublished() & "</published>" & linebreak;
+ results = results & " <source>" & entry.getSource() & "</source>" & linebreak;
+ results = results & " <title>" & entry.getTitle() & "</title>" & linebreak;
+ results = results & " <description>" & XMLFormat(entry.getdescription()) & "</description>" & linebreak;
+ results = results & " <tags>" & entry.getTags() & "</tags>" & linebreak;
+ results = results & " </entry>" & linebreak;
+
+ }
+ results = results & "</entries>";
+
+ return results;
+
+ }
+
+ public string function formatAuthorsAsXML (array authors){
+
+ var results = '<?xml version="1.0" encoding="iso-8859-1"?>' & linebreak;
+
+ results = results & "<authors>" & linebreak;
+
+ for (i=1;i <= ArrayLen(arguments.authors); i++){
+ var author = authors[i];
+ results = results & " <author>" & author & "</author>"& linebreak;
+ }
+ results = results & "</authors>";
+ return results;
+ }
+
+ public string function formatSourcesAsXML (array sources){
+
+ var results = '<?xml version="1.0" encoding="iso-8859-1"?>' & linebreak;
+
+ results = results & "<sources>" & linebreak;
+
+ for (i=1;i <= ArrayLen(arguments.sources); i++){
+ var source = sources[i];
+ results = results & " <source>" & source & "</source>"& linebreak;
+ }
+ results = results & "</sources>";
+ return results;
+ }
+
+
+
+ public struct function formatDates (string dateString){
+ var process = {};
+
+ if(listlen(arguments.dateString, "-") > 1){
+ process.start = getToken(arguments.dateString,1,"-");
+ process.end = getToken(arguments.dateString,2,"-");
+ }
+ else{
+ if(left(arguments.dateString, "1") == "-"){
+ process.start = "";
+ process.end = replace(arguments.dateString, "-", "", "Once");
+ }
+ else{
+ process.start = replace(arguments.dateString, "-", "", "Once");
+ process.end = "";
+ }
+ }
+
+
+ var results = {startDate="",endDate=""};
+
+ if (len(process.start)){
+ results.startDate = formatDate(process.start);
+ }
+ if (len(process.end)){
+ results.endDate = formatDate(process.end);
+ }
+
+ return results;
+ }
+
+ public date function formatDate (string dateString){
+ var year = Left(arguments.dateString,4);
+ var month = Mid(arguments.dateString,5,2);
+ var day = Right(arguments.dateString,2);
+ var results = CreateDate(year,month,day);
+
+ return results;
+ }
+
+
+
+ public string function formatCountAsXML(numeric count){
+ var results = '<?xml version="1.0" encoding="iso-8859-1"?>' & linebreak;
+ results = results & "<counts>" & linebreak;
+ results = results & " <count>" & count & "</count>"& linebreak;
+ results = results & "</counts>";
+ return results;
+ }
+}
View
@@ -0,0 +1,90 @@
+<cfsilent>
+<cfscript>
+ entryService = new entry();
+ formatter = new formatter();
+
+ methods = ["get","count","listAuthors","listSources"];
+ formats = ["xml","json"];
+ authors = entryService.listAuthors();
+ sources = entryService.listSources();
+
+ urlArguments = listToArray(cgi.path_info, "/");
+
+ for (i=1;i<=ArrayLen(urlArguments); i++){
+ arg = Trim(urlArguments[i]);
+
+ if (ArrayFindNoCase(authors,ListToArray(arg)[1])){
+ url.author = arg;
+ }
+
+ if (ArrayFindNoCase(sources,ListToArray(arg)[1])){
+ url.source = arg;
+ }
+
+ if (Find("-",arg)){
+ if ((left(arg, 1)) == "-" ||
+ (right(arg, 1) == "-") ||
+ (mid(arg,9,1) == "-" && len(arg) == 17)){
+ dates = formatter.formatDates(arg);
+ url.startDate=dates.startDate;
+ url.endDate=dates.endDate;
+ }
+ }
+ if (ArrayFindNoCase(formats,ListToArray(arg)[1])){
+ url.format = arg;
+ }
+
+ if (ArrayFindNoCase(methods,arg)){
+ url.method = arg;
+ }
+
+ if (Find("|",arg)){
+ url.offset = getToken(arg,1,"|");
+ url.maxresults = getToken(arg,2,"|");
+ }
+
+ if (Find('"',arg)){
+
+ url.q = mid(arg,2,len(arg)-2);
+
+ }
+ }
+
+
+ param name="url.method" type="string" default="get"; //done
+ param name="url.source" type="string" default=""; //done
+ param name="url.author" type="string" default=""; //done
+ param name="url.startDate" type="string" default=""; //done
+ param name="url.endDate" type="string" default=""; //done
+ param name="url.format" type="string" default="xml"; //done
+ param name="url.offset" type="string" default="#entryService.getoffset()#";//done
+ param name="url.maxresults" type="string" default="100";//done
+ param name="url.q" type="string" default="";//done
+
+ param name="url.orderby" type="string" default="#entryService.getorderby()#";
+
+ if (url.method == "get"){
+ results = entryService.get(url.source,url.author,url.startDate,url.endDate,url.q,url.offset,url.maxresults,url.orderby);
+ returnContent = formatter.formatEntriesAsXML(results);
+ }
+ else if(url.method == "count"){
+ results = entryService.count(url.source,url.author,url.startDate,url.endDate,url.q);
+ returnContent = formatter.formatCountAsXML(results);
+ }
+ else if(url.method == "listAuthors"){
+ results = entryService.listAuthors();
+ returnContent = formatter.formatAuthorsAsXML(results);
+ }
+ else if(url.method == "listSources"){
+ results = entryService.listSources();
+ returnContent = formatter.formatSourcesAsXML(results);
+ }
+
+ if (url.format == "json"){
+ returnContent = serializeJSON(results);
+ }
+
+
+</cfscript>
+
+</cfsilent><cfoutput>#returnContent#</cfoutput>
View
@@ -0,0 +1,31 @@
+<h1>Evangelist API Docs</h1>
+<h2>Getting Started</h2>
+<p>To use the API start with the base url:</p>
+<p>http://yourdomain.com/evangelists/api/index.cfm/ </p>
+<p>Then append modifiers to it</p>
+<p>First comes what you are looking for, or the method: </p>
+<p>get|count|listAuthors|listSources</p>
+<p>From there you add modifiers to filter your search:</p>
+
+
+<table>
+ <tr><th>Modifier</th><th>format</th><th>Default</th></tr>
+ <tr><td>author</td><td>a comma delimited list of authors</td><td>all</td></tr>
+ <tr><td>source</td><td>a comma delimited list of sources</td><td>all</td></tr>
+ <tr><td>daterange</td><td>a range of dates in the format yyyymmdd-yyyymmdd</td><td>all</td></tr>
+ <tr><td>format</td><td>xml or json</td><td>xml</td></tr>
+ <tr><td>offset and maxresults</td><td>Numbers in the format offset|maxresults</td><td>0|20</td></tr>
+ <tr><td>q</td><td>a search term in Quotes</td><td>None</td></tr>
+</table>
+<h2>Examples</h2>
+<p><strong>Get all records for terryr in json format</strong></p>
+<p>http://yourdomain.com/evangelists/api/index.cfm/get/terryr/json</p>
+<p><strong>Get a count of all records from vimeo for March 2011in xml</strong></p>
+<p>http://yourdomain.com/evangelists/api/index.cfm/get/vimeo/20110301-20110401</p>
+<p><strong>Get a list of all records since the end of March</strong></p>
+<p>http://yourdomain.com/evangelists/api/index.cfm/get/vimeo/20110331-</p>
+<p>Get a list of 5 r<strong>ecords for ptrani and rstewart from vimeo and youtube before march 2011 in json</strong></p>
+<p>http://yourdomain.com/evangelists/api/index.cfm/get/rstewart,ptrani/vimeo,youtube/-20110301/0|5/json</p>
+<p><strong>Get a list of 5 records for ptrani and rstewart from vimeo and youtube before march 2011 in json that are about &quot;blackberry&quot;</strong></p>
+<p>http://yourdomain.com/evangelists/api/index.cfm/get/rstewart,ptrani/vimeo,youtube/-20110301/0|5/&quot;blackberry&quot;/json</p>
+<p>&nbsp;</p>

0 comments on commit c2cda9a

Please sign in to comment.