Skip to content

Commit

Permalink
Merge pull request #15 from fluidinfo/6-remove-jquery-reliance
Browse files Browse the repository at this point in the history
Merges branch 6-remove-jquery-reliance that fixes issue #6.
  • Loading branch information
ntoll committed Aug 2, 2011
2 parents d84833e + f3d88b3 commit f7f80a2
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 157 deletions.
3 changes: 1 addition & 2 deletions SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
<head>
<title>jquery-fluidinfo.js Test Runner</title>
<link rel="stylesheet" type="text/css" href="lib/jasmine-1.0.2/jasmine.css">
<script type="text/javascript" src="lib/jquery.min.js"></script>
<script type="text/javascript" src="lib/sinon-1.1.1.js"></script>
<script type="text/javascript" src="lib/jasmine-1.0.2/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-1.0.2/jasmine-html.js"></script>

<!-- include source files here... -->
<script type="text/javascript" src="jquery-fluidinfo.js"></script>
<script type="text/javascript" src="fluidinfo.js"></script>

<!-- include spec files here... -->
<script type="text/javascript" charset="utf-8" src="spec/fluidinfoSpec.js"></script>
Expand Down
138 changes: 100 additions & 38 deletions jquery-fluidinfo.js → fluidinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
* @author <a href="http://twitter.com/onigiri">onigiri</a>, <a href="http://twitter.com/ntoll">ntoll</a> & <a href="http://twitter.com/barshirtcliff">barshirtcliff</a>
* @version 0.1
* @constructor
* @param options {Object} Contains various parameters for the call.
* @param options {Object} Contains various parameters for the new session with
* Fluidinfo.
* <dl>
* <dt>username</dt>
* <dd>The username to use when authenticating with Fluidinfo.</dd>
* <dt>password</dt>
* <dd>The password to use when authenticating with Fluidinfo</dd>
* <dt>instance</dt>
* <dd>The instance to connect to. Either "main", "sandbox" or a bespoke
* instance. Defaults to "main".</dd>
* </dl>
* returns {Object} An object through which one interacts with Fluidinfo.
*/
fluidinfo = function(options) {
Expand Down Expand Up @@ -63,8 +73,10 @@ fluidinfo = function(options) {
}
}

// The object to eventually be returned
session = new Object();
/**
* Represents a session with Fluidinfo.
*/
var session = new Object();

if(options) {
if(options.instance) {
Expand All @@ -88,7 +100,6 @@ fluidinfo = function(options) {
}
}
}

if((options.username != undefined) && (options.password != undefined)) {
session.authorizationToken = Base64.encode(options.username + ":" + options.password);
}
Expand All @@ -99,16 +110,14 @@ fluidinfo = function(options) {
session.baseURL = "https://fluiddb.fluidinfo.com/";
}

var utils = new Object();

