Skip to content

Instantly share code, notes, and snippets.

@quickredfox
Created February 10, 2010 15:18
Show Gist options
  • Save quickredfox/300418 to your computer and use it in GitHub Desktop.
Save quickredfox/300418 to your computer and use it in GitHub Desktop.
/// THIS IS BROKEN, NEW VERSION TO COME... watch for it in my repos.
/* PraizedActionFacade.js 0.1 - "Facade Pattern" to simplify Praizd API actions for tempalte building
*
* Author: Francois Lafortune
* Copyright (c) 2010 PraizedMedia Inc.
*
* REQUIRES jsonPath() http://goessner.net/articles/JsonPath/
* I felt I should stress this: jsonPath does most of what's magic here and save a lot of lines of code
* in doing so...
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
var PraizedActionFacade = new function PraizedActionFacade(){
var facades = this;
// __noSuchMethod__ (like method_missing) works only in firefox,
// used here to determine via firebug what has yet to be implemented...
facades.__noSuchMethod__ = function(fn){
console.debug('must implement:',fn);
}
/*
No pre-existing standard has been applied here, but
what I came up with goes along the lines of...
{
pid: a pid (string),
name: something's name (string),
value: something's value if it had a name (string||object),
title: if no name then title and text is used (string),
text: something's text (string),
permalink: thing's permalink (string),
subject: the targeted thing (facades::object),
owner: the thing's user (facades::object),
attachments: facades objects colletion (array),
iconURL: the stream icon url (string)
// App specific
appURL: specific to quickredfox's development needs only (string),
params: additional params,
}
*/
facades.merchant = function(merchant){
return {
pid: merchant.pid,
name: merchant.name,
type: 'merchant',
value: (merchant.location ? [merchant.location.street_address,merchant.location.city.name,merchant.location.country.code].join(', ') : 'no address'),
permalink: merchant.permalink,
appURL: 'app://merchants/'+merchant.pid+'.json'
};
}
facades.comment = function(comment){
var targetType = comment.merchant ? 'merchant' : 'comment';
var target = facades[targetType](comment[targetType]);
return {
pid: comment.pid,
type: 'comment',
text: comment.comment,
title: comment.user.display_name+" comments",
subject: target,
permalink: target.permalink,
owner: facades.user(comment.user),
appURL: 'app://'+target.type+'s/'+target.pid+'/comments/'+target.pid+'.json'
}
}
facades.user = function(user){
return {
permalink: user.permalink,
name: user.display_name,
type: 'user',
appURL: 'app://users/'+user.login+'.json'
}
}
facades.question = function(question){
return {
pid: question.pid,
type: 'question',
text: question.content,
owner: facades.user(question.user),
permalink: question.permalink,
title: question.user.display_name+" asks",
params: {
q: question.what,
l: question.where
},
appURL: 'app://questions/'+question.pid+'.json'
}
}
facades.favorite = function(favorite){
var merchant = facades.merchant(favorite.merchant);
return {
type: 'favorite',
title: favorite.user.display_name+" favors",
subject: merchant,
permalink: merchant.permalink,
owner: facades.user(favorite.user),
appURL: 'app://merchants/'+merchant.pid+'.json'
}
}
facades.answer = function(answer){
var attachments = [];
if(answer.merchants)
for(var i=0,m;m=answer.merchants[i++];) attachments.push(facades.merchant(m))
return {
pid: answer.pid,
type: 'answer',
title: answer.user.display_name+" answers",
text: answer.content,
permalink: answer.permalink,
subject: facades.question(answer.question),
owner: facades.user(answer.user),
appURL: 'app://questions/'+answer.question.pid+'/answers/'+answer.pid+'.json',
attachments: attachments
}
}
facades.vote = function(vote){
var target = facades[vote.voteable_type](vote[vote.voteable_type]);
return {
value:(vote.vote?'like':'dislike'),
type: 'vote',
title: vote.user.display_name+(vote.vote?' likes':' dislikes'),
subject: target,
owner: facades.user(vote.user),
appURL: 'app://'+vote.voteable_type+'/'+target.pid+'.json'
}
}
facades.create = function(action){
var data = {};
data.pid = jsonPath(action,'$.pid')[0];
data.appURL = 'app://actions/'+data.pid+".json";
data.type = jsonPath(action,'$..action_type.type_name')[0];
data.type = (data.type == 'praized' ? 'like' : data.type == 'razed' ? 'disliked' : data.type );
data.iconURL = jsonPath(action,'$..avatar.small')[0];
var target = jsonPath(action,'$..targets[0]')[0];
var targetType =(function(t){for(var k in t) return k;})(target);
data.content= facades[targetType](target[targetType]);
if(data.type == 'new_merchant') data.content.title = '"'+data.content.name+"\" has been added";
// data.permalink= data.data.subject.permalink
return data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment