Skip to content

Instantly share code, notes, and snippets.

View couchoud's full-sized avatar

Chris Couchoud couchoud

View GitHub Profile
@couchoud
couchoud / supplant.js
Created September 11, 2009 15:01
basic hs templating using String object and Regex
if (typeof String.prototype.supplant !== 'function') {
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' ? r : a;
});
};
}
find . -regex '.*.svn' -exec rm -rf "{}" \;
+ (CGRect) aspectFittedRect:(CGRect)inRect max:(CGRect)maxRect
{
float originalAspectRatio = inRect.size.width / inRect.size.height;
float maxAspectRatio = maxRect.size.width / maxRect.size.height;
CGRect newRect = maxRect;
if (originalAspectRatio > maxAspectRatio) { // scale by width
newRect.size.height = maxRect.size.height * inRect.size.height / inRect.size.width;
newRect.origin.y += (maxRect.size.height - newRect.size.height)/2.0;
} else {
@couchoud
couchoud / gist:536884
Created August 19, 2010 02:58
post 4 - cs1
// A generic User object
user = {
id : 1,
firstName : "John",
lastName : "Doe",
email : "john.doe@fooey.com"
}
// REST api endpoint
@couchoud
couchoud / gist:537851
Created August 19, 2010 13:20
post_4 - cs2
/*!
* Javascript Pattern for Rest Service API’s
* http://www.chriscouchoud.com/
*
* Dual licensed under the MIT and GPL licenses.
*/
service = {
baseURL : "http://www.yourrestapi.com",
@couchoud
couchoud / gist:540917
Created August 20, 2010 18:57
post_4 - cs3
serviceDefinitions = {
"getUser" : {
"url" : "/users/{id}"
"type" : "GET",
"dataType" : "json",
"contentType" : ""
},
"updateUser" : {
@couchoud
couchoud / gist:540975
Created August 20, 2010 19:37
post_4 - cs4
// Your call to update a user could look a little like this:
service.create(serviceDefinitions["updateUser"], user, {data:{first_name:"Jane", last_name:"Doe"}, success: someGlobalSuccessMethod})
// Your call to get a recipe could look like this:
service.create(serviceDefinitions["getRecipe"], recipe, {success: someGlobalSuccessMethod});
// Some other call that involves some processing
var resource = $.extend({},user);
resource["fullName"] = function() {return this.firstName + this.lastName};
service.create(serviceDefinitions["getUserByFullName"], resource, {success: someGlobalSuccessMethod});
function recordingsByDate(recording_list) {
// collapse data into a dictionary of Array's
var dict = {},
date = null;
$(recording_list).each(function(i, val){
date = this.YOUR_DATE_NORMALIZATION_FUNCTION(); // returns something like 'May 1, 2002'
if(!dict[date]){
// recording and series list updates
var device = RemoteScheduling.user.getSelectedDevice();
RemoteScheduling.bindTo(device, RemoteScheduling.Device.events.RECORDINGS_LIST_UPDATED, YOUR_CALLBACK_FUNCTION);
RemoteScheduling.bindTo(device, RemoteScheduling.Device.events.SERIES_LIST_UPDATED, YOUR_CALLBACK_FUNCTION);
// NOTE - anytime the series list is updated we do a refresh on the recordings list as well.
// modify recording
{"error":
{
"message":"Internal error Transformation error: org.xml.sax.SAXParseException: Element type "com:modifyScheduledRecording" must be followed by either attribute specifications, ">" or "/>".; nested exception is javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: Element type "com:modifyScheduledRecording" must be followed by either attribute specifications, ">" or "/>". in net.comcast.cim.remotescheduling.utils.RemoteSchedulingUtils",
"code":"RemoteScheduling:008"
}
}
// series
{"error":