/**
* Magic voodoo used to identify an array (potentially a Fluidinfo set).
* Taken from page 61 of Doug Crockford's "Javascript: The Good Parts".
*
* @param value A value that might be an array.
* @return {boolean} An indication if the value is an array.
*/
utils.isArray = function(value) {
function isArray(value) {
return Object.prototype.toString.apply(value) === "[object Array]";
};

Expand All @@ -120,7 +129,7 @@ fluidinfo = function(options) {
* '/' values.
* @return {string} The appropriately encoded URL.
*/
utils.encodeURL = function(path) {
function encodeURL(path) {
var result = "";
for(i=0; i<path.length; i++) {
result += "/" + encodeURIComponent(path[i]);
Expand All @@ -137,7 +146,7 @@ fluidinfo = function(options) {
* @param value A value whose "type" needs identifying
* @return A boolean indication if the value is a Fluidinfo primitive type.
*/
utils.isPrimitive = function(value) {
function isPrimitive(value) {
// check the easy type matches first
var valueType = typeof(value);
var primitiveTypes = ["number", "string", "boolean"];
Expand All @@ -152,7 +161,7 @@ fluidinfo = function(options) {
}
// check for an array (potential set) and validate it only contains
// strings (currently multi-type arrays are not allowed)
if(utils.isArray(value)) {
if(isArray(value)) {
for(i=0; i<value.length; i++) {
memberType = typeof(value[i]);
if(memberType !== "string") {
Expand All @@ -174,7 +183,7 @@ fluidinfo = function(options) {
* @return the most appropriate MIME to use of null if not appropriate /
* required.
*/
utils.detectContentType = function(options) {
function detectContentType(options) {
// a "PUT" to objects/ or about/ endpoints means dealing with the MIME
// of a tag-value. If no MIME type is passed in the options objects
// then check if the value is a Fluidinfo primitive type. Otherwise
Expand All @@ -183,7 +192,7 @@ fluidinfo = function(options) {
if(options.type==="PUT" && (options.url.match("^objects\/") || options.url.match("^about\/"))) {
if(options.contentType){
result = options.contentType;
} else if (api.utils.isPrimitive(options.data)) {
} else if (isPrimitive(options.data)) {
result = "application/vnd.fluiddb.value+json";
} else {
throw { name: "ValueError", message: "Must supply Content-Type"};
Expand All @@ -195,43 +204,96 @@ fluidinfo = function(options) {
return result;
}

var api = new Object();

api.utils = utils;
/**
* Returns an appropriate XMLHttpRequest Object depending on the browser.
* Based upon code from here:
* http://www.quirksmode.org/js/xmlhttp.html
*/
function createXMLHTTPObject() {
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];
var xhr = false;
for(var i=0; i<XMLHttpFactories.length; i++) {
try {
xhr = XMLHttpFactories[i]();
} catch(e) {
continue;
}
break;
}
return xhr;
}

/**
* Makes an appropriate AJAX request to Fluidinfo
* Sends an appropriate XMLHTTPRequest based request to Fluidinfo.
* @param options {Object} An object containing the following named options:
* <dl>
* <dt>type</dt>
* <dd>The request's HTTP method. [GET, POST, PUT, DELETE or HEAD]</dd>
* <dt>url</dt>
* <dd>The full URL of the resource to which the request is made.</dd>
* <dt>data</dt>
* <dd>The body of data that may be the payload of the request (in the
* case of POST and PUT requests).</dd>
* <dt>async</dt>
* <dd>Indicates if the request is to be asyncronous (default is true)</dd>
* <dt>onSuccess</dt>
* <dd>A function that takes the XHR request as an argument. Called upon
* successful completion of the request.</dd>
* <dt>onError</dt>
* <dd>A function that takes the XHR request as an argument. Called when
* the request resulted in an error.</dd>
* </dl>
*
* @private
* @param options {Object} Contains various parameters for the call.
*/
api.ajax = function(options){
options.contentType = api.utils.detectContentType(options);
if(api.utils.isArray(options.url)) {
options.url = api.utils.encodeURL(options.url);
function sendRequest(options) {
if(isArray(options.url)) {
options.url = encodeURL(options.url);
}
options.url = session.baseURL+options.url;
options.async = options.async || true;
options.beforeSend = function(xhrObj){
if(session.authorizationToken != undefined){
xhrObj.setRequestHeader("Authorization","Basic " + session.authorizationToken);
};
if(options.contentType) {
xhrObj.setRequestHeader("Content-Type", options.contentType);
}
var method = options.type.toUpperCase() || "GET";
var url = session.baseURL+options.url;
var async = options.async || true;
var xhr = createXMLHTTPObject();
if(!xhr) return;
xhr.open(method, url, async);
if(session.authorizationToken != undefined){
xhr.setRequestHeader("Authorization", "Basic " + session.authorizationToken);
};
options.processData = false;
$.ajax(options);
var contentType = detectContentType(options);
if(contentType) {
xhr.setRequestHeader("Content-Type", contentType);
}
xhr.onreadystatechange = function() {
if(xhr.readyState != 4) return;
if(xhr.status < 300 || xhr.status == 304) {
if(options.onSuccess){
options.onSuccess(xhr);
}
} else if (options.onError){
// there appears to be a problem
options.onError(xhr);
}
}
xhr.send(options.data)
}

/**
* Contains functions to facilitate the calling of the Fluidinfo REST API.
*/
var api = new Object();

/**
* Makes an HTTP GET call to the Fluidinfo API
*
*/
api.get = function(options){
options.type = "GET";
options.data = null;
session.api.ajax(options);
sendRequest(options);
}

/**
Expand All @@ -240,7 +302,7 @@ fluidinfo = function(options) {
*/
api.post = function(options){
options.type = "POST";
session.api.ajax(options);
sendRequest(options);
}

/**
Expand All @@ -249,7 +311,7 @@ fluidinfo = function(options) {
*/
api.put = function(options){
options.type = "PUT";
session.api.ajax(options);
sendRequest(options);
}

/**
Expand All @@ -259,7 +321,7 @@ fluidinfo = function(options) {
api.delete = function(options){
options.type = "DELETE";
options.data = null;
session.api.ajax(options);
sendRequest(options);
}

/**
Expand All @@ -269,7 +331,7 @@ fluidinfo = function(options) {
api.head = function(options){
options.type = "HEAD";
options.data = null;
session.api.ajax(options);
sendRequest(options);
}

session.api = api;
Expand Down
18 changes: 0 additions & 18 deletions lib/jquery.min.js

This file was deleted.

Loading

0 comments on commit f7f80a2

Please sign in to comment.