Skip to content

Instantly share code, notes, and snippets.

@tswicegood
Created August 8, 2009 03:33
Show Gist options
  • Save tswicegood/164288 to your computer and use it in GitHub Desktop.
Save tswicegood/164288 to your computer and use it in GitHub Desktop.
dojo.provide('d51.data.TwitterReadStore');
dojo.require("dojo.data.util.simpleFetch");
dojo.require("dojo.io.script");
dojo.require('dijit._Widget');
dojo.require('dojox.encoding.base64');
dojo.declare('d51.data.TwitterReadStore',
[dijit._Widget], {
method: "",
username: "",
password: "",
_storeRef: "_S",
constructor: function(args) {
// here for kicks
},
_canAuthenticate: function() {
return this.username != '' && this.password != '';
},
_generateEncodedUsernameAndPassword: function() {
if (!this._canAuthenticate()) {
return '';
}
var userAndPass = this.username + ':' + this.password;
chars = [];
dojo.forEach(userAndPass, function(char, idx) {
chars[idx] = char.charCodeAt(0);
});
return dojox.encoding.base64.encode(chars);
},
getFeatures: function() {
return {
'dojo.data.api.Read': true
};
},
getAttributes: function() {
return [];
},
close: function() {
},
isItem: function(item) {
return item && item[this._storeRef] === this;
},
isItemLoaded: function(item) {
return this.isItem(item);
},
loadItem: function(keywordArgs) {
},
_processData: function(data) {
var storeObject = this;
dojo.forEach(data, function(item) {
item[storeObject._storeRef] = storeObject;
});
return data;
},
_fetchItems: function(request, fetchHandler, errorHandler) {
var self = this;
xhrGetArgs = {
url: "https://twitter.com/" + this.method,
handle: function(data) {
fetchHandler(self._processData(data), request);
},
handleAs:'json',
error: function(error) {
errorHandler(error, request);
}
};
console.log(xhrGetArgs);
if (this._canAuthenticate()) {
console.log("I can authorize!");
xhrGetArgs['headers'] = {
"Authorization": "Basic " + this._generateEncodedUsernameAndPassword()
};
console.log(xhrGetArgs);
}
dojo.xhrGet(xhrGetArgs);
},
getValues: function(item, attribute) {
if (!this.isItem(item)) {
throw new Error("TwitterReadStore: invalid item")
}
if (typeof attribute !== 'string') {
throw new Error('TwitterReadStore: invalid attribute')
}
try {
return [item[attribute]];
} catch (e) {
return [];
}
},
getValue: function(item, attribute) {
var values = this.getValues(item, attribute);
return values.length == 0 ? undefined : values[0];
},
hasAttribute: function(item, attribute) {
return this.getValues(item, attribute) > 0;
},
containsValue: function(item, attribute, value) {
var values = this.getValues(item, attribute);
return dojo.some(values, function(thisValue) {
return thisValue == value;
});
}
});
dojo.extend(
d51.data.TwitterReadStore,
dojo.data.util.simpleFetch
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment