Skip to content

Instantly share code, notes, and snippets.

@edgargoncalves
Created February 19, 2009 15:25
Show Gist options
  • Save edgargoncalves/66959 to your computer and use it in GitHub Desktop.
Save edgargoncalves/66959 to your computer and use it in GitHub Desktop.
// Source code for Buxfer transaction posting with Ubiquity commands.
// By Edgar Gonçalves.
// Version 0.4
// Thu Feb 19 2009.
// Changes in 0.4:
// Removed twixfer. Redone command syntax. Now:
// "BUXFER-SPEND 43.2 IN something".
// "BUXFER-RECEIVE 34 IN a gift".
// Changes in 0.3: internal fixes, global variables unified into a single one, to avoid name clashes.
//////////////////////////////
// Specific noun: a number
//////////////////////////////
var noun_number = {
_name: "number",
suggest: function( text, html ) {
if (!text || typeof text != "string") {
return [];
}
if (text == "") {
// If input is blank, don't suggest.
return [];
}
text = text.replace(",",".");
if (text.charAt(0) == '.')
text = "0" + text;
var number = parseFloat(text);
if (isNaN(number)) {
return this.suggest("0");
}
text = text.split(" ")[0];
return [ CmdUtils.makeSugg(text, null, number)];
}
};
var BUXFERUBIQUITY = {
//////////////////////////////
//Public Variables:
//////////////////////////////
buxferUrl: "www.buxfer.com",
buxferAPIUrl: "https://www.buxfer.com/api/",
//////////////////////////////
// Public Methods:
//////////////////////////////
// gets username and password for a given domain, from Firefox's login-manager.
getLoginInfo: function (domain, fuzzySearch) {
var loginManager = Components.classes["@mozilla.org/login-manager;1"]
.getService(Components.interfaces.nsILoginManager);
var domains = [];
if(fuzzySearch) {
domains.push("http://" + domain);
domains.push("http://www." + domain);
domains.push("https://" + domain);
domains.push("https://www." + domain);
} else {
domains = [domain];
}
for(var i = 0; i < domains.length; i++) {
for(var j = 0; j < domains.length; j++) {
var logins = loginManager.findLogins({}, domains[i], domains[j], null);
if(logins.length > 0)
return {username: logins[0].username,
password: logins[0].password};
}
}
return null;
},
// Function called to log in the user in buxfer.
// Receives the function (with a token and an uid) to be performed after the login is done.
loginUser: function (successCallback) {
if ( !(successCallback)) {
successCallback = function(token, uid){};
}
var username, password;
var loginInfo = this.getLoginInfo(this.buxferUrl, true);
if (loginInfo) {
username = loginInfo.username;
password = loginInfo.password;
} else {
// We have no login stored, send the user to the Buxfer and tell him/her to login
displayMessage("You have to login to Buxfer before using buxfer-send.");
Utils.openUrlInBrowser(this.buxferUrl);
return;
}
var onloadHandler = function(data, status) {
if(status !== "success") {
displayMessage("Error logging in: " + status);
return;
}
var jsonResponse = Utils.decodeJson(data).response;
if(jsonResponse.status === "OK"){
successCallback(jsonResponse.token, jsonResponse.uid);
}else{
displayMessage("Error logging in: "+jsonResponse.status);
}
};
var loginurl = this.buxferAPIUrl +
"login.json?userid=" +
encodeURIComponent(username) +
"&password=" + encodeURIComponent(password);
jQuery.get( loginurl, onloadHandler);
},
// adds a buxfer transaction:
addTransaction: function (description, amount) {
var that = this;
this.loginUser( function(token, uid) {
var transactionUrl = that.buxferAPIUrl + "add_transaction.json";
var onloadHandler = function(data, status) {
if(status !== "success"){
displayMessage("Error logging adding it: " + status);
return;
}
var jsonResponse = Utils.decodeJson(data).response;
if(jsonResponse.status !== "OK"){
displayMessage("Error adding transaction : "+jsonResponse.status);
}
if (!jsonResponse.transactionAdded){
displayMessage("Transaction wasn't added...");
return;
}
if (jsonResponse.parseStatus != "success"){
displayMessage("Transaction wasn't parsed correctly...");
return;
}
displayMessage("Buxfer successfully added your transaction");
};
var text = "'" + description + "'" + " " + amount;
var params = "token="+token+"&format=sms&text="+encodeURIComponent(text);
jQuery.ajax( {
type: "POST",
data: params,
dataType: "text",
url: transactionUrl,
cache: false,
success: onloadHandler,
error: function (XMLHttpRequest, textStatus, errorThrown) {
displayMessage("Something very wrong happened: " + textStatus + ", " + errorThrown);}
}
);
});
},
// generic preview function:
previewBuxferEnteredText: function (p, input, modifiers, verb) {
var subs = {
amount: input.text,
description: modifiers.in.text
};
var msg = "Tell Buxfer to " + verb + " ";
if (subs.amount){
msg +="${amount} EUR";
} else {
msg += "some money";
}
if (subs.description){
msg += " in ${description}";
}
p.innerHTML = CmdUtils.renderTemplate( msg, subs );
},
// generic execution function:
executeBuxferAdd: function (input, modifiers, verb) {
var amount = input.text;
if (amount == 0) {
displayMessage("Amount shouln't be 0...");
return;
}
var description = modifiers.in.text || "Undescribed transaction";
if (verb == "spend"){
this.addTransaction(description, amount);
}
if (verb == "receive"){
this.addTransaction(description, "+" + amount);
}
}
};
CmdUtils.CreateCommand({
name: "buxfer-spend",
homepage: "http://http://sites.google.com/site/edgargoncalves/work/software/ubiquity",
author: {name: "Edgar Gonçalves", email: "edgar.goncalves@gmail.com"},
takes: {"amount": noun_number},
icon: "https://www.buxfer.com/media/favicon/favicon-moneybag.ico",
modifiers: {in: noun_arb_text},
description:"Adds an outgoing transaction to <a href='http://www.buxfer.com'>Buxfer</a>.",
help:"Adds a <a href='http://www.buxfer.com'>Buxfer</a> outgoing transaction with description and amount.<br/>" +
" Usage: &quot;buxfer-spend amount in description&quot;",
preview: function (p, input, modifiers) {
BUXFERUBIQUITY.previewBuxferEnteredText (p, input, modifiers, "spend");
},
execute: function(amountText, modifiers) {
BUXFERUBIQUITY.executeBuxferAdd(amountText, modifiers, "spend");
}
});
CmdUtils.CreateCommand({
name: "buxfer-receive",
homepage: "http://http://sites.google.com/site/edgargoncalves/work/software/ubiquity",
author: {name: "Edgar Gonçalves", email: "edgar.goncalves@gmail.com"},
takes: {"amount": noun_number},
icon: "https://www.buxfer.com/media/favicon/favicon-moneybag.ico",
modifiers: {in: noun_arb_text},
description:"Adds an incoming transaction to <a href='http://www.buxfer.com'>Buxfer</a>.",
help:"Adds a <a href='http://www.buxfer.com'>Buxfer</a> incoming transaction with description and amount.<br/>" +
" Usage: &quot;buxfer-spend amount in description&quot;",
preview: function (p, input, modifiers) {
BUXFERUBIQUITY.previewBuxferEnteredText (p, input, modifiers, "receive");
},
execute: function(amountText, modifiers) {
BUXFERUBIQUITY.executeBuxferAdd(amountText, modifiers, "receive");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment